Use the AI SDK Gateway provider to access multiple AI models and providers through a unified interface. Supports model routing, text generation, streaming, and object generation with type safety.
Use the AI SDK Gateway provider to access a wide variety of AI models and providers through a unified interface.
This skill helps you integrate the AI SDK Gateway provider into your Node.js/TypeScript projects. The Gateway provider allows you to route requests to multiple AI providers (OpenAI, Anthropic, xAI, Google, etc.) using a single consistent API.
First, install the Gateway provider package:
```bash
npm i @ai-sdk/gateway
```
You'll also need the core AI SDK if not already installed:
```bash
npm i ai
```
```typescript
import { gateway } from '@ai-sdk/gateway';
import { generateText } from 'ai';
```
```typescript
const { text } = await generateText({
model: gateway('xai/grok-3-beta'),
prompt: 'Tell me about the history of the San Francisco Mission-style burrito.',
});
console.log(text);
```
The Gateway provider uses the format `provider/model-name` to route requests:
```typescript
import { streamText } from 'ai';
const { textStream } = await streamText({
model: gateway('anthropic/claude-3-5-sonnet-20241022'),
prompt: 'Write a short story about space exploration.',
});
for await (const chunk of textStream) {
process.stdout.write(chunk);
}
```
```typescript
import { generateObject } from 'ai';
import { z } from 'zod';
const { object } = await generateObject({
model: gateway('openai/gpt-4o'),
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(z.string()),
steps: z.array(z.string()),
}),
}),
prompt: 'Generate a recipe for chocolate chip cookies.',
});
console.log(object.recipe);
```
1. **Install dependencies**
```bash
npm i @ai-sdk/gateway ai
```
2. **Import the gateway provider**
```typescript
import { gateway } from '@ai-sdk/gateway';
```
3. **Choose your AI function** (`generateText`, `streamText`, `generateObject`, etc.)
4. **Specify the model** using the `provider/model-name` format
5. **Provide your prompt or schema** and handle the response
6. **Add error handling** for network issues or API failures
Set up API keys for the providers you'll use:
```bash
ANTHROPIC_API_KEY=your_key_here
OPENAI_API_KEY=your_key_here
XAI_API_KEY=your_key_here
```
```typescript
async function generateWithFallback(prompt: string) {
const models = [
'openai/gpt-4o',
'anthropic/claude-3-5-sonnet-20241022',
'xai/grok-3-beta',
];
for (const model of models) {
try {
return await generateText({ model: gateway(model), prompt });
} catch (error) {
console.error(`Failed with ${model}, trying next...`);
}
}
throw new Error('All providers failed');
}
```
For complete documentation, visit [ai-sdk.dev/docs](https://ai-sdk.dev/docs)
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/ai-sdk-gateway-provider/raw