Back to Copilotkit

Writing agent state

showcase/shell-docs/src/content/docs/integrations/pydantic-ai/shared-state/in-app-agent-write.mdx

1.57.05.3 KB
Original Source

<IframeSwitcher id="shared-state-write-example" exampleUrl="https://feature-viewer.copilotkit.ai/pydantic-ai/feature/shared_state?sidebar=false&chatDefaultOpen=false" codeUrl="https://feature-viewer.copilotkit.ai/pydantic-ai/feature/shared_state?view=code&sidebar=false&codeLayout=tabs" exampleLabel="Demo" codeLabel="Code" height="700px" />

<Callout type="info"> This example demonstrates writing to shared state in the [CopilotKit Feature Viewer](https://feature-viewer.copilotkit.ai/pydantic-ai/feature/shared_state). </Callout>

What is this?

This guide shows you how to write to your agent's state from your application.

When should I use this?

You can use this when you want to provide the user with feedback about what your agent is doing, specifically when your agent is calling tools. CopilotKit allows you to fully customize how these tools are rendered in the chat.

Implementation

<Steps> <Step> ### Run and connect your agent <RunAndConnect /> </Step> <Step> ### Define the Agent State
Create your Pydantic AI agent with a stateful structure. Here's a complete example that tracks language:

```python title="agent.py"
from textwrap import dedent

from pydantic import BaseModel
from pydantic_ai import Agent, RunContext
from pydantic_ai.ag_ui import StateDeps


class AgentState(BaseModel):
    """State for the agent."""
    language: str = "english"


agent = Agent("openai:gpt-5.4-mini", deps_type=StateDeps[AgentState])


@agent.instructions()
async def language_instructions(ctx: RunContext[StateDeps[AgentState]]) -> str:
    """Instructions for the language tracking agent.

    Args:
        ctx: The run context containing language state information.

    Returns:
        Instructions string for the language tracking agent.
    """
    return dedent(
        f"""
        You are a helpful assistant for tracking the language.

        IMPORTANT:
        - ALWAYS use the lower case for the language
        - ALWAYS response in the current language: {ctx.deps.state.language}
        """
    )


app = agent.to_ag_ui(deps=StateDeps(AgentState()))

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
```
</Step> <Step> ### Call `agent.setState` from the `useAgent` hook `useAgent` returns an `agent` object with a `setState` method that you can use to update the agent state. Calling this will update the agent state and trigger a rerender of anything that depends on the agent state.
```tsx title="ui/app/page.tsx"

// Define the agent state type, should match the actual state of your agent
type AgentState = {
  language: "english" | "spanish";
}

// Example usage in a pseudo React component
function YourMainContent() {
  const { agent } = useAgent({ // [!code highlight]
    agentId: "my_agent",
    initialState: { language: "english" }  // optionally provide an initial state
  });

  // ...

  const toggleLanguage = () => {
    agent.setState({ language: agent.state?.language === "english" ? "spanish" : "english" }); // [!code highlight]
  };

  // ...

  return (
    // style excluded for brevity
    <div>
      <h1>Your main content</h1>
      <p>Language: {agent.state?.language}</p>
      <button onClick={toggleLanguage}>Toggle Language</button>
    </div>
  );
}
```

<Callout type="warn" title="Important">
  The `name` parameter must exactly match the agent name you defined in your CopilotRuntime configuration (e.g., `my_agent` from the quickstart).
</Callout>
</Step> <Step> ### Give it a try! You can now use `agent.setState` to update the agent state and `agent.state` to read it. Try toggling the language button and talking to your agent. You'll see the language change to match the agent's state. </Step> </Steps>

Advanced Usage

Re-run the agent with a hint about what's changed

The new agent state will be used next time the agent runs. If you want to re-run it manually, use copilotkit.runAgent().

The agent will be re-run with the latest updated state. You can also add a hint message before re-running.

tsx

// ...

function YourMainContent() {
  const { agent } = useAgent({
    agentId: "my_agent",
  });
  const { copilotkit } = useCopilotKit(); // [!code highlight]

  // setup to be called when some event in the app occurs
  const toggleLanguage = async () => {
    const newLanguage = agent.state?.language === "english" ? "spanish" : "english";
    agent.setState({ language: newLanguage });

    // add a hint message and re-run the agent
    // [!code highlight:7]
    agent.addMessage({
      id: crypto.randomUUID(),
      role: "user",
      content: `the language has been updated to ${newLanguage}`,
    });
    await copilotkit.runAgent({ agent });
  };

  return (
    // ...
  );
}

Intermediately Stream and Render Agent State

By default, the Pydantic AI Agent state will only update between Pydantic AI Agent node transitions -- which means state updates will be discontinuous and delayed.

{/* You likely want to render the agent state as it updates continuously.

See emit intermediate state. */}