Open History to see the last commit
1---
2title: Anthropic
3id: anthropic-adapter
4order: 2
5description: "Use Anthropic Claude models in TanStack AI with @tanstack/ai-anthropic: Claude Opus, Sonnet, Haiku, and Fable for chat, thinking, prompt caching, and tools."
6keywords:
7 - tanstack ai
8 - anthropic
9 - claude
10 - claude fable 5
11 - claude sonnet 5
12 - claude opus
13 - adapter
14 - llm
15---
16
17The Anthropic adapter calls Claude for chat. It supports thinking, automatic prompt caching, and tools that Anthropic runs.
18
19## 1. Install
20
21<!-- ::start:tabs variant="package-manager" mode="install" -->
22
23react: @tanstack/ai-anthropic
24vue: @tanstack/ai-anthropic
25solid: @tanstack/ai-anthropic
26svelte: @tanstack/ai-anthropic
27preact: @tanstack/ai-anthropic
28angular: @tanstack/ai-anthropic
29vanilla: @tanstack/ai-anthropic
30octane: @tanstack/ai-anthropic
31
32<!-- ::end:tabs -->
33
34## 2. Set the API key
35
36Put the key in the environment of your server:
37
38```bash
39ANTHROPIC_API_KEY=sk-ant-...
40```
41
42`anthropicText`, `anthropicSummarize`, and `anthropicFiles` use the first of these variables that is set:
43
441. `ANTHROPIC_AUTH_TOKEN`: sent as a Bearer token. An `sk-ant-oat` token here turns on OAuth mode.
452. `ANTHROPIC_OAUTH_TOKEN`: sent as a Bearer token, in OAuth mode.
463. `ANTHROPIC_API_KEY`: sent as `x-api-key`.
47
48If none of them is set, these factories throw. To pass the credential in code, use `createAnthropicChat`. The `create*` factories never read a credential from the environment:
49
50```typescript
51import { createAnthropicChat } from "@tanstack/ai-anthropic";
52
53const adapter = createAnthropicChat("claude-sonnet-5-5", process.env.ANTHROPIC_API_KEY!);
54```
55
56Got a Bearer or OAuth token? See [Bearer and OAuth tokens](./anthropic-advanced#bearer-and-oauth-tokens).
57
58## 3. Stream a chat from the server
59
60```typescript
61import {
62 chat,
63 chatParamsFromRequest,
64 toServerSentEventsResponse,
65} from "@tanstack/ai";
66import { anthropicText } from "@tanstack/ai-anthropic";
67
68export async function POST(request: Request) {
69 const { messages, threadId, runId } = await chatParamsFromRequest(request);
70
71 const stream = chat({
72 adapter: anthropicText("claude-sonnet-5-5"),
73 messages,
74 threadId,
75 runId,
76 });
77
78 return toServerSentEventsResponse(stream);
79}
80```
81
82## 4. Render the chat
83
84The client is the same for every provider. Point `useChat` at your route:
85
86```tsx
87import { useState } from "react";
88import { useChat, fetchServerSentEvents } from "@tanstack/ai-react";
89
90export function Chat() {
91 const [input, setInput] = useState("");
92 const { messages, sendMessage } = useChat({
93 connection: fetchServerSentEvents("/api/chat"),
94 threadId: "support-chat",
95 });
96
97 return (
98 <form
99 onSubmit={(event) => {
100 event.preventDefault();
101 sendMessage(input);
102 setInput("");
103 }}
104 >
105 {messages.map((message) => (
106 <p key={message.id}>
107 {message.parts
108 .map((part) => (part.type === "text" ? part.content : ""))
109 .join("")}
110 </p>
111 ))}
112 <input value={input} onChange={(event) => setInput(event.target.value)} />
113 </form>
114 );
115}
116```
117
118For Vue, Solid, Svelte, Angular, and the other frameworks, see [Quick Start](../getting-started/quick-start#3-render-the-chat).
119
120You now have a streaming Claude chat. [Tools](../tools/tools) and [Structured Outputs](../structured-outputs/overview) work the same as with every adapter.
121
122## Summarization
123
124```typescript
125import { summarize } from "@tanstack/ai";
126import { anthropicSummarize } from "@tanstack/ai-anthropic";
127
128const result = await summarize({
129 adapter: anthropicSummarize("claude-sonnet-4-6"),
130 text: "Your long text to summarize...",
131 threadId: "thread-1",
132 maxLength: 100,
133 style: "concise", // "concise" | "bullet-points" | "paragraph"
134});
135
136console.log(result.summary);
137```
138
139## Files
140
141`anthropicFiles` uploads media to the Anthropic Files API, so a message can point to the file. See [Files API](../advanced/files-api).
142
143## No image generation
144
145Anthropic does not make images. For images, use [OpenAI](./openai) or [Gemini](./gemini).
146
147## Provider tools
148
149Anthropic runs some tools itself, such as web search and a code sandbox. Import them from `@tanstack/ai-anthropic/tools` and add them to `tools`:
150
151```typescript
152import { chat } from "@tanstack/ai";
153import { anthropicText } from "@tanstack/ai-anthropic";
154import { webSearchTool } from "@tanstack/ai-anthropic/tools";
155
156const stream = chat({
157 adapter: anthropicText("claude-sonnet-5-5"),
158 messages: [{ role: "user", content: "What's new in AI this week?" }],
159 threadId: "thread-1",
160 tools: [
161 webSearchTool({
162 name: "web_search",
163 type: "web_search_20250305",
164 max_uses: 2,
165 }),
166 ],
167});
168```
169
170Tools that Anthropic runs:
171
172- `webSearchTool`: search the web, with citations.
173- `webFetchTool`: read one URL.
174- `codeExecutionTool`: run code in a sandbox, also with hosted skills.
175- `memoryTool`: keep facts across turns.
176
177Tools that your app runs when Claude asks:
178
179- `bashTool`: run shell commands.
180- `textEditorTool`: view and change files.
181- `computerUseTool`: control a virtual desktop.
182- `customTool`: a tool with an inline schema.
183
184Each tool, with a full sample, is on [Anthropic: Advanced](./anthropic-advanced#provider-tools). For which models take which tool, see [Provider Tools](../tools/provider-tools.md).
185
186## Models and options
187
188### Model options
189
190Provider options go in `modelOptions`, with the Anthropic names:
191
192```typescript
193import { chat } from "@tanstack/ai";
194import { anthropicText } from "@tanstack/ai-anthropic";
195
196const stream = chat({
197 adapter: anthropicText("claude-sonnet-5-5"),
198 messages: [{ role: "user", content: "Hello!" }],
199 threadId: "thread-1",
200 modelOptions: {
201 max_tokens: 4096,
202 stop_sequences: ["END"],
203 },
204});
205```
206
207- The sampling options `temperature`, `top_p`, and `top_k` also go in `modelOptions`. Claude Sonnet 5, Claude Opus 4.7 and later, and the 5-generation models reject them. The types take them only on the models that accept them, for example `claude-sonnet-4-6`.
208- When you do not set `max_tokens`, the adapter sends the full output limit of the model. See [The `max_tokens` default](./anthropic-advanced#max_tokens-default).
209- Sampling options do not go at the root of `chat()`. See [Moving Sampling Options into modelOptions](../migration/sampling-options-to-model-options).
210
211### Reasoning
212
213Want Claude to think harder, or not at all? Set `reasoning` on `chat()`. The adapter turns the level into the right thinking fields for the model.
214
215```typescript
216import { chat } from "@tanstack/ai";
217import { anthropicText } from "@tanstack/ai-anthropic";
218
219const stream = chat({
220 adapter: anthropicText("claude-opus-5-5"),
221 messages: [{ role: "user", content: "Plan a database migration." }],
222 threadId: "thread-1",
223 reasoning: "xhigh",
224});
225```
226
227What goes to Anthropic:
228
229- `off` sends `thinking: { type: "disabled" }` on the models with `off` in the table below. On a model without `off`, such as `claude-fable-5`, the types reject it. At run time, it moves to the lowest level that the model has.
230- Claude 4.6 and later get adaptive thinking and `output_config.effort`. With an `outputSchema`, the adapter adds `output_config.format` next to the effort.
231- Budget models get `thinking: { type: "enabled", budget_tokens }`. The level picks the budget, or you set it with `budgetTokens`. If `max_tokens` is below the budget, the adapter raises it.
232- `summary: false` sends `display: "omitted"`, so the thinking text does not stream.
233
234The levels of each model:
235
236| Model | Levels | `budgetTokens` |
237| --- | --- | --- |
238| `claude-opus-5-5`, `claude-sonnet-5-5`, `claude-fable-5-1`, `claude-fable-5`, `claude-opus-5`, `claude-opus-4-8`, `claude-opus-4-7` | `low` to `max` | no |
239| `claude-haiku-5-5`, `claude-sonnet-5` | `off`, `low` to `max` | no |
240| `claude-opus-5-fast` | `off`, `low` to `max` | yes |
241| `claude-opus-4-6`, `claude-sonnet-4-6` | `low`, `medium`, `high`, `max` | yes |
242| `claude-opus-4-5` | `low`, `medium`, `high` | yes |
243| `claude-haiku-4-5`, `claude-sonnet-4-5`, `claude-opus-4-1` | `off` to `high` | yes |
244
245A budget model with `budgetTokens`:
246
247```typescript
248import { chat } from "@tanstack/ai";
249import { anthropicText } from "@tanstack/ai-anthropic";
250
251const stream = chat({
252 adapter: anthropicText("claude-haiku-4-5"),
253 messages: [{ role: "user", content: "Check this proof." }],
254 threadId: "thread-1",
255 reasoning: { level: "high", budgetTokens: 8000 },
256});
257```
258
259Structured output forces a tool, and Claude cannot think while a tool is forced. So the structured output call sends no thinking. See [Reasoning](../chat/reasoning) for the levels and the nearest-level rule.
260
261### Models
262
263Newest first. The package exports the full list as `ANTHROPIC_MODELS`.
264
265| Family | Models |
266| --- | --- |
267| Claude 5.5 | `claude-opus-5-5`, `claude-sonnet-5-5`, `claude-haiku-5-5` |
268| Claude 5 | `claude-fable-5-1`, `claude-fable-5`, `claude-opus-5`, `claude-opus-5-fast`, `claude-sonnet-5` |
269| Claude 4 | `claude-opus-4-8`, `claude-opus-4-7`, `claude-opus-4-6`, `claude-sonnet-4-6`, `claude-opus-4-5`, `claude-sonnet-4-5`, `claude-haiku-4-5`, `claude-opus-4-1` |
270
271`adapter.inputModalities` lists the input kinds of the selected model. See [Check what a model accepts](../advanced/extend-adapter#check-what-a-model-accepts).
272
273## Advanced
274
275These topics are on [Anthropic: Advanced](./anthropic-advanced):
276
277- Bearer and OAuth tokens, gateways, and your own Anthropic client.
278- Claude on Vertex AI.
279- The `max_tokens` default and prompt caching.
280- Every provider tool, with a full sample.
281- The API reference.
282
283To call Claude through a GitHub Copilot plan, see [GitHub Copilot](./openai-compatible-advanced#claude-models).