Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@ The `chat()` method now includes an automatic tool execution loop:

```typescript
import { chat, tool, maxIterations } from '@tanstack/ai'
import { openai } from '@tanstack/ai-openai'
import { openaiText } from '@tanstack/ai-openai'

const stream = chat({
adapter: openai(),
adapter: openaiText(),
model: 'gpt-4o',
messages: [{ role: 'user', content: "What's the weather in Paris?" }],
tools: [weatherTool],
Expand Down
86 changes: 36 additions & 50 deletions docs/adapters/anthropic.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Anthropic Adapter
title: Anthropic
id: anthropic-adapter
order: 2
---

The Anthropic adapter provides access to Claude models, including Claude Sonnet 4.5, Claude Opus 4.5, and more.
Expand All @@ -14,72 +15,63 @@ npm install @tanstack/ai-anthropic
## Basic Usage

```typescript
import { ai } from "@tanstack/ai";
import { chat } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";

const adapter = anthropicText();

const stream = ai({
const stream = chat({
adapter,
model: "claude-sonnet-4-5",
messages: [{ role: "user", content: "Hello!" }],
model: "claude-sonnet-4-5-20250929",
});
```

## Basic Usage - Custom API Key

```typescript
import { ai } from "@tanstack/ai";
import { createAnthropicText } from "@tanstack/ai-anthropic";
import { chat } from "@tanstack/ai";
import { createAnthropicChat } from "@tanstack/ai-anthropic";

const adapter = createAnthropicText(process.env.ANTHROPIC_API_KEY!, {
const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, {
// ... your config options
});

const stream = ai({
const stream = chat({
adapter,
model: "claude-sonnet-4-5",
messages: [{ role: "user", content: "Hello!" }],
model: "claude-sonnet-4-5-20250929",
});
```

## Configuration

```typescript
import { createAnthropicText, type AnthropicTextConfig } from "@tanstack/ai-anthropic";
import { createAnthropicChat, type AnthropicChatConfig } from "@tanstack/ai-anthropic";

const config: AnthropicTextConfig = {
const config: Omit<AnthropicChatConfig, 'apiKey'> = {
baseURL: "https://api.anthropic.com", // Optional, for custom endpoints
};

const adapter = createAnthropicText(process.env.ANTHROPIC_API_KEY!, config);
const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, config);
```

## Available Models

### Chat Models

- `claude-sonnet-4-5-20250929` - Claude Sonnet 4.5 (balanced)
- `claude-opus-4-5-20251101` - Claude Opus 4.5 (most capable)
- `claude-haiku-4-0-20250514` - Claude Haiku 4.0 (fastest)
- `claude-3-5-sonnet-20241022` - Claude 3.5 Sonnet
- `claude-3-opus-20240229` - Claude 3 Opus


## Example: Chat Completion

```typescript
import { ai, toStreamResponse } from "@tanstack/ai";
import { chat, toStreamResponse } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";

const adapter = anthropicText();

export async function POST(request: Request) {
const { messages } = await request.json();

const stream = ai({
const stream = chat({
adapter,
model: "claude-sonnet-4-5",
messages,
model: "claude-sonnet-4-5-20250929",
});

return toStreamResponse(stream);
Expand All @@ -89,7 +81,7 @@ export async function POST(request: Request) {
## Example: With Tools

```typescript
import { ai, toolDefinition } from "@tanstack/ai";
import { chat, toolDefinition } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
import { z } from "zod";

Expand All @@ -108,24 +100,24 @@ const searchDatabase = searchDatabaseDef.server(async ({ query }) => {
return { results: [] };
});

const stream = ai({
const stream = chat({
adapter,
model: "claude-sonnet-4-5",
messages,
model: "claude-sonnet-4-5-20250929",
tools: [searchDatabase],
});
```

## Provider Options
## Model Options

Anthropic supports various provider-specific options:

```typescript
const stream = ai({
const stream = chat({
adapter: anthropicText(),
model: "claude-sonnet-4-5",
messages,
model: "claude-sonnet-4-5-20250929",
providerOptions: {
modelOptions: {
max_tokens: 4096,
temperature: 0.7,
top_p: 0.9,
Expand All @@ -140,7 +132,7 @@ const stream = ai({
Enable extended thinking with a token budget. This allows Claude to show its reasoning process, which is streamed as `thinking` chunks:

```typescript
providerOptions: {
modelOptions: {
thinking: {
type: "enabled",
budget_tokens: 2048, // Maximum tokens for thinking
Expand All @@ -150,20 +142,14 @@ providerOptions: {

**Note:** `max_tokens` must be greater than `budget_tokens`. The adapter automatically adjusts `max_tokens` if needed.

**Supported Models:**

- `claude-sonnet-4-5-20250929` and newer
- `claude-opus-4-5-20251101` and newer

When thinking is enabled, the model's reasoning process is streamed separately from the response text and appears as a collapsible thinking section in the UI.

### Prompt Caching

Cache prompts for better performance and reduced costs:

```typescript
const stream = ai({
const stream = chat({
adapter: anthropicText(),
model: "claude-sonnet-4-5",
messages: [
{
role: "user",
Expand All @@ -180,7 +166,7 @@ const stream = ai({
],
},
],
model: "claude-sonnet-4-5-20250929",
model: "claude-sonnet-4-5",
});
```

Expand All @@ -189,14 +175,14 @@ const stream = ai({
Anthropic supports text summarization:

```typescript
import { ai } from "@tanstack/ai";
import { summarize } from "@tanstack/ai";
import { anthropicSummarize } from "@tanstack/ai-anthropic";

const adapter = anthropicSummarize();

const result = await ai({
const result = await summarize({
adapter,
model: "claude-sonnet-4-5-20250929",
model: "claude-sonnet-4-5",
text: "Your long text to summarize...",
maxLength: 100,
style: "concise", // "concise" | "bullet-points" | "paragraph"
Expand All @@ -217,20 +203,20 @@ ANTHROPIC_API_KEY=sk-ant-...

### `anthropicText(config?)`

Creates an Anthropic text/chat adapter using environment variables.
Creates an Anthropic chat adapter using environment variables.

**Returns:** An Anthropic text adapter instance.
**Returns:** An Anthropic chat adapter instance.

### `createAnthropicText(apiKey, config?)`
### `createAnthropicChat(apiKey, config?)`

Creates an Anthropic text/chat adapter with an explicit API key.
Creates an Anthropic chat adapter with an explicit API key.

**Parameters:**

- `apiKey` - Your Anthropic API key
- `config.baseURL?` - Custom base URL (optional)

**Returns:** An Anthropic text adapter instance.
**Returns:** An Anthropic chat adapter instance.

### `anthropicSummarize(config?)`

Expand Down
Loading
Loading