|
2 | 2 | title: Anthropic |
3 | 3 | id: anthropic-adapter |
4 | 4 | order: 2 |
| 5 | +description: "Use Anthropic Claude models with TanStack AI — Claude Sonnet 4.5, Claude Opus, and more via the @tanstack/ai-anthropic adapter." |
| 6 | +keywords: |
| 7 | + - tanstack ai |
| 8 | + - anthropic |
| 9 | + - claude |
| 10 | + - claude sonnet 4.5 |
| 11 | + - claude opus |
| 12 | + - adapter |
| 13 | + - llm |
5 | 14 | --- |
6 | 15 |
|
7 | 16 | The Anthropic adapter provides access to Claude models, including Claude Sonnet 4.5, Claude Opus 4.5, and more. |
@@ -228,3 +237,198 @@ Creates an Anthropic summarization adapter with an explicit API key. |
228 | 237 | - [Getting Started](../getting-started/quick-start) - Learn the basics |
229 | 238 | - [Tools Guide](../tools/tools) - Learn about tools |
230 | 239 | - [Other Adapters](./openai) - Explore other providers |
| 240 | + |
| 241 | +## Provider Tools |
| 242 | + |
| 243 | +Anthropic exposes several native tools beyond user-defined function calls. |
| 244 | +Import them from `@tanstack/ai-anthropic/tools` and pass them into |
| 245 | +`chat({ tools: [...] })`. |
| 246 | + |
| 247 | +> For the full concept, a comparison matrix, and type-gating details, see |
| 248 | +> [Provider Tools](../tools/provider-tools.md). |
| 249 | +
|
| 250 | +### `webSearchTool` |
| 251 | + |
| 252 | +Enables Claude to run Anthropic's native web search with inline citations. |
| 253 | +Scope the search with `allowed_domains` or `blocked_domains` (mutually |
| 254 | +exclusive); set `max_uses` to cap per-turn cost. |
| 255 | + |
| 256 | +```typescript |
| 257 | +import { chat } from "@tanstack/ai"; |
| 258 | +import { anthropicText } from "@tanstack/ai-anthropic"; |
| 259 | +import { webSearchTool } from "@tanstack/ai-anthropic/tools"; |
| 260 | + |
| 261 | +const stream = chat({ |
| 262 | + adapter: anthropicText("claude-opus-4-6"), |
| 263 | + messages: [{ role: "user", content: "What's new in AI this week?" }], |
| 264 | + tools: [ |
| 265 | + webSearchTool({ |
| 266 | + name: "web_search", |
| 267 | + type: "web_search_20250305", |
| 268 | + max_uses: 2, |
| 269 | + }), |
| 270 | + ], |
| 271 | +}); |
| 272 | +``` |
| 273 | + |
| 274 | +**Supported models:** every current Claude model. `claude-3-haiku` supports |
| 275 | +only `web_search` (not `web_fetch`). See [Provider Tools](../tools/provider-tools.md#which-models-support-which-tools). |
| 276 | + |
| 277 | +### `webFetchTool` |
| 278 | + |
| 279 | +Lets Claude fetch the contents of a URL directly, useful when you want the |
| 280 | +model to read a specific page rather than run a search. Takes no required |
| 281 | +arguments — pass an optional config object to override defaults. |
| 282 | + |
| 283 | +```typescript |
| 284 | +import { chat } from "@tanstack/ai"; |
| 285 | +import { anthropicText } from "@tanstack/ai-anthropic"; |
| 286 | +import { webFetchTool } from "@tanstack/ai-anthropic/tools"; |
| 287 | + |
| 288 | +const stream = chat({ |
| 289 | + adapter: anthropicText("claude-sonnet-4-5"), |
| 290 | + messages: [{ role: "user", content: "Summarise https://example.com" }], |
| 291 | + tools: [webFetchTool()], |
| 292 | +}); |
| 293 | +``` |
| 294 | + |
| 295 | +**Supported models:** Claude Sonnet 4.x and above. See [Provider Tools](../tools/provider-tools.md#which-models-support-which-tools). |
| 296 | + |
| 297 | +### `codeExecutionTool` |
| 298 | + |
| 299 | +Gives Claude a sandboxed code-execution environment so it can run Python |
| 300 | +snippets, analyse data, and return results inline. Choose the version string |
| 301 | +that matches your desired API revision. |
| 302 | + |
| 303 | +```typescript |
| 304 | +import { chat } from "@tanstack/ai"; |
| 305 | +import { anthropicText } from "@tanstack/ai-anthropic"; |
| 306 | +import { codeExecutionTool } from "@tanstack/ai-anthropic/tools"; |
| 307 | + |
| 308 | +const stream = chat({ |
| 309 | + adapter: anthropicText("claude-sonnet-4-5"), |
| 310 | + messages: [{ role: "user", content: "Plot a histogram of [1,2,2,3,3,3]" }], |
| 311 | + tools: [ |
| 312 | + codeExecutionTool({ name: "code_execution", type: "code_execution_20250825" }), |
| 313 | + ], |
| 314 | +}); |
| 315 | +``` |
| 316 | + |
| 317 | +**Supported models:** Claude Sonnet 4.x and above. See [Provider Tools](../tools/provider-tools.md#which-models-support-which-tools). |
| 318 | + |
| 319 | +### `computerUseTool` |
| 320 | + |
| 321 | +Allows Claude to observe a virtual desktop (screenshots) and interact with it |
| 322 | +via keyboard and mouse events. Provide the screen resolution so Claude can |
| 323 | +calculate accurate coordinates. |
| 324 | + |
| 325 | +```typescript |
| 326 | +import { chat } from "@tanstack/ai"; |
| 327 | +import { anthropicText } from "@tanstack/ai-anthropic"; |
| 328 | +import { computerUseTool } from "@tanstack/ai-anthropic/tools"; |
| 329 | + |
| 330 | +const stream = chat({ |
| 331 | + adapter: anthropicText("claude-sonnet-4-5"), |
| 332 | + messages: [{ role: "user", content: "Open the browser and go to example.com" }], |
| 333 | + tools: [ |
| 334 | + computerUseTool({ |
| 335 | + type: "computer_20250124", |
| 336 | + name: "computer", |
| 337 | + display_width_px: 1024, |
| 338 | + display_height_px: 768, |
| 339 | + }), |
| 340 | + ], |
| 341 | +}); |
| 342 | +``` |
| 343 | + |
| 344 | +**Supported models:** Claude Sonnet 3.5 and above. See [Provider Tools](../tools/provider-tools.md#which-models-support-which-tools). |
| 345 | + |
| 346 | +### `bashTool` |
| 347 | + |
| 348 | +Provides Claude with a persistent bash shell session, letting it run arbitrary |
| 349 | +commands, install packages, or manipulate files on the host. Choose the type |
| 350 | +string that matches your API revision. |
| 351 | + |
| 352 | +```typescript |
| 353 | +import { chat } from "@tanstack/ai"; |
| 354 | +import { anthropicText } from "@tanstack/ai-anthropic"; |
| 355 | +import { bashTool } from "@tanstack/ai-anthropic/tools"; |
| 356 | + |
| 357 | +const stream = chat({ |
| 358 | + adapter: anthropicText("claude-sonnet-4-5"), |
| 359 | + messages: [{ role: "user", content: "List all TypeScript files in src/" }], |
| 360 | + tools: [bashTool({ name: "bash", type: "bash_20250124" })], |
| 361 | +}); |
| 362 | +``` |
| 363 | + |
| 364 | +**Supported models:** Claude Sonnet 3.5 and above. See [Provider Tools](../tools/provider-tools.md#which-models-support-which-tools). |
| 365 | + |
| 366 | +### `textEditorTool` |
| 367 | + |
| 368 | +Gives Claude a structured text-editor interface for viewing and modifying files |
| 369 | +using `str_replace`, `create`, `view`, and `undo_edit` commands. Choose the |
| 370 | +type string for the API revision you target. |
| 371 | + |
| 372 | +```typescript |
| 373 | +import { chat } from "@tanstack/ai"; |
| 374 | +import { anthropicText } from "@tanstack/ai-anthropic"; |
| 375 | +import { textEditorTool } from "@tanstack/ai-anthropic/tools"; |
| 376 | + |
| 377 | +const stream = chat({ |
| 378 | + adapter: anthropicText("claude-sonnet-4-5"), |
| 379 | + messages: [{ role: "user", content: "Fix the bug in src/index.ts" }], |
| 380 | + tools: [ |
| 381 | + textEditorTool({ type: "text_editor_20250124", name: "str_replace_editor" }), |
| 382 | + ], |
| 383 | +}); |
| 384 | +``` |
| 385 | + |
| 386 | +**Supported models:** Claude Sonnet 3.5 and above. See [Provider Tools](../tools/provider-tools.md#which-models-support-which-tools). |
| 387 | + |
| 388 | +### `memoryTool` |
| 389 | + |
| 390 | +Enables Claude to store and retrieve information across conversation turns |
| 391 | +using Anthropic's managed memory service. Call with no arguments to use |
| 392 | +default configuration. |
| 393 | + |
| 394 | +```typescript |
| 395 | +import { chat } from "@tanstack/ai"; |
| 396 | +import { anthropicText } from "@tanstack/ai-anthropic"; |
| 397 | +import { memoryTool } from "@tanstack/ai-anthropic/tools"; |
| 398 | + |
| 399 | +const stream = chat({ |
| 400 | + adapter: anthropicText("claude-sonnet-4-5"), |
| 401 | + messages: [{ role: "user", content: "Remember that I prefer metric units" }], |
| 402 | + tools: [memoryTool()], |
| 403 | +}); |
| 404 | +``` |
| 405 | + |
| 406 | +**Supported models:** Claude Sonnet 4.x and above. See [Provider Tools](../tools/provider-tools.md#which-models-support-which-tools). |
| 407 | + |
| 408 | +### `customTool` |
| 409 | + |
| 410 | +Creates a tool with an inline JSON Schema input definition instead of going |
| 411 | +through `toolDefinition()`. Useful when you need fine-grained control over the |
| 412 | +schema shape or want to add `cache_control`. Unlike branded provider tools, |
| 413 | +`customTool` returns a plain `Tool` and is accepted by any chat model. |
| 414 | + |
| 415 | +```typescript |
| 416 | +import { chat } from "@tanstack/ai"; |
| 417 | +import { anthropicText } from "@tanstack/ai-anthropic"; |
| 418 | +import { customTool } from "@tanstack/ai-anthropic/tools"; |
| 419 | +import { z } from "zod"; |
| 420 | + |
| 421 | +const stream = chat({ |
| 422 | + adapter: anthropicText("claude-sonnet-4-5"), |
| 423 | + messages: [{ role: "user", content: "Look up user 42" }], |
| 424 | + tools: [ |
| 425 | + customTool( |
| 426 | + "lookup_user", |
| 427 | + "Look up a user by ID and return their profile", |
| 428 | + z.object({ userId: z.number() }), |
| 429 | + ), |
| 430 | + ], |
| 431 | +}); |
| 432 | +``` |
| 433 | + |
| 434 | +**Supported models:** all current Claude models. See [Provider Tools](../tools/provider-tools.md#which-models-support-which-tools). |
0 commit comments