|
| 1 | +--- |
| 2 | +title: Shared state |
| 3 | +icon: "lucide/Repeat" |
| 4 | +description: Write to agent's state from your application. |
| 5 | +--- |
| 6 | + |
| 7 | +<video |
| 8 | + src="https://cdn.copilotkit.ai/docs/copilotkit/images/coagents/write-agent-state.mp4" |
| 9 | + className="rounded-lg shadow-xl" |
| 10 | + loop |
| 11 | + playsInline |
| 12 | + controls |
| 13 | + autoPlay |
| 14 | + muted |
| 15 | +/> |
| 16 | +<Callout> |
| 17 | + This video shows the result of `npx copilotkit@latest init` with the [implementation](#implementation) section applied to it! |
| 18 | +</Callout> |
| 19 | + |
| 20 | +## What is this? |
| 21 | + |
| 22 | +This guide shows you how to read and write to your agent's state from your application. LlamaIndex's `get_ag_ui_workflow_router` handler allows you to have stateful concepts that are automatically loaded into the LLM's context. |
| 23 | + |
| 24 | +From the front-end you can modify this state directly or via tools - enabling a bi-directional concept of state. |
| 25 | + |
| 26 | +## When should I use this? |
| 27 | + |
| 28 | +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. |
| 29 | + |
| 30 | +## Implementation |
| 31 | + |
| 32 | +<Steps> |
| 33 | + <Step> |
| 34 | + ### Run and Connect Your Agent to CopilotKit |
| 35 | + |
| 36 | + You'll need to run your agent and connect it to CopilotKit before proceeding. If you haven't done so already, |
| 37 | + you can follow the instructions in the [Getting Started](/llamaindex/quickstart) guide. |
| 38 | + |
| 39 | + </Step> |
| 40 | + <Step> |
| 41 | + ### Define the Agent State |
| 42 | + |
| 43 | + First, define the agent's state in the workflow router. |
| 44 | + |
| 45 | + ```python title="agent/agent/agent.py" |
| 46 | + from llama_index.llms.openai import OpenAI |
| 47 | + from llama_index.protocols.ag_ui.router import get_ag_ui_workflow_router |
| 48 | + |
| 49 | + agentic_chat_router = get_ag_ui_workflow_router( |
| 50 | + llm=OpenAI(model="gpt-4.1"), |
| 51 | + system_prompt=SYSTEM_PROMPT, |
| 52 | + initial_state={ |
| 53 | + "language": "english" |
| 54 | + }, |
| 55 | + ) |
| 56 | + ``` |
| 57 | + |
| 58 | + </Step> |
| 59 | + <Step> |
| 60 | + ### Call `setState` function from the `useCoAgent` hook |
| 61 | + `useCoAgent` returns a `setState` function that you can use to update the agent state. Calling this |
| 62 | + will update the agent state and trigger a rerender of anything that depends on the agent state. |
| 63 | + |
| 64 | + ```tsx title="ui/app/page.tsx" |
| 65 | + import { useCoAgent } from "@copilotkit/react-core"; |
| 66 | + |
| 67 | + // Define the agent state type, should match the actual state of your agent |
| 68 | + type AgentState = { |
| 69 | + language: "english" | "spanish"; |
| 70 | + } |
| 71 | + |
| 72 | + // Example usage in a pseudo React component |
| 73 | + function YourMainContent() { |
| 74 | + const { state, setState } = useCoAgent<AgentState>({ |
| 75 | + name: "sample_agent", |
| 76 | + initialState: { language: "spanish" } // optionally provide an initial state |
| 77 | + }); |
| 78 | + |
| 79 | + // ... |
| 80 | + |
| 81 | + const toggleLanguage = () => { |
| 82 | + setState({ language: state.language === "english" ? "spanish" : "english" }); |
| 83 | + }; |
| 84 | + |
| 85 | + // ... |
| 86 | + |
| 87 | + return ( |
| 88 | + // style excluded for brevity |
| 89 | + <div> |
| 90 | + <h1>Your main content</h1> |
| 91 | + <p>Language: {state.language}</p> // [!code highlight:2] |
| 92 | + <button onClick={toggleLanguage}>Toggle Language</button> |
| 93 | + </div> |
| 94 | + ); |
| 95 | + } |
| 96 | + ``` |
| 97 | + |
| 98 | + </Step> |
| 99 | + <Step> |
| 100 | + ### Give it a try! |
| 101 | + You can now use the `setState` function to update the agent state and `state` to read it. Try toggling the language button |
| 102 | + and talking to your agent. You'll see the language change to match the agent's state. |
| 103 | + </Step> |
| 104 | +</Steps> |
| 105 | + |
| 106 | +## Advanced Usage |
| 107 | + |
| 108 | +### Re-run the agent with a hint about what's changed |
| 109 | + |
| 110 | +The new agent state will be used next time the agent runs. |
| 111 | +If you want to re-run it manually, use the `run` argument on the `useCoAgent` hook. |
| 112 | + |
| 113 | +The agent will be re-run, and it will get not only the latest updated state, but also a **hint** that can depend on the data delta between the previous and the current state. |
| 114 | + |
| 115 | +```tsx title="ui/app/page.tsx" |
| 116 | +import { useCoAgent } from "@copilotkit/react-core"; |
| 117 | +import { TextMessage, MessageRole } from "@copilotkit/runtime-client-gql"; // [!code highlight] |
| 118 | + |
| 119 | +// ... |
| 120 | + |
| 121 | +function YourMainContent() { |
| 122 | + const { state, setState, run } = useCoAgent<AgentState>({ |
| 123 | + name: "sample_agent", |
| 124 | + initialState: { language: "spanish" } // optionally provide an initial state |
| 125 | + }); |
| 126 | + |
| 127 | + // setup to be called when some event in the app occurs |
| 128 | + const toggleLanguage = () => { |
| 129 | + const newLanguage = state.language === "english" ? "spanish" : "english"; |
| 130 | + setState({ language: newLanguage }); |
| 131 | + |
| 132 | + // re-run the agent and provide a hint about what's changed |
| 133 | + run(({ previousState, currentState }) => { // [!code highlight:6] |
| 134 | + return new TextMessage({ |
| 135 | + role: MessageRole.User, |
| 136 | + content: `the language has been updated to ${currentState.language}`, |
| 137 | + }); |
| 138 | + }); |
| 139 | + }; |
| 140 | + |
| 141 | + return ( |
| 142 | + // ... |
| 143 | + ); |
| 144 | +} |
| 145 | +``` |
0 commit comments