showcase/integrations/claude-sdk-python/docs/setup/shared-state-setup.mdx
The demo exposes frontend preferences to Claude and gives the agent a tool
for writing notes back into CopilotKit state. Keep the state contract small,
typed, and mirrored by the UI components that read the same values.
<DemoCode file="src/agents/shared_state_read_write_agent.py" region="shared-state-setup" />
This dedicated demo uses the Anthropic Messages API directly. Pass the
schema to `client.messages.stream` so Claude can call `set_notes`.
<DemoCode
file="src/agents/shared_state_read_write_agent.py"
region="shared-state-tool-registration"
/>
When Claude calls `set_notes`, replace the notes array, emit a
`StateSnapshotEvent` for the UI, and return a `ToolCallResultEvent` to
Claude before the loop continues.
<DemoCode
file="src/agents/shared_state_read_write_agent.py"
region="shared-state-set-notes"
/>
Mount the dedicated runner on `/shared-state-read-write` and return its
AG-UI events as a streaming response.
<DemoCode
file="src/agent_server.py"
region="shared-state-read-write-route"
/>