# Shared State

> Create a two-way connection between your React Native UI and agent state.

## What is shared state? [#what-is-shared-state]

Shared state connects your app's UI with the agent's run. With shared state you can:

- Display the agent's current progress and intermediate results
- Update the agent's state through UI interactions
- React to state changes in real time across your app

<ImageZoom
  src="https://cdn.copilotkit.ai/docs/copilotkit/images/coagents/SharedStateCoAgents.gif"
  alt="Shared State Demo"
  width={1000}
  height={1000}
  className="rounded-lg shadow-lg border mt-0"
/>

## When should I use this? [#when-should-i-use-this]

Use shared state when you want the agent and the user to work on the same app state. The UI shows what the agent writes. The agent reads what the user changes in the UI.

<OpsPlatformCTA
  variant="inline"
  title="Building stateful agents?"
  body="Persistent threads ship with CopilotKit Intelligence on the free Developer tier."
  surface="docs_shared_state"
/>

## Reading agent state [#reading-agent-state]

<Steps>
  <Step>
    ### Keep the state in conversation variables

    On AG2, shared state is the agent's **conversation variables**
    (`context.variables`). `AGUIStream` syncs them in both directions, so you
    do not build state events yourself:

    - State that the UI writes with `agent.setState` arrives on
      `RunAgentInput.state` and merges into the variables for the run.
    - At the start of the run, `AGUIStream` sends a `STATE_SNAPSHOT` if the
      merged variables differ from the state the client sent.
    - At the end of the run, it sends another `STATE_SNAPSHOT` if a tool
      changed `context.variables`.

    A snapshot replaces the client's state as a whole, so each one carries the
    client's keys and the server's variables together.

    ```python title="agent.py"
    from typing import Annotated

    from fastapi import FastAPI, Header
    from fastapi.responses import StreamingResponse
    from pydantic import Field

    from ag2 import Agent, Context, tool
    from ag2.ag_ui import AGUIStream, RunAgentInput
    from ag2.config import OpenAIResponsesConfig

    @tool
    def set_language(
        language: Annotated[str, Field(description="language such as english or spanish")],
        context: Context,
    ) -> str:
        """Update the language in shared state."""
        context.variables["language"] = language.lower()
        return f"Language set to {language}"

    agent = Agent(
        name="assistant",
        prompt=(
            "You are a helpful assistant for tracking language. "
            "Always respond in the current language."
        ),
        config=OpenAIResponsesConfig(model="gpt-6-luna"),
        tools=[set_language],
    )

    stream = AGUIStream(agent)
    app = FastAPI()

    @app.post("/chat")
    async def run_agent(
        message: RunAgentInput,
        accept: str | None = Header(None),
    ) -> StreamingResponse:
        return StreamingResponse(
            stream.dispatch(message, accept=accept),
            media_type=accept or "text/event-stream",
        )
    ```

    Everything in the variables is sent to the browser. Do not put secrets
    there. Pass them with `dispatch(dependencies=...)`, which never leaves the
    server.

  </Step>
  <Step>
    ### Do not seed state with `Agent(variables=...)`

    Agent variables take precedence over the incoming `RunAgentInput.state` on
    every run. A default set with `Agent(variables={"language": "english"})`
    overwrites whatever the UI wrote with `agent.setState`, and resets a value
    the agent changed on the previous turn. Nothing reports an error. Provide
    defaults in the UI instead, or with `context.variables.setdefault` inside a
    tool.

  </Step>
  <Step>
    ### Send intermediate snapshots from long tools

    The automatic snapshots only arrive at the start and the end of a run. To
    update the UI while a long tool runs, send a snapshot from an async tool
    with `context.send`:

    ```python title="agent.py"
    from ag_ui.core import StateSnapshotEvent

    from ag2 import Context, tool
    from ag2.ag_ui import AGUIEvent

    @tool
    async def long_running_task(context: Context) -> str:
        """A long task that streams state updates as it progresses."""
        for step in range(3):
            context.variables["progress"] = step
            await context.send(
                AGUIEvent(StateSnapshotEvent(snapshot=dict(context.variables)))
            )
        return "Done"
    ```

  </Step>
