Shared State
Create a two-way connection between your UI and AG2 agent state.
Open your coding agent in your project's folder, or in an empty folder for a new app.This runs in a coding agent on your computer.
What is shared state?#
CoAgents maintain a shared state that seamlessly connects your UI with the agent's execution. This shared state system allows you to:
- 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 application
The foundation of this system is built on AG2 conversation variables (Context.variables) and AG-UI StateSnapshotEvents, which AGUIStream emits automatically at the start of the run (when the agent or dispatch() defines initial variables) and again at the end of the run when a tool has changed the variables.
CopilotKit consumes AG-UI protocol events streamed by AG2 over /chat. See the AG2 AG-UI integration docs.
When should I use this?#
State streaming is perfect when you want to facilitate collaboration between your agent and the user. Any state that your AG2 backend emits in snapshot events will be automatically shared by the UI. Similarly, any state that the user updates in the UI will be automatically reflected.
This allows for a consistent experience where both the agent and the user are on the same page.