The Anthropic adapter provides access to Claude models, including Claude Sonnet 4.5, Claude Opus 4.5, and more.
npm install @tanstack/ai-anthropic
npm install @tanstack/ai-anthropic
import { chat } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages: [{ role: "user", content: "Hello!" }],
});
import { chat } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages: [{ role: "user", content: "Hello!" }],
});
import { chat } from "@tanstack/ai";
import { createAnthropicChat } from "@tanstack/ai-anthropic";
const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, {
// ... your config options
});
const stream = chat({
adapter: adapter("claude-sonnet-4-5"),
messages: [{ role: "user", content: "Hello!" }],
});
import { chat } from "@tanstack/ai";
import { createAnthropicChat } from "@tanstack/ai-anthropic";
const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, {
// ... your config options
});
const stream = chat({
adapter: adapter("claude-sonnet-4-5"),
messages: [{ role: "user", content: "Hello!" }],
});
import { createAnthropicChat, type AnthropicChatConfig } from "@tanstack/ai-anthropic";
const config: Omit<AnthropicChatConfig, 'apiKey'> = {
baseURL: "https://api.anthropic.com", // Optional, for custom endpoints
};
const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, config);
import { createAnthropicChat, type AnthropicChatConfig } from "@tanstack/ai-anthropic";
const config: Omit<AnthropicChatConfig, 'apiKey'> = {
baseURL: "https://api.anthropic.com", // Optional, for custom endpoints
};
const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, config);
import { chat, toServerSentEventsResponse } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
export async function POST(request: Request) {
const { messages } = await request.json();
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages,
});
return toServerSentEventsResponse(stream);
}
import { chat, toServerSentEventsResponse } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
export async function POST(request: Request) {
const { messages } = await request.json();
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages,
});
return toServerSentEventsResponse(stream);
}
import { chat, toolDefinition } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
import { z } from "zod";
const searchDatabaseDef = toolDefinition({
name: "search_database",
description: "Search the database",
inputSchema: z.object({
query: z.string(),
}),
});
const searchDatabase = searchDatabaseDef.server(async ({ query }) => {
// Search database
return { results: [] };
});
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages,
tools: [searchDatabase],
});
import { chat, toolDefinition } from "@tanstack/ai";
import { anthropicText } from "@tanstack/ai-anthropic";
import { z } from "zod";
const searchDatabaseDef = toolDefinition({
name: "search_database",
description: "Search the database",
inputSchema: z.object({
query: z.string(),
}),
});
const searchDatabase = searchDatabaseDef.server(async ({ query }) => {
// Search database
return { results: [] };
});
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages,
tools: [searchDatabase],
});
Anthropic supports various provider-specific options:
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages,
modelOptions: {
max_tokens: 4096,
temperature: 0.7,
top_p: 0.9,
top_k: 40,
stop_sequences: ["END"],
},
});
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages,
modelOptions: {
max_tokens: 4096,
temperature: 0.7,
top_p: 0.9,
top_k: 40,
stop_sequences: ["END"],
},
});
Enable extended thinking with a token budget. This allows Claude to show its reasoning process, which is streamed as thinking chunks:
modelOptions: {
thinking: {
type: "enabled",
budget_tokens: 2048, // Maximum tokens for thinking
},
}
modelOptions: {
thinking: {
type: "enabled",
budget_tokens: 2048, // Maximum tokens for thinking
},
}
Note: max_tokens must be greater than budget_tokens. The adapter automatically adjusts max_tokens if needed.
Cache prompts for better performance and reduced costs:
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages: [
{
role: "user",
content: [
{
type: "text",
content: "What is the capital of France?",
metadata: {
cache_control: {
type: "ephemeral",
},
},
},
],
},
],
});
const stream = chat({
adapter: anthropicText("claude-sonnet-4-5"),
messages: [
{
role: "user",
content: [
{
type: "text",
content: "What is the capital of France?",
metadata: {
cache_control: {
type: "ephemeral",
},
},
},
],
},
],
});
Anthropic supports text summarization:
import { summarize } from "@tanstack/ai";
import { anthropicSummarize } from "@tanstack/ai-anthropic";
const result = await summarize({
adapter: anthropicSummarize("claude-sonnet-4-5"),
text: "Your long text to summarize...",
maxLength: 100,
style: "concise", // "concise" | "bullet-points" | "paragraph"
});
console.log(result.summary);
import { summarize } from "@tanstack/ai";
import { anthropicSummarize } from "@tanstack/ai-anthropic";
const result = await summarize({
adapter: anthropicSummarize("claude-sonnet-4-5"),
text: "Your long text to summarize...",
maxLength: 100,
style: "concise", // "concise" | "bullet-points" | "paragraph"
});
console.log(result.summary);
Set your API key in environment variables:
ANTHROPIC_API_KEY=sk-ant-...
ANTHROPIC_API_KEY=sk-ant-...
Creates an Anthropic chat adapter using environment variables.
Returns: An Anthropic chat adapter instance.
Creates an Anthropic chat adapter with an explicit API key.
Parameters:
Returns: An Anthropic chat adapter instance.
Creates an Anthropic summarization adapter using environment variables.
Returns: An Anthropic summarize adapter instance.
Creates an Anthropic summarization adapter with an explicit API key.
Parameters:
Returns: An Anthropic summarize adapter instance.
