@tanstack/ai-anthropic 0.18.10 → 0.18.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +213 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source
|
|
4
|
+
media="(prefers-color-scheme: dark)"
|
|
5
|
+
srcset="https://tanstack.com/api/readme/ai.png?theme=dark"
|
|
6
|
+
/>
|
|
7
|
+
<source
|
|
8
|
+
media="(prefers-color-scheme: light)"
|
|
9
|
+
srcset="https://tanstack.com/api/readme/ai.png"
|
|
10
|
+
/>
|
|
11
|
+
<img
|
|
12
|
+
src="https://tanstack.com/api/readme/ai.png"
|
|
13
|
+
alt="TanStack AI"
|
|
14
|
+
width="900"
|
|
15
|
+
/>
|
|
16
|
+
</picture>
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
<br />
|
|
20
|
+
|
|
21
|
+
# @tanstack/ai-anthropic
|
|
22
|
+
|
|
23
|
+
Anthropic Claude adapter for TanStack AI chat, tool calling, thinking, and structured outputs
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install @tanstack/ai-anthropic
|
|
29
|
+
# or
|
|
30
|
+
pnpm add @tanstack/ai-anthropic
|
|
31
|
+
# or
|
|
32
|
+
yarn add @tanstack/ai-anthropic
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Setup
|
|
36
|
+
|
|
37
|
+
Set your API key as an environment variable:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
export ANTHROPIC_API_KEY="sk-ant-..."
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
### Text/Chat Adapter
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { chat } from '@tanstack/ai'
|
|
49
|
+
import { anthropicText } from '@tanstack/ai-anthropic'
|
|
50
|
+
|
|
51
|
+
const stream = chat({
|
|
52
|
+
adapter: anthropicText('claude-sonnet-4-6'),
|
|
53
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
54
|
+
})
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### With Explicit API Key
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { createAnthropicChat } from '@tanstack/ai-anthropic'
|
|
61
|
+
|
|
62
|
+
const adapter = createAnthropicChat(
|
|
63
|
+
'claude-sonnet-4-6',
|
|
64
|
+
process.env.ANTHROPIC_API_KEY!,
|
|
65
|
+
{
|
|
66
|
+
baseURL: 'https://api.anthropic.com', // optional, for custom endpoints
|
|
67
|
+
},
|
|
68
|
+
)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### With Tools
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { chat, toServerSentEventsResponse, toolDefinition } from '@tanstack/ai'
|
|
75
|
+
import { anthropicText } from '@tanstack/ai-anthropic'
|
|
76
|
+
import { z } from 'zod'
|
|
77
|
+
|
|
78
|
+
const searchDatabase = toolDefinition({
|
|
79
|
+
name: 'search_database',
|
|
80
|
+
description: 'Search the database',
|
|
81
|
+
inputSchema: z.object({ query: z.string() }),
|
|
82
|
+
}).server(async ({ query }) => {
|
|
83
|
+
return { results: [] }
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
export async function POST(request: Request) {
|
|
87
|
+
const { messages } = await request.json()
|
|
88
|
+
|
|
89
|
+
const stream = chat({
|
|
90
|
+
adapter: anthropicText('claude-sonnet-4-6'),
|
|
91
|
+
messages,
|
|
92
|
+
tools: [searchDatabase],
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
return toServerSentEventsResponse(stream)
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Thinking
|
|
100
|
+
|
|
101
|
+
Newer Claude models use adaptive thinking — the model decides when and how much to think, and depth is tuned with `output_config.effort`:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const stream = chat({
|
|
105
|
+
adapter: anthropicText('claude-sonnet-5'),
|
|
106
|
+
messages: [{ role: 'user', content: 'Plan a database migration.' }],
|
|
107
|
+
modelOptions: {
|
|
108
|
+
thinking: { type: 'adaptive', display: 'summarized' },
|
|
109
|
+
output_config: { effort: 'xhigh' },
|
|
110
|
+
max_tokens: 64_000,
|
|
111
|
+
},
|
|
112
|
+
})
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Claude 4.6 models also accept the manual `{ type: 'enabled', budget_tokens }` shape; `budget_tokens` must be less than `max_tokens`. Which shapes and sampling parameters each model accepts is enforced by the adapter's types — see the docs for the per-model rules.
|
|
116
|
+
|
|
117
|
+
### Prompt Caching
|
|
118
|
+
|
|
119
|
+
Mark a content part with `cache_control` to cache it:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
const stream = chat({
|
|
123
|
+
adapter: anthropicText('claude-sonnet-4-6'),
|
|
124
|
+
messages: [
|
|
125
|
+
{
|
|
126
|
+
role: 'user',
|
|
127
|
+
content: [
|
|
128
|
+
{
|
|
129
|
+
type: 'text',
|
|
130
|
+
content: 'What is the capital of France?',
|
|
131
|
+
metadata: { cache_control: { type: 'ephemeral' } },
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
})
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Summarization
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { summarize } from '@tanstack/ai'
|
|
143
|
+
import { anthropicSummarize } from '@tanstack/ai-anthropic'
|
|
144
|
+
|
|
145
|
+
const result = await summarize({
|
|
146
|
+
adapter: anthropicSummarize('claude-sonnet-4-6'),
|
|
147
|
+
text: 'Your long text to summarize...',
|
|
148
|
+
maxLength: 100,
|
|
149
|
+
style: 'concise', // "concise" | "bullet-points" | "paragraph"
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
console.log(result.summary)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Provider Tools
|
|
156
|
+
|
|
157
|
+
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`.
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import { chat } from '@tanstack/ai'
|
|
161
|
+
import { anthropicText } from '@tanstack/ai-anthropic'
|
|
162
|
+
import { webSearchTool } from '@tanstack/ai-anthropic/tools'
|
|
163
|
+
|
|
164
|
+
const stream = chat({
|
|
165
|
+
adapter: anthropicText('claude-opus-4-7'),
|
|
166
|
+
messages: [{ role: 'user', content: "What's new in AI this week?" }],
|
|
167
|
+
tools: [
|
|
168
|
+
webSearchTool({
|
|
169
|
+
name: 'web_search',
|
|
170
|
+
type: 'web_search_20250305',
|
|
171
|
+
max_uses: 2,
|
|
172
|
+
}),
|
|
173
|
+
],
|
|
174
|
+
})
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Claude on Vertex AI
|
|
178
|
+
|
|
179
|
+
Install `@anthropic-ai/vertex-sdk` next to this package and import from `@tanstack/ai-anthropic/vertex`:
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
import { chat } from '@tanstack/ai'
|
|
183
|
+
import { anthropicVertexText } from '@tanstack/ai-anthropic/vertex'
|
|
184
|
+
|
|
185
|
+
const stream = chat({
|
|
186
|
+
adapter: anthropicVertexText('claude-sonnet-5', {
|
|
187
|
+
project: 'my-project',
|
|
188
|
+
location: 'europe-west1',
|
|
189
|
+
}),
|
|
190
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
191
|
+
})
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
`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.
|
|
195
|
+
|
|
196
|
+
## Features
|
|
197
|
+
|
|
198
|
+
- ✅ Streaming chat
|
|
199
|
+
- ✅ Function/tool calling and Anthropic's native provider tools
|
|
200
|
+
- ✅ Extended and adaptive thinking
|
|
201
|
+
- ✅ Structured output
|
|
202
|
+
- ✅ Prompt caching
|
|
203
|
+
- ✅ Summarization (`anthropicSummarize`)
|
|
204
|
+
- ✅ Claude on Vertex AI (`@tanstack/ai-anthropic/vertex`)
|
|
205
|
+
|
|
206
|
+
## Documentation
|
|
207
|
+
|
|
208
|
+
- [Anthropic adapter](https://tanstack.com/ai/latest/docs/adapters/anthropic): configuration, per-model thinking rules, every provider tool, and the API reference
|
|
209
|
+
- [Provider Tools](https://tanstack.com/ai/latest/docs/tools/provider-tools): the concept, comparison matrix, and which models support which tools
|
|
210
|
+
|
|
211
|
+
## License
|
|
212
|
+
|
|
213
|
+
MIT
|
package/package.json
CHANGED