Open History to see the last commit
1---
2title: Thinking & Reasoning
3id: thinking-content
4order: 5
5description: "Show the thinking of a reasoning model in your chat UI. The thinking streams as ThinkingPart entries in message.parts, in the order the model used."
6keywords:
7 - tanstack ai
8 - thinking
9 - reasoning
10 - extended thinking
11 - claude thinking
12 - o-series
13 - chain of thought
14 - ThinkingPart
15---
16
17A thinking part holds the text a reasoning model writes before the answer. Render it in order with the tool calls.
18
19TanStack AI streams the thinking as `ThinkingPart` entries in `message.parts`, next to the text and the tool calls.
20
21## Quick setup
22
23### 1. Turn thinking on in the server route
24
25Set `reasoning` on `chat()`. It works the same on every provider:
26
27```typescript
28// api/chat.ts
29import {
30 chat,
31 chatParamsFromRequest,
32 toServerSentEventsResponse,
33} from "@tanstack/ai";
34import { anthropicText } from "@tanstack/ai-anthropic";
35
36export async function POST(request: Request) {
37 const { messages, threadId, runId } = await chatParamsFromRequest(request);
38
39 const stream = chat({
40 adapter: anthropicText("claude-sonnet-5-5"),
41 messages,
42 threadId,
43 runId,
44 reasoning: "high",
45 });
46
47 return toServerSentEventsResponse(stream);
48}
49```
50
51### 2. Render the thinking parts on the client
52
53Put each thinking part in a box that the user can open:
54
55```tsx
56import { useChat, fetchServerSentEvents } from "@tanstack/ai-react";
57
58export function Chat() {
59 const { messages } = useChat({
60 connection: fetchServerSentEvents("/api/chat"),
61 threadId: "support-chat",
62 });
63
64 return messages.map((message) => (
65 <div key={message.id}>
66 {message.parts.map((part, index) => {
67 if (part.type === "thinking") {
68 return (
69 <details key={index}>
70 <summary>Thinking...</summary>
71 <pre style={{ whiteSpace: "pre-wrap" }}>{part.content}</pre>
72 </details>
73 );
74 }
75 if (part.type === "text") {
76 return <p key={index}>{part.content}</p>;
77 }
78 return null;
79 })}
80 </div>
81 ));
82}
83```
84
85Ask a hard question. The thinking box fills first, then the answer streams below it.
86
87Some models think without sending text back. For those models, the thinking part stays empty.
88
89## How it works
90
91The thinking streams before the answer. Its `content` grows token by token, the same as a text part.
92
93The usual order of events:
94
951. `REASONING_START` and `REASONING_MESSAGE_START` start the thinking.
962. `REASONING_MESSAGE_CONTENT` events add tokens to `ThinkingPart.content`.
973. `TEXT_MESSAGE_START` starts the visible answer.
984. `TEXT_MESSAGE_CONTENT` events stream the answer text.
99
100`useChat` in every framework updates `messages` with both thinking parts and text parts while they arrive.
101
102To see levels and token budgets, read [Reasoning](./reasoning).
103
104## Keep the thinking out of the UI
105
106If you want the model to think but do not want to show the text, set `summary: false`:
107
108```typescript
109import { chat } from "@tanstack/ai";
110import { openaiText } from "@tanstack/ai-openai";
111
112const stream = chat({
113 adapter: openaiText("gpt-6.1-sol"),
114 messages: [{ role: "user", content: "Plan a database migration." }],
115 threadId: "thread-1",
116 reasoning: { level: "high", summary: false },
117});
118```
119
120Then no thinking text streams.
121
122## Thinking and tool calls in order
123
124Claude can think, call a tool, think again, then call another tool. Your UI must show that order, not all the thinking in one block at the top.
125
126You get this order by default. `message.parts` keeps the order that the model used. For that answer, the part types are:
127
128```text
129thinking, tool-call, thinking, tool-call
130```
131
132What this means for your UI:
133
134- Each thinking block is its own `ThinkingPart`. Each one gets its own `REASONING_MESSAGE_END` and `REASONING_END`.
135- Text after a second thinking block starts a new text part. You get "A" and "B" as two parts, not "AB".
136- The render loop in the [Quick setup](#2-render-the-thinking-parts-on-the-client) already walks the parts in order. You do not need extra code.
137
138## Advanced
139
140### The `ThinkingPart` type
141
142This is the type in `@tanstack/ai`, which the server and stored messages use. The thinking part that `useChat` returns has only `type` and `content`.
143
144```typescript
145interface ThinkingPart {
146 type: "thinking";
147 content: string;
148 stepId?: string;
149 signature?: string;
150 redacted?: boolean;
151}
152```
153
154The thinking content comes from the `REASONING_*` events and from `REASONING_ENCRYPTED_VALUE`. `STEP_STARTED` and `STEP_FINISHED` carry only `stepName`. They do not carry thinking text.
155
156### Signed thinking
157
158Thinking without a signature stays in the UI. Signed thinking is a `ThinkingPart` with a `signature`. Anthropic extended thinking uses it.
159
160- The next request sends signed thinking back in the same order as the original response. This includes the order around provider-executed tools.
161- In the next request body, the signature goes on the spec field `encryptedValue` of the `role: "reasoning"` message.
162- In the stream, the signature arrives as a `REASONING_ENCRYPTED_VALUE` event.
163
164### Redacted thinking
165
166Claude can send a redacted thinking block. It is encrypted, so it has no text. It arrives as a `ThinkingPart` with:
167
168- `redacted: true`
169- an empty `content`
170- the encrypted data in `signature`
171
172Keep the part in your stored messages. The next request sends it back to Claude unchanged. In a UI, the part from `useChat` has an empty `content`. Show a short label such as "Thinking hidden" in place of the empty text.
173
174### The block order goes back to the model
175
176The stored message keeps a `blockOrder` map. The next request sends the blocks in the same order. Claude needs this, because it signs each thinking block against the blocks before it.
177
178Gemini, Mistral, and Bedrock Converse also send thinking back on the next request, for history from the same model. History from another model goes in as plain text. See [Keep saved history when you switch](../advanced/runtime-adapter-switching#keep-saved-history-when-you-switch).
179
180### Provider options
181
182For all provider options, see the [Anthropic adapter](../adapters/anthropic) and the [OpenAI adapter](../adapters/openai). To see how thinking fits into a tool loop, read [Agentic Cycle](./agentic-cycle).