Skip to main content

One state, both directions

Shared state is a single state object that the agent and the UI both read and write. The agent updates it as it works and your React components render it live. When the user edits that same state in the UI, the change flows back so the agent sees it on its next turn. The classic example is a recipe: the agent drafts it, the user tweaks an ingredient or an instruction, and the agent picks up from the edited version. Neither side owns the state; they share it.
Shared state relies on a Flow with custom state. Define an AgentState that subclasses CopilotKitState and type your Flow as Flow[AgentState]. Crews do not carry custom state, so this pattern is Flow-only.

How it works

1

Define the shared state on your Flow

Subclass CopilotKitState so the agent keeps CopilotKit’s message plumbing, then add your own fields. Here the shared field is recipe.
2

Read and write the state from the agent

The agent reads the current state by dumping it into the system prompt, and writes it back by assigning to self.state.recipe. A generate_recipe tool lets the model return the updated recipe as structured arguments.
Two things make this shared rather than one-way: dumping self.state into the prompt means the agent always works from the latest recipe (including edits the user made in the UI), and assigning self.state.recipe puts the new value into the state snapshot sent to connected clients at the end of the step. For updates during a long step, emit explicitly with copilotkit_emit_state (see Agentic Generative UI).
3

Serve the Flow over AG-UI

Expose the Flow from your FastAPI app with add_crewai_flow_fastapi_endpoint, then register it in the CopilotKit runtime. See the Frontend Overview for the full server and runtime setup.
4

Read and write the state from the UI

useAgent gives you both directions in one hook. Read the shared state off agent.state, and write it back with agent.setState(...). Subscribe to OnStateChanged so your component re-renders whenever the agent updates the state.
agent.state reads the shared state, agent.setState(...) writes it back so the agent sees the change on its next turn, and agent.isRunning reflects whether the agent is currently working.
setState replaces the entire state object rather than merging. Always spread the current state ({ ...agent.state, ... }) and override only the fields you are changing, or you will drop the conversation and other runtime fields the agent depends on.

The two-way loop

Putting the pieces together, a single recipe object is kept in sync in both directions:
  • Agent edits, UI updates. The Flow assigns self.state.recipe, the new value ships in the step’s state snapshot, and OnStateChanged re-renders your inputs.
  • User edits, agent sees it. A change in the UI calls agent.setState(...), and because the Flow dumps self.state into its prompt, the agent works from the edited recipe on its next turn.

Agentic Generative UI

Render live agent state as it changes.

Predictive State

Stream in-progress state to the UI as the agent works.

Human-in-the-Loop

Pause the agent to collect user approval or input mid-run.