Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
7950595
feat: split up adapters into functionalities and add generate function
AlemTuzlak Dec 10, 2025
fd7cf3d
ci: apply automated fixes
autofix-ci[bot] Dec 10, 2025
964ac90
Merge branch 'main' into feat/splitting-adapters-up
AlemTuzlak Dec 10, 2025
59f01ec
readme update
AlemTuzlak Dec 10, 2025
1f6cc72
add structured outputs and stream flag
AlemTuzlak Dec 11, 2025
86fa305
ci: apply automated fixes
autofix-ci[bot] Dec 11, 2025
c5adb3e
image generation for openai and gemini
AlemTuzlak Dec 11, 2025
5c9703c
ci: apply automated fixes
autofix-ci[bot] Dec 11, 2025
b797649
structured outputs
AlemTuzlak Dec 11, 2025
14c4297
Merge branch 'feat/splitting-adapters-up' of https://github.com/TanSt…
AlemTuzlak Dec 11, 2025
6e8f62b
lock fix
AlemTuzlak Dec 11, 2025
935111d
ci: apply automated fixes
autofix-ci[bot] Dec 11, 2025
96a08b1
Splitting made operational with extra tests (#142)
jherr Dec 12, 2025
2b56f66
Adding av adapters (#144)
jherr Dec 13, 2025
2f5d2dc
Splitting adapters up fixes (#148)
jherr Dec 17, 2025
5fda1f4
migrate model into adapter (#157)
AlemTuzlak Dec 18, 2025
a3d2a22
Merge branch 'main' into feat/splitting-adapters-up
AlemTuzlak Dec 18, 2025
4429b2f
type safety tests for chat
AlemTuzlak Dec 18, 2025
12b97aa
ci: apply automated fixes
autofix-ci[bot] Dec 18, 2025
e02fc65
image tests
AlemTuzlak Dec 18, 2025
d7242a1
ci: apply automated fixes
autofix-ci[bot] Dec 18, 2025
39c1eb9
update format
AlemTuzlak Dec 18, 2025
c7a85ae
ci: apply automated fixes
autofix-ci[bot] Dec 18, 2025
7ba644d
flatten options object
AlemTuzlak Dec 18, 2025
3e0cdee
Merge branch 'feat/splitting-adapters-up' of https://github.com/TanSt…
AlemTuzlak Dec 18, 2025
584160c
ci: apply automated fixes
autofix-ci[bot] Dec 18, 2025
d7cae68
testing fixups
jherr Dec 18, 2025
99a4656
fix: streaming functions and migration doc
jherr Dec 18, 2025
9522456
few more adapter/model fixups
jherr Dec 18, 2025
2cb51f8
ci: apply automated fixes
autofix-ci[bot] Dec 18, 2025
a9b12ea
small bug fixes
AlemTuzlak Dec 19, 2025
823756c
ci: apply automated fixes
autofix-ci[bot] Dec 19, 2025
47b961d
fix up exports and example
AlemTuzlak Dec 19, 2025
2f8a4a2
tighten up types
AlemTuzlak Dec 19, 2025
5dded11
ci: apply automated fixes
autofix-ci[bot] Dec 19, 2025
bbc5f98
tighten up types
AlemTuzlak Dec 19, 2025
954a245
Merge branch 'feat/splitting-adapters-up' of https://github.com/TanSt…
AlemTuzlak Dec 19, 2025
f7ad2cb
ci: apply automated fixes
autofix-ci[bot] Dec 19, 2025
d87a93f
fixes
AlemTuzlak Dec 19, 2025
5bc2ff6
ci: apply automated fixes
autofix-ci[bot] Dec 19, 2025
d1abe4d
updating the changeset
jherr Dec 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 18 additions & 0 deletions .changeset/two-bikes-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@tanstack/ai-anthropic': minor
'@tanstack/ai-gemini': minor
'@tanstack/ai-ollama': minor
'@tanstack/ai-openai': minor
'@tanstack/ai': minor
'@tanstack/ai-client': minor
'@tanstack/ai-devtools': minor
'@tanstack/ai-react': minor
'@tanstack/ai-react-ui': minor
'@tanstack/ai-solid': minor
'@tanstack/ai-svelte': minor
'@tanstack/ai-vue': minor
'@tanstack/ai-vue-ui': minor
'@tanstack/react-ai-devtools': minor
---

Split up adapters for better tree shaking into separate functionalities
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,40 @@
A powerful, type-safe AI SDK for building AI-powered applications.

- Provider-agnostic adapters (OpenAI, Anthropic, Gemini, Ollama, etc.)
- **Tree-shakeable adapters** - Import only what you need for smaller bundles
- **Multimodal content support** - Send images, audio, video, and documents
- **Image generation** - Generate images with OpenAI DALL-E/GPT-Image and Gemini Imagen
- Chat completion, streaming, and agent loop strategies
- Headless chat state management with adapters (SSE, HTTP stream, custom)
- Isomorphic type-safe tools with server/client execution
- **Enhanced integration with TanStack Start** - Share implementations between AI tools and server functions

### <a href="https://tanstack.com/ai">Read the docs →</b></a>

## Tree-Shakeable Adapters

Import only the functionality you need for smaller bundle sizes:

```typescript
// Only chat functionality - no summarization code bundled
import { openaiText } from '@tanstack/ai-openai/adapters'
import { generate } from '@tanstack/ai'

const textAdapter = openaiText()

const result = generate({
adapter: textAdapter,
model: 'gpt-4o',
messages: [{ role: 'user', content: [{ type: 'text', content: 'Hello!' }] }],
})

for await (const chunk of result) {
console.log(chunk)
}
```

Available adapters: `openaiText`, `openaiEmbed`, `openaiSummarize`, `anthropicText`, `geminiText`, `ollamaText`, and more.

## Bonus: TanStack Start Integration

TanStack AI works with **any** framework (Next.js, Express, Remix, etc.).
Expand Down
165 changes: 98 additions & 67 deletions docs/adapters/anthropic.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
title: Anthropic Adapter
slug: /adapters/anthropic
title: Anthropic
id: anthropic-adapter
order: 2
---

The Anthropic adapter provides access to Claude models, including Claude 3.5 Sonnet, Claude 3 Opus, and more.
The Anthropic adapter provides access to Claude models, including Claude Sonnet 4.5, Claude Opus 4.5, and more.

## Installation

Expand All @@ -15,62 +16,55 @@ npm install @tanstack/ai-anthropic

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

const adapter = anthropic();
import { anthropicText } from "@tanstack/ai-anthropic";

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

## Basic Usage - Custom API Key

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

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

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

## Configuration

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

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

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


## Example: Chat Completion

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

const adapter = anthropic();
import { anthropicText } from "@tanstack/ai-anthropic";

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

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

return toStreamResponse(stream);
Expand All @@ -81,11 +75,9 @@ export async function POST(request: Request) {

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

const adapter = anthropic();

const searchDatabaseDef = toolDefinition({
name: "search_database",
description: "Search the database",
Expand All @@ -96,46 +88,40 @@ const searchDatabaseDef = toolDefinition({

const searchDatabase = searchDatabaseDef.server(async ({ query }) => {
// Search database
return { results: [...] };
return { results: [] };
});

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

## Provider Options
## Model Options

Anthropic supports provider-specific options:
Anthropic supports various provider-specific options:

```typescript
const stream = chat({
adapter: anthropic(),
adapter: anthropicText("claude-sonnet-4-5"),
messages,
model: "claude-3-5-sonnet-20241022",
providerOptions: {
thinking: {
type: "enabled",
budgetTokens: 1000,
},
cacheControl: {
type: "ephemeral",
ttl: "5m",
},
sendReasoning: true,
modelOptions: {
max_tokens: 4096,
temperature: 0.7,
top_p: 0.9,
top_k: 40,
stop_sequences: ["END"],
},
});
```

### Thinking (Extended Thinking)

Enable extended thinking with a token budget. This allows Claude to show its reasoning process, which is streamed as `thinking` chunks and displayed as `ThinkingPart` in messages:
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 @@ -145,32 +131,49 @@ providerOptions: {

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

**Supported Models:**
### Prompt Caching

- `claude-sonnet-4-5-20250929` and newer
- `claude-opus-4-5-20251101` and newer
Cache prompts for better performance and reduced costs:

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.
```typescript
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",
},
},
},
],
},
],
});
```

### Prompt Caching
## Summarization

Cache prompts for better performance:
Anthropic supports text summarization:

```typescript
messages: [
{ role: "user", content: [{
type: "text",
content: "What is the capital of France?",
metadata: {
cache_control: {
type: "ephemeral",
ttl: "5m",
}
}
}]}
]
```
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);
```

## Environment Variables

Expand All @@ -182,15 +185,43 @@ ANTHROPIC_API_KEY=sk-ant-...

## API Reference

### `anthropic(config)`
### `anthropicText(config?)`

Creates an Anthropic chat adapter using environment variables.

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

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

Creates an Anthropic adapter instance.
Creates an Anthropic chat adapter with an explicit API key.

**Parameters:**

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

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

### `anthropicSummarize(config?)`

Creates an Anthropic summarization adapter using environment variables.

**Returns:** An Anthropic summarize adapter instance.

### `createAnthropicSummarize(apiKey, config?)`

Creates an Anthropic summarization adapter with an explicit API key.

**Parameters:**

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

**Returns:** An Anthropic summarize adapter instance.

## Limitations

**Returns:** An Anthropic adapter instance.
- **Image Generation**: Anthropic does not support image generation. Use OpenAI or Gemini for image generation.

## Next Steps

Expand Down
Loading
Loading