What is AG-UI?
AG-UI (Agent-UI) is an event-based communication protocol that enables standardized, streaming communication between AI agents and user interfaces. All three layers of CopilotKit communicate exclusively through AG-UI events streamed over Server-Sent Events (SSE).AG-UI events follow a structured lifecycle and are validated with Zod schemas to ensure type safety and consistency across the stack.
Protocol Fundamentals
Event-Based Architecture
Instead of traditional request-response patterns, AG-UI uses a unidirectional event stream:- Real-time streaming - Text and data stream as they’re generated
- Progressive rendering - UI updates incrementally as events arrive
- Tool execution visibility - Track tool calls in real-time
- Structured lifecycle - Clear start/end boundaries for runs and steps
Transport Layer
Events are transported via Server-Sent Events (SSE) with these characteristics:- HTTP-based streaming - Works with standard HTTP infrastructure
- Automatic reconnection - Browser automatically reconnects on connection loss
- Text-based format - Easy to debug and monitor
- Event-type routing - Events are tagged with their type for client-side handling
packages/v2/core/src/agent.ts:148
Event Lifecycle
Every agent run follows a predictable event sequence:The lifecycle provides clear boundaries for tracking progress, handling errors, and managing state persistence.
Core Event Types
Run Lifecycle Events
These events mark the boundaries of an agent execution:RUN_STARTED
Emitted when the agent begins processing a request.
packages/v2/runtime/src/runner/in-memory.ts:129
RUN_FINISHED
Emitted when the agent completes successfully.
packages/v2/core/src/intelligence-agent.ts:118
RUN_ERROR
Emitted when the agent encounters an unrecoverable error.
packages/v2/core/src/intelligence-agent.ts:121
Step Events
Some agent frameworks (like LangGraph) execute in discrete steps:STEP_STARTED
Marks the beginning of a processing step.
STEP_FINISHED
Marks the completion of a step.
Message Events
Message events enable real-time streaming of agent responses:TEXT_MESSAGE_START
Begins a new message in the conversation.
packages/v2/sqlite-runner/src/__tests__/sqlite-runner.test.ts:132
TEXT_MESSAGE_CONTENT
Streams content chunks as the agent generates text.
packages/v2/sqlite-runner/src/__tests__/sqlite-runner.test.ts:140
TEXT_MESSAGE_END
Marks the completion of a message.
Tool Events
Tool events provide visibility into function calling:TOOL_CALL_START
Emitted when the agent begins calling a tool.
packages/v2/sqlite-runner/src/__tests__/sqlite-runner.test.ts:148
TOOL_CALL_ARGS
Provides the arguments for the tool call.
packages/v2/sqlite-runner/src/__tests__/sqlite-runner.e2e.test.ts:265
TOOL_CALL_END
Marks the end of a tool call (arguments are complete).
packages/v2/sqlite-runner/src/__tests__/sqlite-runner.e2e.test.ts:270
TOOL_CALL_RESULT
Contains the result returned by the tool.
packages/v2/sqlite-runner/src/__tests__/sqlite-runner.e2e.test.ts:274
Custom Events
The protocol supports custom events for domain-specific needs:CUSTOM
packages/v2/core/src/intelligence-agent.ts:59
Event Flow Examples
Simple Q&A Interaction
Tool-Assisted Response
Multi-Step Agent Workflow
Event Processing
Frontend Event Handling
The frontend subscribes to the event stream via Observables:packages/v2/core/src/agent.ts:172
Runtime Event Emission
Agents emit events through theonEvent callback:
packages/v2/runtime/src/runner/in-memory.ts:157
Error Handling
Graceful Degradation
The protocol includes mechanisms for handling incomplete or interrupted streams:- Event finalization - Missing
_ENDevents are automatically generated - Zod validation - Events are validated, with invalid events logged but not crashing
- Abort handling - ZodErrors from aborted streams are suppressed
packages/v2/core/src/agent.ts:30
Event Validation
All events are validated against Zod schemas before processing:State Management
Event Compaction
To optimize storage and transmission, the runtime can compact event streams:- Merge consecutive
TEXT_MESSAGE_CONTENTevents - Deduplicate redundant state updates
- Remove intermediate events that don’t affect final state
packages/v2/runtime/src/runner/in-memory.ts:213
History Replay
The AgentRunner can replay historic events when reconnecting:- Session restoration - Users can refresh without losing context
- Multi-device sync - Same conversation across devices
- Audit trails - Complete history of agent interactions
packages/v2/runtime/src/runner/in-memory.ts:294
Best Practices
For Agent Developers
- Emit events in order - Follow the lifecycle sequence
- Always emit terminal events - Every
RUN_STARTEDneeds aRUN_FINISHEDorRUN_ERROR - Close all messages - Every
TEXT_MESSAGE_STARTneeds aTEXT_MESSAGE_END - Include context - Attach
messageId,runId,threadIdwhere relevant - Validate early - Check event structure before emitting
For Frontend Developers
- Handle all event types - Don’t assume a specific sequence
- Expect streaming - Process content incrementally
- Show progress - Use lifecycle events to display loading states
- Handle errors gracefully - Subscribe to both
nextanderror - Clean up subscriptions - Unsubscribe when component unmounts
For Runtime Developers
- Preserve event order - Don’t reorder or batch events incorrectly
- Monitor stream health - Log errors and disconnections
- Implement backpressure - Don’t overwhelm slow clients
- Compact wisely - Only compact when safe (not during active runs)
Next Steps
Architecture
Understand the three-layer architecture
Agents
Learn how agents emit AG-UI events
Runtime
Explore AgentRunner and event streaming
Frontend Integration
Handle events in your UI
