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

reasoning.md

183 lines5.5 KB

1---
2title: Reasoning
3id: reasoning
4order: 6
5description: "Make a reasoning model think harder, or not at all, with one reasoning option on chat(). It works the same on every provider and is typed per model."
6keywords:
7 - tanstack ai
8 - reasoning
9 - thinking
10 - reasoning effort
11 - extended thinking
12 - thinking budget
13---
14​
15Pass `reasoning` to `chat()`. The adapter maps that level onto the provider setting, such as OpenAI `reasoning.effort` or Anthropic `thinking`.
16​
17Set `reasoning` on `chat()`. The adapter sends the right field for the model.
18​
19## Quick setup
20​
21### 1. Set a level on the server
22​
23```typescript
24// api/chat.ts
25import {
26 chat,
27 chatParamsFromRequest,
28 toServerSentEventsResponse,
29} from "@tanstack/ai";
30import { openaiText } from "@tanstack/ai-openai";
31​
32export async function POST(request: Request) {
33 const { messages, threadId, runId } = await chatParamsFromRequest(request);
34​
35 const stream = chat({
36 adapter: openaiText("gpt-6.1-sol"),
37 messages,
38 threadId,
39 runId,
40 reasoning: "high",
41 });
42​
43 return toServerSentEventsResponse(stream);
45```
46​
47### 2. Show the thinking on the client
48​
49The thinking text arrives as `thinking` parts in `message.parts`, next to the text parts:
50​
51```tsx
52import { useChat, fetchServerSentEvents } from "@tanstack/ai-react";
53​
54export function Chat() {
55 const { messages } = useChat({
56 connection: fetchServerSentEvents("/api/chat"),
57 threadId: "support-chat",
58 });
59​
60 return messages.map((message) => (
61 <div key={message.id}>
62 {message.parts.map((part, index) => {
63 if (part.type === "thinking") {
64 return (
65 <details key={index}>
66 <summary>Thinking</summary>
67 <pre>{part.content}</pre>
68 </details>
69 );
70 }
71 return part.type === "text" ? <p key={index}>{part.content}</p> : null;
72 })}
73 </div>
74 ));
76```
77​
78Send a hard question. A "Thinking" box fills first, then the answer streams below it.
79​
80Some models think without sending any text back. For those models, the thinking part stays empty. See [Thinking & Reasoning](./thinking-content) for the order of the parts and for signed thinking.
81​
82## How it works
83​
84The levels go from least to most thinking:
85​
86`off`, `minimal`, `low`, `medium`, `high`, `xhigh`, `max`
87​
88If you leave `reasoning` out, the provider default applies.
89​
90### Your editor knows the levels of the model
91​
92Each model has only some of the levels. The types accept only the levels of the model you picked:
93​
94```typescript
95import { chat } from "@tanstack/ai";
96import { anthropicText } from "@tanstack/ai-anthropic";
97​
98chat({
99 adapter: anthropicText("claude-opus-5-5"),
100 messages: [{ role: "user", content: "Hello" }],
101 threadId: "thread-1",
102 reasoning: "max",
103});
104​
105chat({
106 adapter: anthropicText("claude-haiku-4-5"),
107 messages: [{ role: "user", content: "Hello" }],
108 threadId: "thread-1",
109 // @ts-expect-error claude-haiku-4-5 has no max level
110 reasoning: "max",
111});
112```
113​
114A model that does not reason takes no `reasoning` at all. Each adapter page lists the levels of its models.
115​
116### A level that the model does not have
117​
118A level can come from a settings menu or from middleware, so the types cannot always catch it. At run time, the adapter moves the level to the nearest level that the model has. It looks up first, then down.
119​
120For example, `minimal` on `claude-opus-5-5` goes out as `low`. `max` on `gemini-3.8-flash` goes out as `high`.
121​
122## Advanced
123​
124### Hide the thinking or set a budget
125​
126Pass an object for more control:
127​
128- `level`: the level.
129- `summary`: `false` keeps the thinking text out of the stream. The default is `true`.
130- `budgetTokens`: an exact thinking budget. Only budget models take it, for example Claude Haiku 4.5 and Gemini 2.5 Pro.
131​
132```typescript
133import { chat } from "@tanstack/ai";
134import { geminiText } from "@tanstack/ai-gemini";
135​
136const stream = chat({
137 adapter: geminiText("gemini-2.5-pro"),
138 messages: [{ role: "user", content: "Check this proof." }],
139 threadId: "thread-1",
140 reasoning: { level: "high", budgetTokens: 8000 },
141});
142```
143​
144If you do not set `budgetTokens`, a budget model gets a budget from the level:
145​
146| Level | Shared default budget |
147| --- | --- |
148| `minimal` | 1024 tokens |
149| `low` | 2048 tokens |
150| `medium` | 8192 tokens |
151| `high`, `xhigh`, `max` | 16384 tokens |
152​
153Gemini 2.5 uses its own budgets. For example, `high` is 32768 tokens on `gemini-2.5-pro` and 24576 tokens on `gemini-2.5-flash`.
154​
155### Change the level in middleware
156​
157Middleware can set the level for one model call. For example, think less when the run retries:
158​
159```typescript
160import type { ChatMiddleware } from "@tanstack/ai";
161​
162const lowEffort: ChatMiddleware = {
163 name: "low-effort",
164 onConfig: () => ({ reasoning: { level: "low", summary: true } }),
165};
166```
167​
168### What each provider gets
169​
170| Adapter | Sent as |
171| --- | --- |
172| OpenAI, Azure OpenAI, Grok, Bedrock Responses, Neon Responses | `reasoning: { effort, summary }` |
173| OpenAI Chat Completions, Groq, LLM Gateway, Lovable, Bedrock chat, Cloudflare, Neon Chat Completions | `reasoning_effort` |
174| Anthropic, Neon Claude | adaptive thinking with the effort, or a thinking budget |
175| Gemini | `thinkingConfig.thinkingLevel` or `thinkingConfig.thinkingBudget` |
176| OpenRouter, Vercel AI Gateway | the gateway's `reasoning` object |
177| Mistral | `reasoning_effort` or `prompt_mode` |
178| BytePlus | `thinking.type` and `reasoning_effort` |
179| Ollama | `think` |
180| Bedrock Converse | Claude thinking fields |
181| Claude Code, Codex, OpenCode | the CLI or server effort setting |
182​
183On 0.x with reasoning fields in `modelOptions`? See [Moving reasoning into chat()](../migration/reasoning-option).