State Management
State management is a core feature of the AG-UI protocol that enables real-time synchronization between agents and frontend applications. By providing efficient mechanisms for sharing and updating state, AG-UI creates a foundation for collaborative experiences where both AI agents and human users can work together seamlessly.Shared State Architecture
In AG-UI, state is a structured data object that:- Persists across interactions with an agent
- Can be accessed by both the agent and the frontend
- Updates in real-time as the interaction progresses
- Provides context for decision-making on both sides
- Agents can access the application’s current state to make informed decisions
- Frontends can observe and react to changes in the agent’s internal state
- Both sides can modify the state, creating a collaborative workflow
State Synchronization Methods
AG-UI provides two complementary methods for state synchronization:State Snapshots
TheSTATE_SNAPSHOT
event delivers a complete representation of an agent’s
current state:
- At the beginning of an interaction to establish the initial state
- After connection interruptions to ensure synchronization
- When major state changes occur that require a complete refresh
- To establish a new baseline for future delta updates
STATE_SNAPSHOT
event, it should replace its
existing state model entirely with the contents of the snapshot.
State Deltas
TheSTATE_DELTA
event delivers incremental updates to the state using JSON
Patch format (RFC 6902):
- Frequent small updates during streaming interactions
- Large state objects where most properties remain unchanged
- High-frequency updates that would be inefficient to send as full snapshots
JSON Patch Format
AG-UI uses the JSON Patch format (RFC 6902) for state deltas, which defines a standardized way to express changes to a JSON document:-
add: Adds a value to an object or array
-
replace: Replaces a value
-
remove: Removes a value
-
move: Moves a value from one location to another
STATE_SNAPSHOT
.
State Processing in AG-UI
In the AG-UI implementation, state deltas are applied using thefast-json-patch
library:
- Patches are applied atomically (all or none)
- The original state is not mutated during the application process
- Errors are caught and handled gracefully
Human-in-the-Loop Collaboration
The shared state system is fundamental to human-in-the-loop workflows in AG-UI. It enables:- Real-time visibility: Users can observe the agent’s thought process and current status
- Contextual awareness: The agent can access user actions, preferences, and application state
- Collaborative decision-making: Both human and AI can contribute to the evolving state
- Feedback loops: Humans can correct or guide the agent by modifying state properties
CopilotKit Implementation
CopilotKit, a popular framework for building AI assistants, leverages AG-UI’s state management system through its “shared state” feature. This implementation enables bidirectional state synchronization between agents (particularly LangGraph agents) and frontend applications. CopilotKit’s shared state system is implemented through:- Reading the agent’s current state in the frontend
- Updating the agent’s state from the frontend
- Rendering UI components based on the agent’s state
Best Practices
When implementing state management in AG-UI:- Use snapshots judiciously: Full snapshots should be sent only when necessary to establish a baseline.
- Prefer deltas for incremental changes: Small state updates should use deltas to minimize data transfer.
- Structure state thoughtfully: Design state objects to support partial updates and minimize patch complexity.
- Handle state conflicts: Implement strategies for resolving conflicting updates from agent and frontend.
- Include error recovery: Provide mechanisms to resynchronize state if inconsistencies are detected.
- Consider security implications: Avoid storing sensitive information in shared state.