@ai-sdk/gateway 0.0.0-02dba89b-20251009204516 → 0.0.0-4115c213-20260122152721
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/CHANGELOG.md +886 -167
- package/dist/index.d.mts +205 -34
- package/dist/index.d.ts +205 -34
- package/dist/index.js +459 -180
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +450 -148
- package/dist/index.mjs.map +1 -1
- package/docs/00-ai-gateway.mdx +625 -0
- package/package.json +19 -6
- package/src/errors/as-gateway-error.ts +33 -0
- package/src/errors/create-gateway-error.ts +132 -0
- package/src/errors/extract-api-call-response.ts +15 -0
- package/src/errors/gateway-authentication-error.ts +84 -0
- package/src/errors/gateway-error.ts +47 -0
- package/src/errors/gateway-internal-server-error.ts +33 -0
- package/src/errors/gateway-invalid-request-error.ts +33 -0
- package/src/errors/gateway-model-not-found-error.ts +47 -0
- package/src/errors/gateway-rate-limit-error.ts +33 -0
- package/src/errors/gateway-response-error.ts +42 -0
- package/src/errors/index.ts +16 -0
- package/src/errors/parse-auth-method.ts +23 -0
- package/src/gateway-config.ts +7 -0
- package/src/gateway-embedding-model-settings.ts +22 -0
- package/src/gateway-embedding-model.ts +109 -0
- package/src/gateway-fetch-metadata.ts +127 -0
- package/src/gateway-image-model-settings.ts +12 -0
- package/src/gateway-image-model.ts +145 -0
- package/src/gateway-language-model-settings.ts +159 -0
- package/src/gateway-language-model.ts +212 -0
- package/src/gateway-model-entry.ts +58 -0
- package/src/gateway-provider-options.ts +66 -0
- package/src/gateway-provider.ts +284 -0
- package/src/gateway-tools.ts +15 -0
- package/src/index.ts +27 -0
- package/src/tool/perplexity-search.ts +294 -0
- package/src/vercel-environment.ts +6 -0
- package/src/version.ts +6 -0
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: AI Gateway
|
|
3
|
+
description: Learn how to use the AI Gateway provider with the AI SDK.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# AI Gateway Provider
|
|
7
|
+
|
|
8
|
+
The [AI Gateway](https://vercel.com/docs/ai-gateway) provider connects you to models from multiple AI providers through a single interface. Instead of integrating with each provider separately, you can access OpenAI, Anthropic, Google, Meta, xAI, and other providers and their models.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- Access models from multiple providers without having to install additional provider modules/dependencies
|
|
13
|
+
- Use the same code structure across different AI providers
|
|
14
|
+
- Switch between models and providers easily
|
|
15
|
+
- Automatic authentication when deployed on Vercel
|
|
16
|
+
- View pricing information across providers
|
|
17
|
+
- Observability for AI model usage through the Vercel dashboard
|
|
18
|
+
|
|
19
|
+
## Setup
|
|
20
|
+
|
|
21
|
+
The Vercel AI Gateway provider is part of the AI SDK.
|
|
22
|
+
|
|
23
|
+
## Basic Usage
|
|
24
|
+
|
|
25
|
+
For most use cases, you can use the AI Gateway directly with a model string:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
// use plain model string with global provider
|
|
29
|
+
import { generateText } from 'ai';
|
|
30
|
+
|
|
31
|
+
const { text } = await generateText({
|
|
32
|
+
model: 'openai/gpt-5',
|
|
33
|
+
prompt: 'Hello world',
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
// use provider instance (requires version 5.0.36 or later)
|
|
39
|
+
import { generateText, gateway } from 'ai';
|
|
40
|
+
|
|
41
|
+
const { text } = await generateText({
|
|
42
|
+
model: gateway('openai/gpt-5'),
|
|
43
|
+
prompt: 'Hello world',
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The AI SDK automatically uses the AI Gateway when you pass a model string in the `creator/model-name` format.
|
|
48
|
+
|
|
49
|
+
## Provider Instance
|
|
50
|
+
|
|
51
|
+
<Note>
|
|
52
|
+
The `gateway` provider instance is available from the `ai` package in version
|
|
53
|
+
5.0.36 and later.
|
|
54
|
+
</Note>
|
|
55
|
+
|
|
56
|
+
You can also import the default provider instance `gateway` from `ai`:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { gateway } from 'ai';
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
You may want to create a custom provider instance when you need to:
|
|
63
|
+
|
|
64
|
+
- Set custom configuration options (API key, base URL, headers)
|
|
65
|
+
- Use the provider in a [provider registry](/docs/ai-sdk-core/provider-management)
|
|
66
|
+
- Wrap the provider with [middleware](/docs/ai-sdk-core/middleware)
|
|
67
|
+
- Use different settings for different parts of your application
|
|
68
|
+
|
|
69
|
+
To create a custom provider instance, import `createGateway` from `ai`:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { createGateway } from 'ai';
|
|
73
|
+
|
|
74
|
+
const gateway = createGateway({
|
|
75
|
+
apiKey: process.env.AI_GATEWAY_API_KEY ?? '',
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
You can use the following optional settings to customize the AI Gateway provider instance:
|
|
80
|
+
|
|
81
|
+
- **baseURL** _string_
|
|
82
|
+
|
|
83
|
+
Use a different URL prefix for API calls. The default prefix is `https://ai-gateway.vercel.sh/v3/ai`.
|
|
84
|
+
|
|
85
|
+
- **apiKey** _string_
|
|
86
|
+
|
|
87
|
+
API key that is being sent using the `Authorization` header. It defaults to
|
|
88
|
+
the `AI_GATEWAY_API_KEY` environment variable.
|
|
89
|
+
|
|
90
|
+
- **headers** _Record<string,string>_
|
|
91
|
+
|
|
92
|
+
Custom headers to include in the requests.
|
|
93
|
+
|
|
94
|
+
- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise<Response>_
|
|
95
|
+
|
|
96
|
+
Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
|
|
97
|
+
Defaults to the global `fetch` function.
|
|
98
|
+
You can use it as a middleware to intercept requests,
|
|
99
|
+
or to provide a custom fetch implementation for e.g. testing.
|
|
100
|
+
|
|
101
|
+
- **metadataCacheRefreshMillis** _number_
|
|
102
|
+
|
|
103
|
+
How frequently to refresh the metadata cache in milliseconds. Defaults to 5 minutes (300,000ms).
|
|
104
|
+
|
|
105
|
+
## Authentication
|
|
106
|
+
|
|
107
|
+
The Gateway provider supports two authentication methods:
|
|
108
|
+
|
|
109
|
+
### API Key Authentication
|
|
110
|
+
|
|
111
|
+
Set your API key via environment variable:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
AI_GATEWAY_API_KEY=your_api_key_here
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Or pass it directly to the provider:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { createGateway } from 'ai';
|
|
121
|
+
|
|
122
|
+
const gateway = createGateway({
|
|
123
|
+
apiKey: 'your_api_key_here',
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### OIDC Authentication (Vercel Deployments)
|
|
128
|
+
|
|
129
|
+
When deployed to Vercel, the AI Gateway provider supports authenticating using [OIDC (OpenID Connect)
|
|
130
|
+
tokens](https://vercel.com/docs/oidc) without API Keys.
|
|
131
|
+
|
|
132
|
+
#### How OIDC Authentication Works
|
|
133
|
+
|
|
134
|
+
1. **In Production/Preview Deployments**:
|
|
135
|
+
|
|
136
|
+
- OIDC authentication is automatically handled
|
|
137
|
+
- No manual configuration needed
|
|
138
|
+
- Tokens are automatically obtained and refreshed
|
|
139
|
+
|
|
140
|
+
2. **In Local Development**:
|
|
141
|
+
- First, install and authenticate with the [Vercel CLI](https://vercel.com/docs/cli)
|
|
142
|
+
- Run `vercel env pull` to download your project's OIDC token locally
|
|
143
|
+
- For automatic token management:
|
|
144
|
+
- Use `vercel dev` to start your development server - this will handle token refreshing automatically
|
|
145
|
+
- For manual token management:
|
|
146
|
+
- If not using `vercel dev`, note that OIDC tokens expire after 12 hours
|
|
147
|
+
- You'll need to run `vercel env pull` again to refresh the token before it expires
|
|
148
|
+
|
|
149
|
+
<Note>
|
|
150
|
+
If an API Key is present (either passed directly or via environment), it will
|
|
151
|
+
always be used, even if invalid.
|
|
152
|
+
</Note>
|
|
153
|
+
|
|
154
|
+
Read more about using OIDC tokens in the [Vercel AI Gateway docs](https://vercel.com/docs/ai-gateway#using-the-ai-gateway-with-a-vercel-oidc-token).
|
|
155
|
+
|
|
156
|
+
## Bring Your Own Key (BYOK)
|
|
157
|
+
|
|
158
|
+
You can connect your own provider credentials to use with Vercel AI Gateway. This lets you use your existing provider accounts and access private resources.
|
|
159
|
+
|
|
160
|
+
To set up BYOK, add your provider credentials in your Vercel team's AI Gateway settings. Once configured, AI Gateway automatically uses your credentials. No code changes are needed.
|
|
161
|
+
|
|
162
|
+
Learn more in the [BYOK documentation](https://vercel.com/docs/ai-gateway/byok).
|
|
163
|
+
|
|
164
|
+
## Language Models
|
|
165
|
+
|
|
166
|
+
You can create language models using a provider instance. The first argument is the model ID in the format `creator/model-name`:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
import { generateText } from 'ai';
|
|
170
|
+
|
|
171
|
+
const { text } = await generateText({
|
|
172
|
+
model: 'openai/gpt-5',
|
|
173
|
+
prompt: 'Explain quantum computing in simple terms',
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
AI Gateway language models can also be used in the `streamText`, `generateObject`, and `streamObject` functions (see [AI SDK Core](/docs/ai-sdk-core)).
|
|
178
|
+
|
|
179
|
+
## Available Models
|
|
180
|
+
|
|
181
|
+
The AI Gateway supports models from OpenAI, Anthropic, Google, Meta, xAI, Mistral, DeepSeek, Amazon Bedrock, Cohere, Perplexity, Alibaba, and other providers.
|
|
182
|
+
|
|
183
|
+
For the complete list of available models, see the [AI Gateway documentation](https://vercel.com/docs/ai-gateway).
|
|
184
|
+
|
|
185
|
+
## Dynamic Model Discovery
|
|
186
|
+
|
|
187
|
+
You can discover available models programmatically:
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
import { gateway, generateText } from 'ai';
|
|
191
|
+
|
|
192
|
+
const availableModels = await gateway.getAvailableModels();
|
|
193
|
+
|
|
194
|
+
// List all available models
|
|
195
|
+
availableModels.models.forEach(model => {
|
|
196
|
+
console.log(`${model.id}: ${model.name}`);
|
|
197
|
+
if (model.description) {
|
|
198
|
+
console.log(` Description: ${model.description}`);
|
|
199
|
+
}
|
|
200
|
+
if (model.pricing) {
|
|
201
|
+
console.log(` Input: $${model.pricing.input}/token`);
|
|
202
|
+
console.log(` Output: $${model.pricing.output}/token`);
|
|
203
|
+
if (model.pricing.cachedInputTokens) {
|
|
204
|
+
console.log(
|
|
205
|
+
` Cached input (read): $${model.pricing.cachedInputTokens}/token`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
if (model.pricing.cacheCreationInputTokens) {
|
|
209
|
+
console.log(
|
|
210
|
+
` Cache creation (write): $${model.pricing.cacheCreationInputTokens}/token`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// Use any discovered model with plain string
|
|
217
|
+
const { text } = await generateText({
|
|
218
|
+
model: availableModels.models[0].id, // e.g., 'openai/gpt-4o'
|
|
219
|
+
prompt: 'Hello world',
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Credit Usage
|
|
224
|
+
|
|
225
|
+
You can check your team's current credit balance and usage:
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
import { gateway } from 'ai';
|
|
229
|
+
|
|
230
|
+
const credits = await gateway.getCredits();
|
|
231
|
+
|
|
232
|
+
console.log(`Team balance: ${credits.balance} credits`);
|
|
233
|
+
console.log(`Team total used: ${credits.total_used} credits`);
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
The `getCredits()` method returns your team's credit information based on the authenticated API key or OIDC token:
|
|
237
|
+
|
|
238
|
+
- **balance** _number_ - Your team's current available credit balance
|
|
239
|
+
- **total_used** _number_ - Total credits consumed by your team
|
|
240
|
+
|
|
241
|
+
## Examples
|
|
242
|
+
|
|
243
|
+
### Basic Text Generation
|
|
244
|
+
|
|
245
|
+
```ts
|
|
246
|
+
import { generateText } from 'ai';
|
|
247
|
+
|
|
248
|
+
const { text } = await generateText({
|
|
249
|
+
model: 'anthropic/claude-sonnet-4',
|
|
250
|
+
prompt: 'Write a haiku about programming',
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
console.log(text);
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Streaming
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
import { streamText } from 'ai';
|
|
260
|
+
|
|
261
|
+
const { textStream } = await streamText({
|
|
262
|
+
model: 'openai/gpt-5',
|
|
263
|
+
prompt: 'Explain the benefits of serverless architecture',
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
for await (const textPart of textStream) {
|
|
267
|
+
process.stdout.write(textPart);
|
|
268
|
+
}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### Tool Usage
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
import { generateText, tool } from 'ai';
|
|
275
|
+
import { z } from 'zod';
|
|
276
|
+
|
|
277
|
+
const { text } = await generateText({
|
|
278
|
+
model: 'xai/grok-4',
|
|
279
|
+
prompt: 'What is the weather like in San Francisco?',
|
|
280
|
+
tools: {
|
|
281
|
+
getWeather: tool({
|
|
282
|
+
description: 'Get the current weather for a location',
|
|
283
|
+
parameters: z.object({
|
|
284
|
+
location: z.string().describe('The location to get weather for'),
|
|
285
|
+
}),
|
|
286
|
+
execute: async ({ location }) => {
|
|
287
|
+
// Your weather API call here
|
|
288
|
+
return `It's sunny in ${location}`;
|
|
289
|
+
},
|
|
290
|
+
}),
|
|
291
|
+
},
|
|
292
|
+
});
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### Provider-Executed Tools
|
|
296
|
+
|
|
297
|
+
Some providers offer tools that are executed by the provider itself, such as [OpenAI's web search tool](/providers/ai-sdk-providers/openai#web-search-tool). To use these tools through AI Gateway, import the provider to access the tool definitions:
|
|
298
|
+
|
|
299
|
+
```ts
|
|
300
|
+
import { generateText, stepCountIs } from 'ai';
|
|
301
|
+
import { openai } from '@ai-sdk/openai';
|
|
302
|
+
|
|
303
|
+
const result = await generateText({
|
|
304
|
+
model: 'openai/gpt-5-mini',
|
|
305
|
+
prompt: 'What is the Vercel AI Gateway?',
|
|
306
|
+
stopWhen: stepCountIs(10),
|
|
307
|
+
tools: {
|
|
308
|
+
web_search: openai.tools.webSearch({}),
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
console.dir(result.text);
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
<Note>
|
|
316
|
+
Some provider-executed tools require account-specific configuration (such as
|
|
317
|
+
Claude Agent Skills) and may not work through AI Gateway. To use these tools,
|
|
318
|
+
you must bring your own key (BYOK) directly to the provider.
|
|
319
|
+
</Note>
|
|
320
|
+
|
|
321
|
+
### Gateway Tools
|
|
322
|
+
|
|
323
|
+
The AI Gateway provider includes built-in tools that are executed by the gateway itself. These tools can be used with any model through the gateway.
|
|
324
|
+
|
|
325
|
+
#### Perplexity Search
|
|
326
|
+
|
|
327
|
+
The Perplexity Search tool enables models to search the web using [Perplexity's search API](https://docs.perplexity.ai/guides/search-quickstart). This tool is executed by the AI Gateway and returns web search results that the model can use to provide up-to-date information.
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
import { gateway, generateText } from 'ai';
|
|
331
|
+
|
|
332
|
+
const result = await generateText({
|
|
333
|
+
model: 'openai/gpt-5-nano',
|
|
334
|
+
prompt: 'Search for news about AI regulations in January 2025.',
|
|
335
|
+
tools: {
|
|
336
|
+
perplexity_search: gateway.tools.perplexitySearch(),
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
console.log(result.text);
|
|
341
|
+
console.log('Tool calls:', JSON.stringify(result.toolCalls, null, 2));
|
|
342
|
+
console.log('Tool results:', JSON.stringify(result.toolResults, null, 2));
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
You can also configure the search with optional parameters:
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
import { gateway, generateText } from 'ai';
|
|
349
|
+
|
|
350
|
+
const result = await generateText({
|
|
351
|
+
model: 'openai/gpt-5-nano',
|
|
352
|
+
prompt:
|
|
353
|
+
'Search for news about AI regulations from the first week of January 2025.',
|
|
354
|
+
tools: {
|
|
355
|
+
perplexity_search: gateway.tools.perplexitySearch({
|
|
356
|
+
maxResults: 5,
|
|
357
|
+
searchLanguageFilter: ['en'],
|
|
358
|
+
country: 'US',
|
|
359
|
+
searchDomainFilter: ['reuters.com', 'bbc.com', 'nytimes.com'],
|
|
360
|
+
}),
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
console.log(result.text);
|
|
365
|
+
console.log('Tool calls:', JSON.stringify(result.toolCalls, null, 2));
|
|
366
|
+
console.log('Tool results:', JSON.stringify(result.toolResults, null, 2));
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
The Perplexity Search tool supports the following optional configuration options:
|
|
370
|
+
|
|
371
|
+
- **maxResults** _number_
|
|
372
|
+
|
|
373
|
+
The maximum number of search results to return (1-20, default: 10).
|
|
374
|
+
|
|
375
|
+
- **maxTokensPerPage** _number_
|
|
376
|
+
|
|
377
|
+
The maximum number of tokens to extract per search result page (256-2048, default: 2048).
|
|
378
|
+
|
|
379
|
+
- **maxTokens** _number_
|
|
380
|
+
|
|
381
|
+
The maximum total tokens across all search results (default: 25000, max: 1000000).
|
|
382
|
+
|
|
383
|
+
- **searchLanguageFilter** _string[]_
|
|
384
|
+
|
|
385
|
+
Filter search results by language using ISO 639-1 language codes (e.g., `['en']` for English, `['en', 'es']` for English and Spanish).
|
|
386
|
+
|
|
387
|
+
- **country** _string_
|
|
388
|
+
|
|
389
|
+
Filter search results by country using ISO 3166-1 alpha-2 country codes (e.g., `'US'` for United States, `'GB'` for United Kingdom).
|
|
390
|
+
|
|
391
|
+
- **searchDomainFilter** _string[]_
|
|
392
|
+
|
|
393
|
+
Limit search results to specific domains (e.g., `['reuters.com', 'bbc.com']`). This is useful for restricting results to trusted sources.
|
|
394
|
+
|
|
395
|
+
- **searchRecencyFilter** _'day' | 'week' | 'month' | 'year'_
|
|
396
|
+
|
|
397
|
+
Filter search results by relative time period. Useful for always getting recent results (e.g., 'week' for results from the last week).
|
|
398
|
+
|
|
399
|
+
The tool works with both `generateText` and `streamText`:
|
|
400
|
+
|
|
401
|
+
```ts
|
|
402
|
+
import { gateway, streamText } from 'ai';
|
|
403
|
+
|
|
404
|
+
const result = streamText({
|
|
405
|
+
model: 'openai/gpt-5-nano',
|
|
406
|
+
prompt: 'Search for the latest news about AI regulations.',
|
|
407
|
+
tools: {
|
|
408
|
+
perplexity_search: gateway.tools.perplexitySearch(),
|
|
409
|
+
},
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
for await (const part of result.fullStream) {
|
|
413
|
+
switch (part.type) {
|
|
414
|
+
case 'text-delta':
|
|
415
|
+
process.stdout.write(part.text);
|
|
416
|
+
break;
|
|
417
|
+
case 'tool-call':
|
|
418
|
+
console.log('\nTool call:', JSON.stringify(part, null, 2));
|
|
419
|
+
break;
|
|
420
|
+
case 'tool-result':
|
|
421
|
+
console.log('\nTool result:', JSON.stringify(part, null, 2));
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
### Usage Tracking with User and Tags
|
|
428
|
+
|
|
429
|
+
Track usage per end-user and categorize requests with tags:
|
|
430
|
+
|
|
431
|
+
```ts
|
|
432
|
+
import type { GatewayProviderOptions } from '@ai-sdk/gateway';
|
|
433
|
+
import { generateText } from 'ai';
|
|
434
|
+
|
|
435
|
+
const { text } = await generateText({
|
|
436
|
+
model: 'openai/gpt-5',
|
|
437
|
+
prompt: 'Summarize this document...',
|
|
438
|
+
providerOptions: {
|
|
439
|
+
gateway: {
|
|
440
|
+
user: 'user-abc-123', // Track usage for this specific end-user
|
|
441
|
+
tags: ['document-summary', 'premium-feature'], // Categorize for reporting
|
|
442
|
+
} satisfies GatewayProviderOptions,
|
|
443
|
+
},
|
|
444
|
+
});
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
This allows you to:
|
|
448
|
+
|
|
449
|
+
- View usage and costs broken down by end-user in your analytics
|
|
450
|
+
- Filter and analyze spending by feature or use case using tags
|
|
451
|
+
- Track which users or features are driving the most AI usage
|
|
452
|
+
|
|
453
|
+
## Provider Options
|
|
454
|
+
|
|
455
|
+
The AI Gateway provider accepts provider options that control routing behavior and provider-specific configurations.
|
|
456
|
+
|
|
457
|
+
### Gateway Provider Options
|
|
458
|
+
|
|
459
|
+
You can use the `gateway` key in `providerOptions` to control how AI Gateway routes requests:
|
|
460
|
+
|
|
461
|
+
```ts
|
|
462
|
+
import type { GatewayProviderOptions } from '@ai-sdk/gateway';
|
|
463
|
+
import { generateText } from 'ai';
|
|
464
|
+
|
|
465
|
+
const { text } = await generateText({
|
|
466
|
+
model: 'anthropic/claude-sonnet-4',
|
|
467
|
+
prompt: 'Explain quantum computing',
|
|
468
|
+
providerOptions: {
|
|
469
|
+
gateway: {
|
|
470
|
+
order: ['vertex', 'anthropic'], // Try Vertex AI first, then Anthropic
|
|
471
|
+
only: ['vertex', 'anthropic'], // Only use these providers
|
|
472
|
+
} satisfies GatewayProviderOptions,
|
|
473
|
+
},
|
|
474
|
+
});
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
The following gateway provider options are available:
|
|
478
|
+
|
|
479
|
+
- **order** _string[]_
|
|
480
|
+
|
|
481
|
+
Specifies the sequence of providers to attempt when routing requests. The gateway will try providers in the order specified. If a provider fails or is unavailable, it will move to the next provider in the list.
|
|
482
|
+
|
|
483
|
+
Example: `order: ['bedrock', 'anthropic']` will attempt Amazon Bedrock first, then fall back to Anthropic.
|
|
484
|
+
|
|
485
|
+
- **only** _string[]_
|
|
486
|
+
|
|
487
|
+
Restricts routing to only the specified providers. When set, the gateway will never route to providers not in this list, even if they would otherwise be available.
|
|
488
|
+
|
|
489
|
+
Example: `only: ['anthropic', 'vertex']` will only allow routing to Anthropic or Vertex AI.
|
|
490
|
+
|
|
491
|
+
- **models** _string[]_
|
|
492
|
+
|
|
493
|
+
Specifies fallback models to use when the primary model fails or is unavailable. The gateway will try the primary model first (specified in the `model` parameter), then try each model in this array in order until one succeeds.
|
|
494
|
+
|
|
495
|
+
Example: `models: ['openai/gpt-5-nano', 'gemini-2.0-flash']` will try the fallback models in order if the primary model fails.
|
|
496
|
+
|
|
497
|
+
- **user** _string_
|
|
498
|
+
|
|
499
|
+
Optional identifier for the end user on whose behalf the request is being made. This is used for spend tracking and attribution purposes, allowing you to track usage per end-user in your application.
|
|
500
|
+
|
|
501
|
+
Example: `user: 'user-123'` will associate this request with end-user ID "user-123" in usage reports.
|
|
502
|
+
|
|
503
|
+
- **tags** _string[]_
|
|
504
|
+
|
|
505
|
+
Optional array of tags for categorizing and filtering usage in reports. Useful for tracking spend by feature, prompt version, or any other dimension relevant to your application.
|
|
506
|
+
|
|
507
|
+
Example: `tags: ['chat', 'v2']` will tag this request with "chat" and "v2" for filtering in usage analytics.
|
|
508
|
+
|
|
509
|
+
- **byok** _Record<string, Array<Record<string, unknown>>>_
|
|
510
|
+
|
|
511
|
+
Request-scoped BYOK (Bring Your Own Key) credentials to use for this request. When provided, any cached BYOK credentials configured in the gateway system are not considered. Requests may still fall back to use system credentials if the provided credentials fail.
|
|
512
|
+
|
|
513
|
+
Each provider can have multiple credentials (tried in order). The structure is a record where keys are provider slugs and values are arrays of credential objects.
|
|
514
|
+
|
|
515
|
+
Examples:
|
|
516
|
+
|
|
517
|
+
- Single provider: `byok: { 'anthropic': [{ apiKey: 'sk-ant-...' }] }`
|
|
518
|
+
- Multiple credentials: `byok: { 'vertex': [{ project: 'proj-1', googleCredentials: { privateKey: '...', clientEmail: '...' } }, { project: 'proj-2', googleCredentials: { privateKey: '...', clientEmail: '...' } }] }`
|
|
519
|
+
- Multiple providers: `byok: { 'anthropic': [{ apiKey: '...' }], 'bedrock': [{ accessKeyId: '...', secretAccessKey: '...' }] }`
|
|
520
|
+
|
|
521
|
+
- **zeroDataRetention** _boolean_
|
|
522
|
+
|
|
523
|
+
Restricts routing requests to providers that have zero data retention policies.
|
|
524
|
+
|
|
525
|
+
You can combine these options to have fine-grained control over routing and tracking:
|
|
526
|
+
|
|
527
|
+
```ts
|
|
528
|
+
import type { GatewayProviderOptions } from '@ai-sdk/gateway';
|
|
529
|
+
import { generateText } from 'ai';
|
|
530
|
+
|
|
531
|
+
const { text } = await generateText({
|
|
532
|
+
model: 'anthropic/claude-sonnet-4',
|
|
533
|
+
prompt: 'Write a haiku about programming',
|
|
534
|
+
providerOptions: {
|
|
535
|
+
gateway: {
|
|
536
|
+
order: ['vertex'], // Prefer Vertex AI
|
|
537
|
+
only: ['anthropic', 'vertex'], // Only allow these providers
|
|
538
|
+
} satisfies GatewayProviderOptions,
|
|
539
|
+
},
|
|
540
|
+
});
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
#### Model Fallbacks Example
|
|
544
|
+
|
|
545
|
+
The `models` option enables automatic fallback to alternative models when the primary model fails:
|
|
546
|
+
|
|
547
|
+
```ts
|
|
548
|
+
import type { GatewayProviderOptions } from '@ai-sdk/gateway';
|
|
549
|
+
import { generateText } from 'ai';
|
|
550
|
+
|
|
551
|
+
const { text } = await generateText({
|
|
552
|
+
model: 'openai/gpt-4o', // Primary model
|
|
553
|
+
prompt: 'Write a TypeScript haiku',
|
|
554
|
+
providerOptions: {
|
|
555
|
+
gateway: {
|
|
556
|
+
models: ['openai/gpt-5-nano', 'gemini-2.0-flash'], // Fallback models
|
|
557
|
+
} satisfies GatewayProviderOptions,
|
|
558
|
+
},
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
// This will:
|
|
562
|
+
// 1. Try openai/gpt-4o first
|
|
563
|
+
// 2. If it fails, try openai/gpt-5-nano
|
|
564
|
+
// 3. If that fails, try gemini-2.0-flash
|
|
565
|
+
// 4. Return the result from the first model that succeeds
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
#### Zero Data Retention Example
|
|
569
|
+
|
|
570
|
+
Set `zeroDataRetention` to true to ensure requests are only routed to providers
|
|
571
|
+
that have zero data retention policies. When `zeroDataRetention` is `false` or not
|
|
572
|
+
specified, there is no enforcement of restricting routing.
|
|
573
|
+
|
|
574
|
+
```ts
|
|
575
|
+
import type { GatewayProviderOptions } from '@ai-sdk/gateway';
|
|
576
|
+
import { generateText } from 'ai';
|
|
577
|
+
|
|
578
|
+
const { text } = await generateText({
|
|
579
|
+
model: 'anthropic/claude-sonnet-4.5',
|
|
580
|
+
prompt: 'Analyze this sensitive document...',
|
|
581
|
+
providerOptions: {
|
|
582
|
+
gateway: {
|
|
583
|
+
zeroDataRetention: true,
|
|
584
|
+
} satisfies GatewayProviderOptions,
|
|
585
|
+
},
|
|
586
|
+
});
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
### Provider-Specific Options
|
|
590
|
+
|
|
591
|
+
When using provider-specific options through AI Gateway, use the actual provider name (e.g. `anthropic`, `openai`, not `gateway`) as the key:
|
|
592
|
+
|
|
593
|
+
```ts
|
|
594
|
+
import type { AnthropicProviderOptions } from '@ai-sdk/anthropic';
|
|
595
|
+
import type { GatewayProviderOptions } from '@ai-sdk/gateway';
|
|
596
|
+
import { generateText } from 'ai';
|
|
597
|
+
|
|
598
|
+
const { text } = await generateText({
|
|
599
|
+
model: 'anthropic/claude-sonnet-4',
|
|
600
|
+
prompt: 'Explain quantum computing',
|
|
601
|
+
providerOptions: {
|
|
602
|
+
gateway: {
|
|
603
|
+
order: ['vertex', 'anthropic'],
|
|
604
|
+
} satisfies GatewayProviderOptions,
|
|
605
|
+
anthropic: {
|
|
606
|
+
thinking: { type: 'enabled', budgetTokens: 12000 },
|
|
607
|
+
} satisfies AnthropicProviderOptions,
|
|
608
|
+
},
|
|
609
|
+
});
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
This works with any provider supported by AI Gateway. Each provider has its own set of options - see the individual [provider documentation pages](/providers/ai-sdk-providers) for details on provider-specific options.
|
|
613
|
+
|
|
614
|
+
### Available Providers
|
|
615
|
+
|
|
616
|
+
AI Gateway supports routing to 20+ providers.
|
|
617
|
+
|
|
618
|
+
For a complete list of available providers and their slugs, see the [AI Gateway documentation](https://vercel.com/docs/ai-gateway/provider-options#available-providers).
|
|
619
|
+
|
|
620
|
+
## Model Capabilities
|
|
621
|
+
|
|
622
|
+
Model capabilities depend on the specific provider and model you're using. For detailed capability information, see:
|
|
623
|
+
|
|
624
|
+
- [AI Gateway provider options](https://vercel.com/docs/ai-gateway/provider-options#available-providers) for an overview of available providers
|
|
625
|
+
- Individual [AI SDK provider pages](/providers/ai-sdk-providers) for specific model capabilities and features
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/gateway",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.0-
|
|
4
|
+
"version": "0.0.0-4115c213-20260122152721",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "./dist/index.js",
|
|
@@ -9,8 +9,18 @@
|
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
10
|
"files": [
|
|
11
11
|
"dist/**/*",
|
|
12
|
-
"
|
|
12
|
+
"docs/**/*",
|
|
13
|
+
"src",
|
|
14
|
+
"!src/**/*.test.ts",
|
|
15
|
+
"!src/**/*.test-d.ts",
|
|
16
|
+
"!src/**/__snapshots__",
|
|
17
|
+
"!src/**/__fixtures__",
|
|
18
|
+
"CHANGELOG.md",
|
|
19
|
+
"README.md"
|
|
13
20
|
],
|
|
21
|
+
"directories": {
|
|
22
|
+
"doc": "./docs"
|
|
23
|
+
},
|
|
14
24
|
"exports": {
|
|
15
25
|
"./package.json": "./package.json",
|
|
16
26
|
".": {
|
|
@@ -20,15 +30,17 @@
|
|
|
20
30
|
}
|
|
21
31
|
},
|
|
22
32
|
"dependencies": {
|
|
23
|
-
"@vercel/oidc": "3.0
|
|
24
|
-
"@ai-sdk/provider": "
|
|
25
|
-
"@ai-sdk/provider-utils": "
|
|
33
|
+
"@vercel/oidc": "3.1.0",
|
|
34
|
+
"@ai-sdk/provider": "0.0.0-4115c213-20260122152721",
|
|
35
|
+
"@ai-sdk/provider-utils": "0.0.0-4115c213-20260122152721"
|
|
26
36
|
},
|
|
27
37
|
"devDependencies": {
|
|
28
38
|
"@types/node": "18.15.11",
|
|
29
39
|
"tsup": "^8",
|
|
40
|
+
"tsx": "4.19.2",
|
|
30
41
|
"typescript": "5.8.3",
|
|
31
42
|
"zod": "3.25.76",
|
|
43
|
+
"@ai-sdk/test-server": "0.0.0-4115c213-20260122152721",
|
|
32
44
|
"@vercel/ai-tsconfig": "0.0.0"
|
|
33
45
|
},
|
|
34
46
|
"peerDependencies": {
|
|
@@ -54,7 +66,8 @@
|
|
|
54
66
|
"scripts": {
|
|
55
67
|
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
|
|
56
68
|
"build:watch": "pnpm clean && tsup --watch",
|
|
57
|
-
"clean": "
|
|
69
|
+
"clean": "del-cli dist docs *.tsbuildinfo",
|
|
70
|
+
"generate-model-settings": "tsx scripts/generate-model-settings.ts",
|
|
58
71
|
"lint": "eslint \"./**/*.ts*\"",
|
|
59
72
|
"type-check": "tsc --build",
|
|
60
73
|
"prettier-check": "prettier --check \"./**/*.ts*\"",
|