Open History to see the last commit
1---
2title: Event metadata
3id: protocol-event-metadata
4order: 1
5description: "What a custom AG-UI server must put in metadata.tanstack so TanStack ChatClient and useChat get finishReason, model, and leftover usage."
6keywords:
7 - tanstack ai
8 - ag-ui protocol
9 - metadata.tanstack
10 - finishReason
11 - custom server
12 - SSE
13---
14
15Your server speaks AG-UI. TanStack `useChat` still misses `finishReason`, the model id, or leftover usage. Put those extras in `metadata.tanstack` on the event. Spec fields stay at the top.
16
17Copy this `RUN_FINISHED` event first:
18
19```json
20{
21 "type": "RUN_FINISHED",
22 "threadId": "thread-1",
23 "runId": "run-1",
24 "usage": [
25 {
26 "inputTokens": 12,
27 "outputTokens": 34,
28 "totalTokens": 46
29 }
30 ],
31 "metadata": {
32 "tanstack": {
33 "finishReason": "stop",
34 "model": "gpt-5.5"
35 }
36 }
37}
38```
39
40The client copies `metadata.tanstack` onto the chunk. After `chunk.type === "RUN_FINISHED"`, read `chunk.metadata?.tanstack?.finishReason`. In-process `usage` is TanStack `TokenUsage` (`promptTokens`). The wire uses the spec array (`inputTokens`).
41
42## Do now
43
44Send these on every successful run:
45
46| Field | Where | Why the client needs it |
47| --- | --- | --- |
48| `type`, `threadId`, `runId` | Spec top level | Frame the run. Missing ids break resume and correlation. |
49| `usage[]` | Spec top level on `RUN_FINISHED` / `RUN_ERROR` | Token counts. Use `inputTokens`, `outputTokens`, `totalTokens`. |
50| `finishReason` | `metadata.tanstack` on `RUN_FINISHED` | After a tool result, the client continues only when this is not `"stop"`. |
51| `model` | `metadata.tanstack` | SSE `[DONE]` fallback and generation reconstruct. |
52
53`finishReason` is one of `"stop"`, `"length"`, `"content_filter"`, `"tool_calls"`, or `null`.
54
55If you omit `finishReason` after a tool result, the client treats it as not `"stop"` and can send another turn.
56
57## Read it on the client
58
59```typescript
60import { useChat, fetchServerSentEvents } from "@tanstack/ai-react";
61
62const { messages } = useChat({
63 connection: fetchServerSentEvents("/api/chat"),
64 onChunk: (chunk) => {
65 if (chunk.type === "RUN_FINISHED") {
66 console.log(chunk.usage);
67 console.log(chunk.metadata?.tanstack?.finishReason);
68 console.log(chunk.metadata?.tanstack?.model);
69 }
70 },
71});
72```
73
74Do not import a helper. Check `chunk.type`, then read `chunk.metadata?.tanstack`.
75
76## `RUN_ERROR`
77
78AG-UI `RUN_ERROR` has `message` and optional `code` at the top. Put correlation ids in `metadata.tanstack`:
79
80```json
81{
82 "type": "RUN_ERROR",
83 "message": "Provider timeout",
84 "code": "TIMEOUT",
85 "metadata": {
86 "tanstack": {
87 "threadId": "thread-1",
88 "runId": "run-1",
89 "model": "gpt-5.5"
90 }
91 }
92}
93```
94
95## Later
96
97Add these when you use the matching feature:
98
99- **Leftover usage.** Spec `usage[]` also accepts `cachedInputTokens` and `reasoningTokens`. Put `cost` and other leftover fields in `metadata.tanstack.usage`. The client rebuilds TanStack `TokenUsage` (`promptTokens`) from the array plus that leftover.
100- **Interrupt errors.** On `RUN_ERROR`, set `metadata.tanstack.interruptErrors` so `ChatClient` can match a failed interrupt submit.
101- **Tool output error.** On `TOOL_CALL_RESULT`, set `metadata.tanstack.state` to `"output-error"` when the tool result is an error payload.
102- **Message stamps.** On wire messages, `metadata.tanstack.createdAt` is an ISO-8601 string.
103- **Signatures.** Stream thinking signatures on `REASONING_ENCRYPTED_VALUE`. On the next-turn body, set spec `encryptedValue` on `role: "reasoning"` messages and on `toolCalls`. Other tool-call provider fields stay in `metadata.tanstack.toolCallMetadata`. See [Thinking & Reasoning](../chat/thinking-content).
104
105A TanStack `chat()` server already writes this bag. Use this page when you emit AG-UI events yourself.
106
107See [Stream Events](../chat/stream-events) for the event table, and [AG-UI Client Compliance](../migration/ag-ui-compliance) for the request body.