Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

thinking-content.md

151 lines6.2 KB

1---
2title: Thinking & Reasoning
3id: thinking-content
4order: 5
5description: "Render reasoning tokens from thinking models (Claude extended thinking, OpenAI o-series) as streamed ThinkingPart in TanStack AI chat UIs."
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​
17Some models expose their internal reasoning as "thinking" content -- Claude with extended thinking, OpenAI o-series models with reasoning, and others. TanStack AI captures this as `ThinkingPart` in messages, streamed to your UI in real-time alongside text and tool calls.
18​
19Unsigned thinking stays in the UI. Signed thinking is a `ThinkingPart` with a `signature`. Anthropic extended thinking uses this. The next request sends signed thinking back in the same order as the original response, including around provider-executed tools. The next-turn body puts that signature on spec `encryptedValue` on the `role: "reasoning"` fan-out. Stream events use `REASONING_ENCRYPTED_VALUE`.
20​
21## How It Works
22​
23Read the `ThinkingPart` in `message.parts`. Thinking content comes from `REASONING_*` events and from `REASONING_ENCRYPTED_VALUE`. `STEP_STARTED` and `STEP_FINISHED` only carry `stepName`.
24​
25```typescript
26interface ThinkingPart {
27 type: "thinking";
28 content: string;
29 stepId?: string;
30 signature?: string;
31 redacted?: boolean;
33```
34​
35The `ThinkingPart` appears in `UIMessage.parts` alongside `TextPart` and `ToolCallPart` entries. As reasoning tokens arrive, its `content` accumulates token by token.
36​
37Claude can also send a redacted thinking block. It is encrypted, so it has no text. It arrives as a `ThinkingPart` with `redacted: true`, an empty `content`, and the encrypted data in `signature`. Keep the part in your stored messages: the next request sends it back to Claude unchanged. In a UI, show a short placeholder such as "Thinking hidden" instead of the empty text.
38​
39## Turn thinking on
40​
41Set `reasoning` on `chat()`. It works the same on every provider:
42​
43```typescript
44import {
45 chat,
46 chatParamsFromRequest,
47 toServerSentEventsResponse,
48} from "@tanstack/ai";
49import { anthropicText } from "@tanstack/ai-anthropic";
50​
51export async function POST(request: Request) {
52 const { messages, threadId, runId } = await chatParamsFromRequest(request);
53 const stream = chat({
54 adapter: anthropicText("claude-sonnet-5-5"),
55 messages,
56 threadId,
57 runId,
58 reasoning: "high",
59 });
60 return toServerSentEventsResponse(stream);
62```
63​
64The thinking text streams back as `ThinkingPart`s. Some models think without sending text back. With those, the part stays empty.
65​
66Want the model to think, but keep the text out of your UI? Set `summary: false`:
67​
68```typescript
69import { chat } from "@tanstack/ai";
70import { openaiText } from "@tanstack/ai-openai";
71​
72const stream = chat({
73 adapter: openaiText("gpt-6.1-sol"),
74 messages: [{ role: "user", content: "Plan a database migration." }],
75 reasoning: { level: "high", summary: false },
76});
77```
78​
79Then no thinking text streams. See [Reasoning](./reasoning) for the levels and token budgets.
80​
81## Rendering in React
82​
83Thinking parts appear in `message.parts` just like text and tool calls. A common pattern is to render them in a collapsible element so they don't dominate the UI:
84​
85```tsx
86import type { UIMessage } from "@tanstack/ai-react";
87​
88function MessageContent({ message }: { message: UIMessage }) {
89 return (
90 <div>
91 {message.parts.map((part, idx) => {
92 if (part.type === "thinking") {
93 return (
94 <details key={idx}>
95 <summary>Thinking...</summary>
96 <pre style={{ whiteSpace: "pre-wrap" }}>{part.content}</pre>
97 </details>
98 );
99 }
100 if (part.type === "text") {
101 return <p key={idx}>{part.content}</p>;
102 }
103 return null;
104 })}
105 </div>
106 );
108```
109​
110## Streaming Behavior
111​
112Thinking content streams **before** the final text response. As reasoning tokens arrive, `ThinkingPart.content` accumulates token by token, the same way `TextPart.content` does for the response text.
113​
114The typical streaming order is:
115​
1161. Reasoning starts (`REASONING_START` / `REASONING_MESSAGE_START`). Encrypted blobs use `REASONING_ENCRYPTED_VALUE`.
1172. Reasoning tokens stream in (`REASONING_MESSAGE_CONTENT`) and accumulate into `ThinkingPart.content`.
1183. `TEXT_MESSAGE_START` starts the visible response.
1194. `TEXT_MESSAGE_CONTENT` streams the response text.
120​
121`STEP_STARTED` and `STEP_FINISHED` only carry `stepName`. They do not carry thinking text.
122​
123If you use `useChat` from `@tanstack/ai-react` (or the Solid/Vue/Svelte equivalents), your `messages` array updates with both thinking and text parts as they arrive.
124​
125## Thinking and tool calls in order
126​
127Claude can think, call a tool, think again, then call another tool. You want your UI to show that order, not all the thinking in one lump at the top.
128​
129You get it by default. `message.parts` keeps the order the model used. For that answer, the part types are:
130​
131```text
132thinking, tool-call, thinking, tool-call
133```
134​
135What this means for your UI:
136​
137- Each thinking block is its own `ThinkingPart`. Each one gets its own `REASONING_MESSAGE_END` and `REASONING_END`.
138- Text after a second thinking block starts a new text part. You get "A" and "B" as two parts, not "AB".
139- The render loop from [Rendering in React](#rendering-in-react) already walks the parts in order. You do not need extra code.
140​
141The order also goes back to the model. The stored message keeps a `blockOrder` map, and the next request sends the blocks in the same order. Claude needs this, because it signs each thinking block against the blocks before it.
142​
143Gemini, 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).
144​
145## Next Steps
146​
147- [Reasoning](./reasoning) -- Levels, budgets, and the levels of each model
148- [Streaming](./streaming) -- Connection adapters and stream events
149- [Agentic Cycle](./agentic-cycle) -- How thinking interacts with tool-calling loops
150- [Anthropic Adapter](../adapters/anthropic) -- Full Anthropic provider options
151- [OpenAI Adapter](../adapters/openai) -- Full OpenAI provider options