Open History to see the last commit
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;
32}
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## Enabling Thinking
40
41How you enable thinking depends on the provider.
42
43### Anthropic (Extended Thinking)
44
45Pass the `thinking` option in `modelOptions` with `type: "enabled"` and a `budget_tokens` (minimum 1024). Keep `budget_tokens` below `modelOptions.max_tokens` so there is room for the visible response in addition to the thinking budget:
46
47```typescript
48import { chat, toServerSentEventsResponse } from "@tanstack/ai";
49import { anthropicText } from "@tanstack/ai-anthropic";
50
51export async function POST(request: Request) {
52 const { messages } = await request.json();
53 const stream = chat({
54 adapter: anthropicText("claude-sonnet-4-6"),
55 messages,
56 modelOptions: {
57 max_tokens: 32000,
58 // budget_tokens must be at least 1024 and below max_tokens
59 thinking: { type: "enabled", budget_tokens: 10000 },
60 },
61 });
62 return toServerSentEventsResponse(stream);
63}
64```
65
66### OpenAI (Reasoning Models)
67
68OpenAI o-series models (o1, o3, o3-mini, o3-pro) perform reasoning automatically. You can control the depth with the `reasoning` option:
69
70```typescript
71import { chat, toServerSentEventsResponse } from "@tanstack/ai";
72import { openaiText } from "@tanstack/ai-openai";
73
74export async function POST(request: Request) {
75 const { messages } = await request.json();
76 const stream = chat({
77 adapter: openaiText("o3-mini"),
78 messages,
79 modelOptions: {
80 reasoning: {
81 effort: "medium", // 'none' | 'minimal' | 'low' | 'medium' | 'high'
82 summary: "auto", // 'auto' | 'detailed'
83 },
84 },
85 });
86 return toServerSentEventsResponse(stream);
87}
88```
89
90When `reasoning.summary` is set, the adapter streams reasoning summary text as thinking content. Without it, reasoning tokens are still used internally but may not be surfaced depending on the model.
91
92GPT-5 and later models also support reasoning. Their `reasoning.effort` accepts `"none" | "minimal" | "low" | "medium" | "high"`, and reasoning activates on any non-`none` value:
93
94```typescript
95import { chat, toServerSentEventsResponse } from "@tanstack/ai";
96import { openaiText } from "@tanstack/ai-openai";
97
98export async function POST(request: Request) {
99 const { messages } = await request.json();
100 const stream = chat({
101 adapter: openaiText("gpt-5.5"),
102 messages,
103 modelOptions: {
104 reasoning: { effort: "high" },
105 },
106 });
107 return toServerSentEventsResponse(stream);
108}
109```
110
111## Rendering in React
112
113Thinking 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:
114
115```tsx
116import type { UIMessage } from "@tanstack/ai-react";
117
118function MessageContent({ message }: { message: UIMessage }) {
119 return (
120 <div>
121 {message.parts.map((part, idx) => {
122 if (part.type === "thinking") {
123 return (
124 <details key={idx}>
125 <summary>Thinking...</summary>
126 <pre style={{ whiteSpace: "pre-wrap" }}>{part.content}</pre>
127 </details>
128 );
129 }
130 if (part.type === "text") {
131 return <p key={idx}>{part.content}</p>;
132 }
133 return null;
134 })}
135 </div>
136 );
137}
138```
139
140## Streaming Behavior
141
142Thinking 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.
143
144The typical streaming order is:
145
1461. Reasoning starts (`REASONING_START` / `REASONING_MESSAGE_START`). Encrypted blobs use `REASONING_ENCRYPTED_VALUE`.
1472. Reasoning tokens stream in (`REASONING_MESSAGE_CONTENT`) and accumulate into `ThinkingPart.content`.
1483. `TEXT_MESSAGE_START` starts the visible response.
1494. `TEXT_MESSAGE_CONTENT` streams the response text.
150
151`STEP_STARTED` and `STEP_FINISHED` only carry `stepName`. They do not carry thinking text.
152
153If 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.
154
155## Next Steps
156
157- [Streaming](./streaming) -- Connection adapters and stream events
158- [Agentic Cycle](./agentic-cycle) -- How thinking interacts with tool-calling loops
159- [Anthropic Adapter](../adapters/anthropic) -- Full Anthropic provider options
160- [OpenAI Adapter](../adapters/openai) -- Full OpenAI provider options