---
url: /development/ag-ui-streaming.md
---

# AG-UI chat streaming

One endpoint: `POST /v1/chat/v2/completions` (behind the gateway: `/api/backend/v1/chat/v2/completions`). Accepted requests return an SSE stream of AG-UI events; requests rejected before streaming starts return a plain HTTP error (400/403/404/409/422/500) without a `RUN_ERROR` event.

![AG-UI chat streaming](./architecture/02-agui-streaming.png)

## Flow

1. The frontend (`page_helpers.makeApiRequest()` and `MucgptAgUiAgent.runAgent()` from `@ag-ui/client`) sends the run request through the API gateway.
2. `chat_router.ag_ui_chat_endpoint` authenticates the user, parses the native AG-UI `RunAgentInput` or the legacy MUCGPT body, validates messages and model, takes the Valkey run lock per conversation (409 if busy) and prepares persistence: ownership check, and only the newest message is sent when a checkpoint already exists.
3. `init_agent()` assembles model, tools (local + MCP), the file backend and the checkpointer.
4. `MUCGPTAgentExecutor.run_agui_with_streaming()` drives `graph.astream(...)` and maps updates to AG-UI events; Langfuse tracing wraps the run.
5. The `EventEncoder` streams the events as `text/event-stream`; the `@ag-ui/client` SSE parser expands `*_CHUNK` events into START/CONTENT/END, and the chat UI renders answer text, tool steps, canvas edits and token usage.

## Events sent by `agent_executor.py`

| Event                                  | Meaning                                                                                                                    |
| -------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `RUN_STARTED`                          | run begins                                                                                                                 |
| `TEXT_MESSAGE_CHUNK`                   | answer tokens                                                                                                              |
| `TOOL_CALL_CHUNK` / `TOOL_CALL_RESULT` | tool step; the live activity label is the tool's `status_message` argument                                                 |
| `CUSTOM` (canvas)                      | live canvas edits                                                                                                          |
| `RUN_FINISHED`                         | token usage · interrupt outcome when a tool waits for approval                                                             |
| `STATE_SNAPSHOT` · `MESSAGES_SNAPSHOT` | best-effort snapshots before an interrupted `RUN_FINISHED`; snapshot failures are logged, `RUN_FINISHED` can still be sent |
| `RUN_ERROR`                            | exception caught in the executor's graph-streaming block                                                                   |

The request-side contract (request shapes, version negotiation, the `forwardedProps.mucgpt` namespace, the vision gate) is documented in the [AG-UI chat contract](./ag-ui-chat.md); persistence behavior in [conversation persistence](./conversation-persistence.md).