</Steps>

Subscribe a component to the agent's state with `useAgent`. Each time the agent changes its state, for example from a tool call, the hook re-renders your component with the new values.

```tsx title="src/NotesCard.tsx"
import { Text, View } from "react-native";
import { useAgent, UseAgentUpdate } from "@copilotkit/react-native/headless";

type NotesState = {
  notes?: string[];
  preferences?: { tone?: string };
};

export function NotesCard() {
  const { agent } = useAgent({ // [!code highlight:4]
    agentId: "default",
    updates: [UseAgentUpdate.OnStateChanged],
  });
  const state = (agent.state ?? {}) as NotesState;

  return (
    <View style={{ padding: 16, gap: 4 }}>
      {(state.notes ?? []).map((note) => (
        <Text key={note}>• {note}</Text>
      ))}
    </View>
  );
}
```

`agent.state` is a plain object. Read it like any other React data and render the parts you care about: notes the agent wrote, structured output, progress, or anything else the agent put there.

## Writing agent state [#writing-agent-state]

The same `agent` has a `setState` method. Call it from a UI event, and the agent reads the new value on its next turn. The UI's writes steer the model.

```tsx
import { Button } from "react-native";

// In NotesCard, below the list:
<Button
  title="Prefer short answers"
  onPress={() =>
    agent.setState({
      ...state,
      preferences: { ...state.preferences, tone: "concise" },
    })
  }
/>
```

This makes shared state two-way. The UI can send the agent new inputs, such as preferences, selections, or partial work, without the chat.

The agent reads the new state on its next run. To run it right away, call `copilotkit.runAgent({ agent })` (from `useCopilotKit`) after `setState`. Running through `copilotkit` also sends your frontend tools and context with the run. To tell the model what changed, add a message first:

```tsx
const { copilotkit } = useCopilotKit();

agent.setState({ ...state, language: newLanguage });
agent.addMessage({
  id: `user-${Date.now()}`,
  role: "user",
  content: `The language has been updated to ${newLanguage}.`,
});
await copilotkit.runAgent({ agent });
```

See [Programmatic Control](/react-native/ag2/programmatic-control) for more ways to run the agent from code.

## Rendering shared state in the UI [#rendering-shared-state-in-the-ui]

Because `agent.state` is plain React data, the UI layer is whatever you'd normally build. Nothing about it is chat-specific: `useAgent` works in any component under `CopilotKitProvider`, so you can render the state on your main screen, not just next to the chat. See **[Render agent state in your app](/react-native/ag2/shared-state/rendering-in-app)** for the full pattern.

## Streaming partial state updates [#streaming-partial-state-updates]

By default, agent state only updates *between* backend checkpoints, so a long tool call shows up as one burst at the end. State streaming copies one tool argument into a state key while the model writes it, so the UI shows the answer token by token.

<WhenFrameworkSupports feature="shared-state-streaming">

> **Not available for AG2 yet.** The Shared State: Streaming demo for AG2 can't run yet, so its code isn't shown here.

See **[State Streaming](/react-native/ag2/shared-state/streaming)** for the full walkthrough, including the `useAgent` subscription on the frontend.

</WhenFrameworkSupports>

<WhenFrameworkSupports feature="shared-state-streaming" not>

State streaming isn't available on this framework yet. The UI gets the agent's state between checkpoints.

</WhenFrameworkSupports>

## Read-only context [#read-only-context]

When a value is **UI-owned**, and the agent should read it but never write it back, use `useAgentContext` instead of shared state. Examples are the current user, the selected record, or the open screen. It sends values one way, from the UI to the agent, and removes them when the component unmounts.

See **[Agent Read-Only Context](/react-native/ag2/shared-state/agent-readonly)** for the full pattern.

<IntegrationGrid path="shared-state" exclude={["agno", "agent-spec", "spring-ai", "langroid"]} />
