Lesson 7 of 12 · 4 min read
Streaming a Run to the User with AG-UI
In this lesson
TL;DR: An agent that only returns a final answer leaves the user staring at a spinner. AG-UI standardises the stream between an agent backend and the screen: the client sends one request describing the thread, the messages, the tools the front end offers and the current shared state; the agent streams back typed events, from
RUN_STARTEDthrough text and tool-call chunks, state deltas and reasoning, toRUN_FINISHEDorRUN_ERROR. A tool defined by the front end is run by the application, which hands control back across the run boundary. Because every framework emits the same events, one front end can drive many backends.
After this lesson, you should be able to place AG-UI next to MCP and A2A, describe a run's request and event stream, explain how shared state stays in sync, and design front-end tools and interrupts so that the user, not the model, confirms side effects.
Before you read
Commit to an answer now. The lesson that follows explains it, and having guessed first, even wrongly, makes the explanation easier to remember.
1Your agent takes forty seconds to research and draft a reply. Users abandon it. The backend is fine. What is missing between the agent and the screen?
Three protocols, three edges
AG-UI's own framing is the simplest map: MCP connects an agent to tools and data, A2A connects an agent to other agents, and AG-UI connects an agent to the user. They are complementary. A single product might call its tools over MCP, delegate to a partner's agent over A2A, and stream everything to its web app over AG-UI.
The request and the stream
A run starts with one request, RunAgentInput. Its fields, as listed in the specification checked November 2026:
| Field | What it carries |
|---|---|
threadId, runId | Which conversation, which attempt |
messages | The conversation so far, including results of front-end tools |
tools | Tools the front end offers and will execute itself |
context | Description and value pairs the app wants the agent to know |
state | The shared state both sides last agreed on |
resume | Answers to an interrupt from the previous run |
forwardedProps | Passed through untouched |
The response is one ordered stream of typed events. AG-UI 1.0 defines 31 event types in eight families:
Run lifecycle
the only mandatory family
- RUN_STARTED, RUN_FINISHED, RUN_ERROR
- STEP_STARTED, STEP_FINISHED
Text messages
streamed replies
- START, CONTENT, END
- CHUNK as a shorthand
Tool calls
arguments as they arrive
- START, ARGS, END
- RESULT, CHUNK
State
shared between agent and app
- STATE_SNAPSHOT replaces
- STATE_DELTA as JSON Patch, MESSAGES_SNAPSHOT
Reasoning
what the model is thinking, if exposed
- REASONING_START to REASONING_END
- Encrypted values pass through
Activity
progress that is not a message
- ACTIVITY_SNAPSHOT
- ACTIVITY_DELTA
Subagents
work delegated inside the run
- SUBAGENT_STARTED
- SUBAGENT_FINISHED, SUBAGENT_ERROR
Passthrough
escape hatches
- RAW from the underlying framework
- CUSTOM for app-specific events
Transport is HTTP with Server-Sent Events, or a binary HTTP binding with Protobuf. Middleware can translate events from frameworks that do not speak AG-UI natively. Version 1.0 is frozen and backward compatible with 0.x in both directions; 1.1 is a working draft no SDK implements yet.
Keeping state in sync
Shared state is what lets an agent and an interface work on the same thing: a draft itinerary, a form half filled in, a list of search results the user is pruning. AG-UI uses two events. A snapshot replaces the client's copy outright. A delta is a JSON Patch computed against the last state both sides agreed, which the request's state field carries. A delta that does not apply cleanly is a signal to ask for a snapshot, not to guess.
Here is a client reducer that applies a short run. It shows three rules that matter in a real interface: nothing before RUN_STARTED, nothing after the run ends, and state deltas applied as patches:
def apply_patch(state, ops):
for op in ops:
*parents, key = op["path"].strip("/").split("/")
target = state
for p in parents:
target = target[int(p)] if isinstance(target, list) else target[p]
if op["op"] == "add" and isinstance(target, list):
target.insert(len(target) if key == "-" else int(key), op["value"])
elif op["op"] in ("add", "replace"):
target[key] = op["value"]
elif op["op"] == "remove":
del target[key]
return state
def reduce(events):
ui = {"status": "idle", "text": "", "state": {}, "tools": []}
for e in events:
t = e["type"]
if ui["status"] == "idle" and t != "RUN_STARTED":
raise ValueError(f"{t} before RUN_STARTED")
if ui["status"] in ("finished", "error"):
raise ValueError(f"{t} after the run ended")
if t == "RUN_STARTED":
ui["status"] = "running"
elif t == "TEXT_MESSAGE_CONTENT":
ui["text"] += e["delta"]
elif t == "TOOL_CALL_START":
ui["tools"].append(e["toolCallName"])
elif t == "STATE_SNAPSHOT":
ui["state"] = e["snapshot"]
elif t == "STATE_DELTA":
apply_patch(ui["state"], e["delta"])
elif t == "RUN_FINISHED":
ui["status"] = "finished"
elif t == "RUN_ERROR":
ui["status"] = "error"
return ui
run = [
{"type": "RUN_STARTED"},
{"type": "STATE_SNAPSHOT", "snapshot": {"trip": {"stops": ["Lyon"]}}},
{"type": "TOOL_CALL_START", "toolCallName": "search_trains"},
{"type": "STATE_DELTA",
"delta": [{"op": "add", "path": "/trip/stops/-", "value": "Turin"}]},
{"type": "TEXT_MESSAGE_CONTENT", "delta": "Added Turin. "},
{"type": "TEXT_MESSAGE_CONTENT", "delta": "Book it?"},
{"type": "RUN_FINISHED"},
]
ui = reduce(run)
print(ui["status"], ui["state"]["trip"]["stops"], ui["text"])
# => finished ['Lyon', 'Turin'] Added Turin. Book it?
The patch helper is deliberately minimal (add, replace and remove on simple paths). Use a full JSON Patch library in production, and treat a failed patch as a request for a fresh snapshot.
Front-end tools and interrupts
Some actions belong to the interface: open a date picker, show a confirmation dialog, read the user's current selection. The front end lists these in the request's tools. When the agent calls one, the application executes it, not the agent, and calling it hands control back across the run boundary. The tool's result comes back as a tool message in messages on the next run.
Interrupts work the same way at a coarser grain. A run can end with an interrupt outcome, for example to ask for approval before a payment, and the next run carries the user's answer in resume. Both patterns keep the person in the loop without holding a connection open while they think.
This is the right place to put confirmation of side effects. The draft specification says applications should get explicit user consent before executing side-effectful tool calls. An agent cannot click "approve" on a dialog it does not render.
Security rules worth repeating
- Model output is untrusted input. The specification says applications must not render streamed content as executable markup. Render text as text; never inject a streamed string as HTML.
- No secrets in state or messages. Both travel to the browser and back; anything in them is visible to the user and anyone with their session.
- The server still authorises. A front-end confirmation is a user-experience control. The backend that executes the payment or the delete must check the user's authority itself.
Where it is supported
Checked November 2026: AG-UI lists LangChain and CrewAI as partners and built-in support in Microsoft Agent Framework, Google ADK, AWS Strands Agents, Mastra, Pydantic AI, LlamaIndex and others, with community integrations for the Claude Agent SDK and OpenAI Agents SDK. Client libraries include CopilotKit and React Native. Treat that list as dated: integrations arrive monthly.
Say it in 30 seconds
"AG-UI is the agent-to-user protocol, next to MCP for tools and A2A for other agents. The client sends one RunAgentInput with the thread, messages, front-end tools, context and shared state, and the agent streams typed events: run lifecycle, text and tool-call chunks, state snapshots and JSON Patch deltas, reasoning, activity, subagents and custom events. Front-end tools run in the application and hand control back; interrupts end a run and the next run resumes with the answer. I render streamed output as text, keep secrets out of state, and make the backend authorise every side effect even when the UI asked the user first."
Check yourself
Answer aloud or on paper before revealing. Producing the explanation unaided is exactly what a panel listens for.
1Which AG-UI event family is mandatory for a producer, and why does a client still need to enforce ordering?
2A STATE_DELTA fails to apply on the client. What is the right response?
3Why are confirmation dialogs a good use of front-end tools, and what do they not replace?