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

README.md

217 lines5.5 KB

TanStack AI

@tanstack/ai-anthropic

Anthropic Claude adapter for TanStack AI chat, tool calling, thinking, and structured outputs

Installation

npm install @tanstack/ai-anthropic
# or
pnpm add @tanstack/ai-anthropic
# or
yarn add @tanstack/ai-anthropic

Setup

Set your API key as an environment variable:

export ANTHROPIC_API_KEY="sk-ant-..."

Usage

Text/Chat Adapter

import { chat } from '@tanstack/ai'
import { anthropicText } from '@tanstack/ai-anthropic'

const stream = chat({
  adapter: anthropicText('claude-sonnet-4-6'),
  messages: [{ role: 'user', content: 'Hello!' }],
  threadId: 'thread-1',
})

With Explicit API Key

import { createAnthropicChat } from '@tanstack/ai-anthropic'

const adapter = createAnthropicChat(
  'claude-sonnet-4-6',
  process.env.ANTHROPIC_API_KEY!,
  {
    baseURL: 'https://api.anthropic.com', // optional, for custom endpoints
  },
)

With Tools

import { chat, toServerSentEventsResponse, toolDefinition } from '@tanstack/ai'
import { anthropicText } from '@tanstack/ai-anthropic'
import { z } from 'zod'

const searchDatabase = toolDefinition({
  name: 'search_database',
  description: 'Search the database',
  inputSchema: z.object({ query: z.string() }),
}).server(async ({ query }) => {
  return { results: [] }
})

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

  const stream = chat({
    adapter: anthropicText('claude-sonnet-4-6'),
    messages,
    threadId,
    tools: [searchDatabase],
  })

  return toServerSentEventsResponse(stream)
}

Thinking

Set how hard Claude thinks with reasoning on chat(). The adapter sends the right thinking fields for the model:

const stream = chat({
  adapter: anthropicText('claude-sonnet-5'),
  messages: [{ role: 'user', content: 'Plan a database migration.' }],
  threadId: 'thread-1',
  reasoning: 'xhigh',
  modelOptions: { max_tokens: 64_000 },
})

Models that think with a token budget, such as Claude Haiku 4.5, also take reasoning: { level: 'high', budgetTokens: 8000 }. The adapter raises max_tokens when it is below the budget. The types take only the levels and options of the selected model. See the docs for the levels of each model.

Prompt Caching

Mark a content part with cache_control to cache it:

const stream = chat({
  adapter: anthropicText('claude-sonnet-4-6'),
  messages: [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          content: 'What is the capital of France?',
          metadata: { cache_control: { type: 'ephemeral' } },
        },
      ],
    },
  ],
  threadId: 'thread-1',
})

Summarization

import { summarize } from '@tanstack/ai'
import { anthropicSummarize } from '@tanstack/ai-anthropic'

const result = await summarize({
  adapter: anthropicSummarize('claude-sonnet-4-6'),
  threadId: 'thread-1',
  text: 'Your long text to summarize...',
  maxLength: 100,
  style: 'concise', // "concise" | "bullet-points" | "paragraph"
})

console.log(result.summary)

Provider Tools

Anthropic's native tools live on @tanstack/ai-anthropic/tools and go into chat({ tools }) alongside your own: webSearchTool, webFetchTool, codeExecutionTool, computerUseTool, bashTool, textEditorTool, memoryTool, and customTool.

import { chat } from '@tanstack/ai'
import { anthropicText } from '@tanstack/ai-anthropic'
import { webSearchTool } from '@tanstack/ai-anthropic/tools'

const stream = chat({
  adapter: anthropicText('claude-opus-4-7'),
  messages: [{ role: 'user', content: "What's new in AI this week?" }],
  threadId: 'thread-1',
  tools: [
    webSearchTool({
      name: 'web_search',
      type: 'web_search_20250305',
      max_uses: 2,
    }),
  ],
})

Claude on Vertex AI

Install @anthropic-ai/vertex-sdk next to this package and import from @tanstack/ai-anthropic/vertex:

import { chat } from '@tanstack/ai'
import { anthropicVertexText } from '@tanstack/ai-anthropic/vertex'

const stream = chat({
  adapter: anthropicVertexText('claude-sonnet-5', {
    project: 'my-project',
    location: 'europe-west1',
  }),
  messages: [{ role: 'user', content: 'Hello!' }],
  threadId: 'thread-1',
})

location is required (on the factory or via GOOGLE_CLOUD_LOCATION, GOOGLE_VERTEX_LOCATION, or CLOUD_ML_REGION); project can come from Application Default Credentials. Only the Claude models in the Vertex catalog are accepted.

Features

  • ✅ Streaming chat
  • ✅ Function/tool calling and Anthropic's native provider tools
  • ✅ Extended and adaptive thinking
  • ✅ Structured output
  • ✅ Prompt caching
  • ✅ Summarization (anthropicSummarize)
  • ✅ Claude on Vertex AI (@tanstack/ai-anthropic/vertex)

Documentation

  • Anthropic adapter: configuration, per-model thinking rules, every provider tool, and the API reference
  • Provider Tools: the concept, comparison matrix, and which models support which tools

License

MIT