content/providers/03-community-providers/32-openrouter.mdx
OpenRouter is a unified API gateway that provides access to hundreds of AI models from leading providers like Anthropic, Google, Meta, Mistral, and more. The OpenRouter provider for the AI SDK enables seamless integration with all these models while offering unique advantages:
Learn more about OpenRouter's capabilities in the OpenRouter Documentation.
The OpenRouter provider is available in the @openrouter/ai-sdk-provider module. You can install it with:
<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}> <Tab> <Snippet text="pnpm add @openrouter/ai-sdk-provider" dark /> </Tab> <Tab> <Snippet text="npm install @openrouter/ai-sdk-provider" dark /> </Tab> <Tab> <Snippet text="yarn add @openrouter/ai-sdk-provider" dark /> </Tab>
<Tab> <Snippet text="bun add @openrouter/ai-sdk-provider" dark /> </Tab> </Tabs>To create an OpenRouter provider instance, use the createOpenRouter function:
import { createOpenRouter } from '@openrouter/ai-sdk-provider';
const openrouter = createOpenRouter({
apiKey: 'YOUR_OPENROUTER_API_KEY',
});
You can obtain your OpenRouter API key from the OpenRouter Dashboard.
OpenRouter supports both chat and completion models. Use openrouter.chat() for chat models and openrouter.completion() for completion models:
// Chat models (recommended)
const chatModel = openrouter.chat('anthropic/claude-3.5-sonnet');
// Completion models
const completionModel = openrouter.completion(
'meta-llama/llama-3.1-405b-instruct',
);
You can find the full list of available models in the OpenRouter Models documentation.
Here are examples of using OpenRouter with the AI SDK:
generateTextimport { createOpenRouter } from '@openrouter/ai-sdk-provider';
import { generateText } from 'ai';
const openrouter = createOpenRouter({
apiKey: 'YOUR_OPENROUTER_API_KEY',
});
const { text } = await generateText({
model: openrouter.chat('anthropic/claude-3.5-sonnet'),
prompt: 'What is OpenRouter?',
});
console.log(text);
streamTextimport { createOpenRouter } from '@openrouter/ai-sdk-provider';
import { streamText } from 'ai';
const openrouter = createOpenRouter({
apiKey: 'YOUR_OPENROUTER_API_KEY',
});
const result = streamText({
model: openrouter.chat('meta-llama/llama-3.1-405b-instruct'),
prompt: 'Write a short story about AI.',
});
for await (const chunk of result.textStream) {
console.log(chunk);
}
OpenRouter offers several advanced features to enhance your AI applications:
Model Flexibility: Switch between hundreds of models without changing your code or managing multiple API keys.
Cost Management: Track usage and costs per model in real-time through the dashboard.
Enterprise Support: Available for high-volume users with custom SLAs and dedicated support.
Cross-Provider Compatibility: Use the same code structure across different model providers.
Regular Updates: Automatic access to new models and features as they become available.
For more information about these features and advanced configuration options, visit the OpenRouter Documentation.