OpenRouter
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:
- Universal Model Access: One API key for hundreds of models from multiple providers
- Cost-Effective: Pay-as-you-go pricing with no monthly fees or commitments
- Transparent Pricing: Clear per-token costs for all models
- High Availability: Enterprise-grade infrastructure with automatic failover
- Simple Integration: Standardized API across all models
- Latest Models: Immediate access to new models as they're released
Learn more about OpenRouter's capabilities in the OpenRouter Documentation.
Setup
The OpenRouter provider is available in the @openrouter/ai-sdk-provider
module. You can install it with:
pnpm add @openrouter/ai-sdk-provider
Provider Instance
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.
Language Models
OpenRouter supports both chat and completion models. Use openrouter.chatModel()
for chat models and openrouter.completionModel()
for completion models:
// Chat models (recommended)const chatModel = openrouter.chatModel('anthropic/claude-3.5-sonnet');
// Completion modelsconst completionModel = openrouter.completionModel( 'meta-llama/llama-3.1-405b-instruct',);
You can find the full list of available models in the OpenRouter Models documentation.
Examples
Here are examples of using OpenRouter with the AI SDK:
generateText
import { createOpenRouter } from '@openrouter/ai-sdk-provider';import { generateText } from 'ai';
const openrouter = createOpenRouter({ apiKey: 'YOUR_OPENROUTER_API_KEY',});
const { text } = await generateText({ model: openrouter.chatModel('anthropic/claude-3.5-sonnet'), prompt: 'What is OpenRouter?',});
console.log(text);
streamText
import { createOpenRouter } from '@openrouter/ai-sdk-provider';import { streamText } from 'ai';
const openrouter = createOpenRouter({ apiKey: 'YOUR_OPENROUTER_API_KEY',});
const result = streamText({ model: openrouter.chatModel('meta-llama/llama-3.1-405b-instruct'), prompt: 'Write a short story about AI.',});
for await (const chunk of result) { console.log(chunk);}
Advanced Features
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.