OpenRouter Provider
OpenRouter provides access to 400+ AI models from dozens of providers through a single API. This is ideal for:
- Multi-model workflows - Switch between Claude, GPT, Llama, and more
- Cost optimization - Route to the cheapest available provider
- Fallback resilience - Automatic failover when models are unavailable
- Unified billing - Single API key for all providers
Set your OpenRouter API key:
export OPENROUTER_API_KEY=sk-or-...Optional analytics configuration:
export OPENROUTER_SITE_URL="https://myapp.com" # Your app URLexport OPENROUTER_APP_NAME="MyApp" # Your app nameModel Format
Section titled “Model Format”OpenRouter models use the format provider/model-name:
openrouter:anthropic/claude-sonnet-4-5openrouter:openai/gpt-4oopenrouter:deepseek/deepseek-chatopenrouter:meta-llama/llama-3.3-70b-instructPopular Models
Section titled “Popular Models”| Model | OpenRouter ID | Best For |
|---|---|---|
| Claude Sonnet 4.5 | anthropic/claude-sonnet-4-5 | Balanced performance |
| GPT-4o | openai/gpt-4o | General tasks, vision |
| DeepSeek Chat (V3) | deepseek/deepseek-chat | Cost-effective coding |
| DeepSeek R1 | deepseek/deepseek-r1 | Complex reasoning |
| Llama 3.3 70B | meta-llama/llama-3.3-70b-instruct | Open-source option |
| Gemini 2.5 Flash | google/gemini-2.5-flash | Fast, long context |
Quick Aliases
Section titled “Quick Aliases”llmist provides shortcuts for common OpenRouter models:
| Alias | Full Model |
|---|---|
or:sonnet | openrouter:anthropic/claude-sonnet-4-5 |
or:opus | openrouter:anthropic/claude-opus-4-5 |
or:haiku | openrouter:anthropic/claude-haiku-4-5 |
or:gpt4o | openrouter:openai/gpt-4o |
or:gpt5 | openrouter:openai/gpt-5.2 |
or:flash | openrouter:google/gemini-2.5-flash |
or:llama | openrouter:meta-llama/llama-3.3-70b-instruct |
or:deepseek | openrouter:deepseek/deepseek-r1 |
Usage Examples
Section titled “Usage Examples”import { LLMist } from 'llmist';
const answer = await LLMist.createAgent() .withModel('openrouter:deepseek/deepseek-chat') .askAndCollect('Explain async/await in JavaScript');import { LLMist } from 'llmist';
// Use the or: prefix for quick accessconst answer = await LLMist.createAgent() .withModel('or:sonnet') .askAndCollect('Write a haiku about coding');# Full model IDnpx @llmist/cli complete "Hello" --model openrouter:deepseek/deepseek-chat
# Using aliasnpx @llmist/cli agent "Review my code" --model or:sonnetRouting Options
Section titled “Routing Options”OpenRouter supports intelligent routing to optimize for cost, speed, or quality:
import { LLMist } from 'llmist';
const answer = await LLMist.createAgent() .withModel('openrouter:deepseek/deepseek-chat') .withExtra({ routing: { route: 'cheapest', // 'cheapest' | 'fastest' | 'quality' }, }) .askAndCollect('Hello!');Route Options
Section titled “Route Options”| Option | Description |
|---|---|
quality | Best quality provider (default) |
cheapest | Lowest cost provider |
fastest | Lowest latency provider |
Model Fallbacks
Section titled “Model Fallbacks”Configure automatic fallback to alternative models:
import { LLMist } from 'llmist';
const answer = await LLMist.createAgent() .withModel('openrouter:anthropic/claude-sonnet-4-5') .withExtra({ routing: { // Try these models in order if primary is unavailable models: [ 'anthropic/claude-sonnet-4-5', 'openai/gpt-4o', 'deepseek/deepseek-chat', ], }, }) .askAndCollect('Complex analysis task...');Provider Routing
Section titled “Provider Routing”Route to a specific provider when a model is available from multiple sources:
const answer = await LLMist.createAgent() .withModel('openrouter:meta-llama/llama-3.3-70b-instruct') .withExtra({ routing: { provider: 'Together', // Route to Together AI // Or specify provider preference order: // order: ['Together', 'Fireworks', 'Anyscale'], }, }) .askAndCollect('Hello!');Manual Configuration
Section titled “Manual Configuration”For advanced setups, configure the provider manually:
import { LLMist, OpenRouterProvider } from 'llmist';import OpenAI from 'openai';
const openRouterClient = new OpenAI({ apiKey: process.env.OPENROUTER_API_KEY, baseURL: 'https://openrouter.ai/api/v1',});
const client = new LLMist({ autoDiscoverProviders: false, adapters: [ new OpenRouterProvider(openRouterClient, { siteUrl: 'https://myapp.com', appName: 'MyApp', }), ],});When to Use OpenRouter
Section titled “When to Use OpenRouter”- Access models not directly supported (Llama, Mistral, Qwen, etc.)
- Need automatic failover between providers
- Want unified billing across multiple models
- Optimizing for cost with dynamic routing
- Testing different models quickly
- Maximum performance (lowest latency)
- Need provider-specific features
- Have negotiated enterprise pricing
- Compliance requires direct API access
Prompt Caching
Section titled “Prompt Caching”llmist automatically enables prompt caching for OpenRouter requests. This works with both Anthropic Claude and Google Gemini models routed through OpenRouter, saving up to 75% on cached input tokens.
How It Works
Section titled “How It Works”llmist adds cache_control breakpoints to the last system message and last user message in each request. OpenRouter uses sticky routing to maximize cache hits by sending subsequent requests to the same provider endpoint.
For models with implicit caching (Gemini 2.5+, OpenAI, DeepSeek), caching happens server-side automatically — no breakpoints needed. llmist extracts and reports cached token counts from the API response in both cases.
Disable Caching
Section titled “Disable Caching”If needed, disable caching explicitly:
const answer = await LLMist.createAgent() .withModel('openrouter:anthropic/claude-sonnet-4-5') .withoutCaching() .askAndCollect('One-off question');Cost Tracking
Section titled “Cost Tracking”OpenRouter passes through usage information, including cached token counts:
for await (const event of agent.run()) { if (event.type === 'llm_call_complete') { console.log('Input tokens:', event.usage?.promptTokens); console.log('Output tokens:', event.usage?.completionTokens); console.log('Estimated cost:', event.cost); }}Error Handling
Section titled “Error Handling”The OpenRouter provider includes enhanced error messages:
| Error | Meaning |
|---|---|
| 401 | Invalid API key - check OPENROUTER_API_KEY |
| 402 | Insufficient credits - add funds at openrouter.ai/credits |
| 429 | Rate limit exceeded - reduce request frequency |
| 503 | Model unavailable - try a different model or use fallbacks |
Reasoning Models via OpenRouter
Section titled “Reasoning Models via OpenRouter”OpenRouter provides access to reasoning-capable models like DeepSeek R1, which excels at math, logic, and coding with chain-of-thought reasoning:
const answer = await LLMist.createAgent() .withModel('openrouter:deepseek/deepseek-r1') .askAndCollect('Prove there are infinitely many primes.');