How Agents "Speak UI" to the Frontend: A Breakdown of the A2UI Protocol
This article explains the A2UI (Agent to UI) protocol, a general pattern that lets AI agents stream structured interface intent to the frontend instead of plain text. It covers why one-way HTTP streaming fits agent runtimes, the core event model, declarative UI descriptions, state synchronization, reliability handling for disconnects and out-of-order events, security governance, and a step-by-step path from a minimal loop to a production-ready integration.
How Agents “Speak UI” to the Frontend: A Breakdown of the A2UI Protocol
1. The Starting Problem: When Agent Output Is No Longer Plain Text
When you chat with an AI assistant in a browser, the answer usually “flows” out word by word. This experience is typically powered by one-way server push over HTTP. But once the scenario shifts from “question answering” to “task execution,” things change: the agent must not only produce natural language, but also call tools, show intermediate reasoning, maintain session state, and even insert tables, forms, progress cards, and clickable action buttons into the conversation flow.
At this point, the limits of a plain-text stream become clear: the frontend receives a string of characters but has no idea which part is a heading, which part is a tool-call result, and which part is an interactive control. As a result, the industry has begun exploring a new class of protocols—ones that let an agent not only “speak content” but also “speak UI.” A2UI (Agent to UI) is a representative example of this idea: it treats the interface itself as a data structure that can be streamed and progressively updated.
It is worth emphasizing that A2UI is not a private interface of any specific product, but an abstraction of a general technical paradigm: the agent outputs structured interface intent, and the frontend is responsible for safe rendering. Understanding this paradigm is more valuable than memorizing any specific field.
2. Why One-Way Streaming Instead of a Bidirectional Long Connection
The agent runtime has a distinct characteristic: the frontend issues a single request, and the backend keeps pushing events until the run finishes. This is a classic “one request, streamed response” pattern.
| Dimension | One-Way Streaming (HTTP-based) | Bidirectional Long Connection |
|---|---|---|
| Communication direction | Server → client | Bidirectional |
| Connection setup | Built on the existing HTTP stack, no upgrade handshake | Requires protocol upgrade |
| Reconnection | Usually supported natively by the client/browser | Must be implemented yourself |
| Infrastructure compatibility | Naturally compatible with proxies, CDNs, cross-origin policies | Poorly supported by some proxies and gateways |
| Connection termination | Simply close the response stream | Must send a close frame |
The core reason for choosing one-way streaming is straightforward: during an agent run, the frontend usually does not need to send data back to the server. The user message is submitted once in the request body, and during the run the frontend is only a passive receiver of events. This matches the one-way push model perfectly, while bringing lower integration cost and better infrastructure compatibility.
Of course, bidirectional capability is not unneeded—it is simply “downgraded” to a separate channel. For example, human takeover during a run or interrupting a task can go through a separate short request, rather than forcing the main event stream to carry bidirectional responsibility.
3. The A2UI Event Model: Splitting a Run into Renderable Fragments
The key design of A2UI-style protocols is to break a single agent run into several semantically clear event categories. A general event system typically includes:
- Lifecycle events: mark the start, end, and abnormal termination of a run, letting the frontend switch global state.
- Text message events: carry natural-language output and support incremental appending.
- UI description events: declare the component tree or interface fragments to render—the dividing line between A2UI and traditional text streams.
- Tool-call events: tell the frontend that a tool was called, with what parameters, and with what result.
- State sync events: push session-level or business-level state so the frontend keeps context consistent.
- Reasoning events: show intermediate thoughts or planning steps (whether to expose them can be chosen by product policy).
An illustrative event structure (expressing design intent only, not a specific interface):
{"event": "ui_patch","run_id": "r-7f3a","seq": 12,"payload": {"root": "card_1","nodes": [{ "id": "card_1", "type": "Card", "props": { "title": "Task Progress" } },{ "id": "bar_1", "type": "Progress", "props": { "value": 0.6 } }]}}
There are three engineering points here:
- Sequence numbers and idempotency: each event carries a monotonically increasing sequence number, so the frontend can deduplicate, sort, and detect whether any events are missing.
- Patch semantics: the UI arrives as “incremental patches,” avoiding the flicker and bandwidth waste of a full redraw every time.
- Root-node addressing: every patch points to a locatable root node, based on which the frontend performs a partial update.
4. Why UI Descriptions Must Be Declarative
Letting the agent directly output executable code or raw markup may seem flexible, but it is extremely risky: injection attacks, style pollution, and uncontrolled loading of external resources all turn “rendering” into “execution.”
A safer approach is declarative description: the agent only outputs “which components, at what hierarchy, with what properties,” while the frontend maintains a component whitelist and a property validation table. The frontend is the only party with the authority to decide “how to render.”
An example of a minimal viable component contract:
{"type": "Form","props": { "submitLabel": "Submit" },"children": [{ "type": "TextField", "props": { "label": "Name", "required": true } },{ "type": "Button", "props": { "action": "submit_form" } }]}
Recommended engineering constraints include:
- Component whitelist: only registered components are allowed; unknown types degrade to a text notice.
- Property schema validation: strict validation of types, value ranges, and lengths.
- Declarative actions: interactive elements like buttons only declare an “action identifier” and do not carry executable logic directly.
- Resource isolation: referencing arbitrary external addresses in the description is forbidden; necessary resources are hosted uniformly by the frontend.
5. State Synchronization: Keeping the UI Consistent with the Session
In agent scenarios, the interface is not a one-time artifact but a “living object” that evolves as the run progresses. State sync events serve three responsibilities:
- Overwriting state: session-level variables, the current plan, collected parameters.
- Incremental state: list appends, progress updates, field revisions.
- Rollback state: restoring to the last consistent snapshot when a step fails.
In practice, a “snapshot + patch” combination is recommended: send one snapshot at the start of the run, then send only patches afterward; also keep the last few snapshots to make it easy to realign quickly after a reconnection. On reconnection, the frontend carries the last received sequence number, and the server resends the missing events accordingly. This mechanism significantly improves availability in weak-network environments.
6. Reliability Design: Disconnects, Out-of-Order Events, and Failure Recovery
A streaming architecture must directly answer three questions:
- Disconnects: rely on automatic client reconnection, combined with sequence numbers to “continue from the breakpoint,” avoiding duplicate rendering.
- Out-of-order events: use the sequence number as the standard, discarding or caching stale events.
- Failures: the abnormal termination in lifecycle events must carry a readable reason and a retryable flag, based on which the frontend shows a degraded interface instead of a blank screen.
In addition, timeout control should be layered: connection idle timeout, single-step tool-call timeout, and overall run timeout, each configured independently, so that one slow tool does not drag down the entire session.
7. Security and Governance: Caging “What Can Be Rendered”
When the interface is generated by a model, the attack surface expands accordingly. Governance recommendations:
- Least-privilege component set: expose only the components and actions the business requires.
- Content sanitization: escape text fields; route rich text through a tag whitelist.
- Audit trail: write UI description events and tool-call events into a logging service for traceability.
- Observability: set up monitoring and alerts for event latency, loss rate, and rendering failure rate.
- Human fallback: high-risk actions require a second confirmation; the agent can only “suggest,” not “decide on behalf.”
8. Implementation Path: From a Minimal Loop to Engineering Maturity
A four-step approach is recommended:
- Get the text stream working: first implement lifecycle and text message events to validate the basic pipeline.
- Introduce a single component: support only one card component and get the “describe—validate—render” loop working end to end.
- Complete state and reconnection: add sequence numbers, snapshots, patches, and resumable transfer.
- Expand components and governance: gradually grow the whitelist while building audit and monitoring in parallel.
An illustrative frontend consumption logic:
const es = new EventSource("/agent/run?session=" + id);let lastSeq = -1;es.onmessage = (e) => {const evt = JSON.parse(e.data);if (evt.seq <= lastSeq) return; // idempotent deduplicationlastSeq = evt.seq;switch (evt.event) {case "ui_patch": renderPatch(evt.payload); break;case "state_sync": applyState(evt.payload); break;case "run_end": es.close(); break;}};
9. Conclusion
The essence of A2UI-style protocols is to turn the “interface” from an implicit backend artifact into an explicit contract between frontend and backend that is negotiable, verifiable, and recoverable. What it solves is not “whether something can be displayed,” but “whether it can be displayed safely, stably, and in an evolvable way.” For developers, three things truly need to be mastered: breaking down an agent run from an event-based perspective, constraining UI generation with declarative descriptions, and ensuring reliability with sequence numbers and snapshots. Master these three points, and regardless of how the underlying infrastructure is chosen, you can build an agent frontend integration with a smooth experience and clear boundaries.
