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

reasoning.md

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