OpenRouter
The unified API gateway to hundreds of AI models from one endpoint, with intelligent routing, fallbacks, and a generous free tier.
Introduction
OpenRouter is a unified API gateway that provides access to hundreds of AI models through a single OpenAI-compatible endpoint. Instead of managing individual API keys, providers, and billing for each model provider separately, developers use one API key and one endpoint to reach models from Anthropic, Google, Meta, Mistral, DeepSeek, OpenAI, and dozens of others.
OpenRouter handles the complexity of provider routing, failover, and billing behind the scenes. It is one of the most popular model aggregators, known for its generous free tier, intelligent routing features, and comprehensive developer tooling including official Client SDKs (TypeScript, Python, Go) and an Agent SDK for building AI agents with tool use and multi-turn loops.
Because OpenRouter exposes a fully OpenAI-compatible API, any existing code written for the OpenAI SDK can be pointed at OpenRouter by simply changing the baseURL to https://openrouter.ai/api/v1, making migration near-instant.
Key Features
- 300+ Models, One Endpoint - Access models from Anthropic, OpenAI, Google, Meta, Mistral, DeepSeek, and dozens more through a single API. Browse the full catalog at openrouter.ai/models.
- Model Fallbacks - Automatically fails over to alternative providers or models when the primary provider returns an error, ensuring production resilience.
- Provider Selection - Choose specific providers for each model or let OpenRouter pick the cheapest, fastest, or most reliable option automatically.
- Free Tier - Multiple free models available with rate limits, including a dedicated Free Models Router. New users receive a small free allowance to test the platform.
- Agent SDK - Official
@openrouter/agentpackage for building AI agents with built-in tool loops, state management, and stop conditions. - Client SDKs - Full type-safe SDKs for TypeScript, Python, and Go with zero boilerplate, auto-generated from the OpenAPI spec.
- Workspaces and Budgets - Organize projects into workspaces with individual API keys, usage limits, and spending controls.
- Structured Outputs - Guarantee JSON responses matching a schema, with response healing for robustness.
- Tool Calling - Native function-calling support compatible with the OpenAI tool-calling format across supported models.
- App Attribution - Set site URL and app name headers to appear on the OpenRouter leaderboards and rankings.
Model Access and Routing
OpenRouter's core value proposition is intelligent model routing. Instead of hardcoding a single model and provider, you configure routing rules that let OpenRouter automatically select the best provider for each request based on availability, price, or latency.
Provider Selection
Many models on OpenRouter are served by multiple providers. You can specify which provider to use by passing the provider parameter, or let OpenRouter choose automatically. Configurable ordering strategies include cheapest, fastest, random, and provider-defined priority. You can also filter providers by a required ignore list to exclude specific backends.
Auto Exacto
Auto Exacto is OpenRouter's smart routing feature that automatically selects the best model for your use case based on the prompt and desired output. Instead of specifying a model, you describe what you need and Auto Exacto picks the most capable and cost-effective model from the catalog for each request.
Model Fallbacks
If a provider returns an error, is overloaded, or experiences an outage, OpenRouter automatically falls back to the next available provider for that model. This happens transparently to the caller and makes production applications much more resilient. You can configure ordered fallback lists to control the failover behavior. This is documented in full on the Model Fallbacks guide.
Private Models and Variants
OpenRouter supports hosting and routing to private models, allowing teams to deploy custom fine-tunes or proprietary models alongside public ones. Model variants let you pin specific versions or configurations of a model, ensuring consistent behavior across requests.
Getting Started
Getting started with OpenRouter takes minutes. Sign up at openrouter.ai, generate an API key, and start making requests.
Direct API (curl)
curl https://openrouter.ai/api/v1/chat/completions \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o",
"messages": [
{"role": "user", "content": "What is the meaning of life?"}
]
}'
Python (requests)
import requests
import json
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": "Bearer ",
"HTTP-Referer": "",
"X-OpenRouter-Title": "",
},
data=json.dumps({
"model": "openai/gpt-4o",
"messages": [
{"role": "user", "content": "What is the meaning of life?"}
]
})
)
print(response.json()["choices"][0]["message"]["content"])
Client SDK (TypeScript)
npm install @openrouter/sdk
import { OpenRouter } from '@openrouter/sdk';
const client = new OpenRouter({
apiKey: '',
httpReferer: '',
});
const completion = await client.chat.send({
model: 'openai/gpt-4o',
messages: [
{ role: 'user', content: 'What is the meaning of life?' }
],
});
console.log(completion.choices[0].message.content);
OpenAI SDK Compatibility
Use any existing OpenAI SDK code by pointing it at OpenRouter's endpoint. No code changes beyond the base URL and API key:
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://openrouter.ai/api/v1',
apiKey: '',
defaultHeaders: {
'HTTP-Referer': '',
'X-OpenRouter-Title': '',
},
});
const completion = await openai.chat.completions.create({
model: 'openai/gpt-4o',
messages: [{ role: 'user', content: 'Hello!' }],
});
Client SDKs (Python)
pip install openrouter-sdk
from openrouter import OpenRouter
client = OpenRouter(
api_key="",
http_referer="",
)
completion = client.chat.send(
model="openai/gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
print(completion.choices[0].message.content)
The HTTP-Referer and X-OpenRouter-Title headers are optional but recommended. They enable your app to appear on OpenRouter leaderboards and rankings through the App Attribution system.
Agent SDK and Tools
OpenRouter's official Agent SDK (@openrouter/agent) provides higher-level primitives for building AI agents. It handles multi-turn conversation loops, tool execution, and state management automatically via the callModel function.
Installation
npm install @openrouter/agent
Building an Agent with Tools
import { callModel, tool } from '@openrouter/agent';
import { z } from 'zod';
const weatherTool = tool({
name: 'get_weather',
description: 'Get the current weather for a location',
inputSchema: z.object({
location: z.string().describe('City name'),
}),
execute: async ({ location }) => {
return { temperature: 72, condition: 'sunny', location };
},
});
const result = await callModel({
model: 'anthropic/claude-sonnet-4',
messages: [
{ role: 'user', content: 'What is the weather in San Francisco?' },
],
tools: [weatherTool],
});
const text = await result.getText();
console.log(text);
The SDK sends the prompt, receives a tool call from the model, executes the tool function, feeds the result back, and returns the final response — all in one callModel invocation. The loop repeats automatically until a stop condition is met.
Stop Conditions
Control when the agent loop terminates with built-in stop conditions:
import { callModel, stepCountIs, maxCost } from '@openrouter/agent';
const result = await callModel({
model: 'anthropic/claude-sonnet-4',
messages: [{ role: 'user', content: 'Research this topic thoroughly' }],
tools: [searchTool],
stopWhen: [stepCountIs(10), maxCost(0.50)],
});
Agent SDK vs Client SDKs
| Dimension | Agent SDK | Client SDKs |
|---|---|---|
| Focus | Agentic primitives — multi-turn loops, tools, stop conditions | Lean API client — mirrors the REST API with full type safety |
| Use when | You want built-in agent loops, tool execution, and state management | You want direct model calls and manage orchestration yourself |
| Conversation state | Managed for you via callModel | You manage it |
| Tool execution | Automatic with the tool() helper | You dispatch tool calls |
| Languages | TypeScript | TypeScript, Python, Go |
Features for Developers
Beyond simple chat completion, OpenRouter offers a comprehensive suite of developer features for building production applications:
Structured Outputs
OpenRouter supports guaranteed structured JSON responses from supported models. Define a JSON schema and the model returns output that conforms. A response healing mechanism automatically retries with a more specific prompt if the initial response doesn't match the schema, ensuring reliable structured data extraction.
Tool Calling (Function Calling)
Native tool calling support compatible with the OpenAI function-calling format. Define tools with JSON Schema parameters, and OpenRouter routes the tool calls and responses through compatible models. This works with the Agent SDK's tool() helper or directly through the REST API.
Response Caching
OpenRouter offers response caching (beta) to serve identical requests from cache, reducing latency and cost for repeated prompts. This is particularly useful for production workloads with predictable query patterns.
Guardrails
OpenRouter supports configurable guardrails that can filter input and output content for safety, moderation, or compliance requirements. These can be customized per workspace or application.
Server Tools and Plugins
OpenRouter supports server-side tool definitions and plugins that extend model capabilities. Server tools are predefined tools hosted on OpenRouter's infrastructure, while plugins allow third-party extensions to the routing and processing pipeline.
Message Transforms
Message transforms allow you to modify messages before they reach the model. This is useful for injecting system prompts, redacting sensitive information, or reformatting content for specific model requirements.
Service Tiers
OpenRouter offers different service tiers that provide varying levels of throughput, priority, and support. Choose a tier that matches your production requirements and budget.
Workspaces and Budgets
Workspaces let you organize projects into isolated environments with their own API keys, usage tracking, and spending limits. Workspace budgets allow you to set hard caps on spending per workspace, preventing runaway costs in production.
Presets and Custom Classifiers
Presets let you save and reuse model configurations including system prompts, temperature, top_p, and other parameters. Custom classifiers allow you to define content classification rules that run alongside your model requests.
Additional Features
- Zero-Drift Mode (ZDR) - Ensures deterministic model outputs across requests by controlling temperature, seed, and other sampling parameters.
- Sovereign AI - Route requests through providers in specific geographic regions for data residency compliance.
- Broadcast Mode - Send the same request to multiple models simultaneously and compare responses.
- MCP Support - Model Context Protocol integration for connecting to external tools and data sources.
- Stripe Projects - Enterprise billing integration with Stripe for team-based spending and invoicing.
- Router Metadata - Information about which provider served your request, latency, and routing decisions.
Pricing and Free Tier
OpenRouter uses a credit-based billing system. You purchase credits, and usage is deducted per request based on the model's pricing. The platform is known for one of the most generous free tiers in the model aggregation space.
Free Tier
- Free Allowance - New users receive a small free credit allowance to test the platform and explore different models.
- Free Models - Multiple free models are available at any time, accessible through the dedicated
openrouter/freemodel router that automatically selects a free model for your requests. - Rate Limits - Free models have lower rate limits (50 requests per day total for new users). Users who have purchased at least 10 credits get 1,000 requests per day on free models.
Pay-As-You-Go Pricing
- Credit Purchase - Buy credits in any amount. OpenRouter charges a 5.5% fee (minimum $0.80) on credit purchases via standard payment methods. Crypto payments incur a 5% fee.
- Pass-Through Pricing - OpenRouter passes through the underlying model provider's pricing without markup. You pay the same per-token rate as you would going directly to the provider.
- BYOK (Bring Your Own Key) - Use your own provider API keys through OpenRouter. There is no additional fee for BYOK traffic.
- Volume Discounts - Higher usage volumes may qualify for discounted pricing. Contact OpenRouter for enterprise volume pricing.
Billing Features
- Usage Monitoring - Track credit usage per model, per workspace, and over time through the dashboard and API.
- Refund Policy - Unused credits are refundable. Check the current terms for details.
- Stripe Projects - Enterprise customers can integrate with Stripe for team billing, invoices, and spending controls.
Comparison with Alternatives
| Feature | OpenRouter | Together AI | Groq | Native Provider APIs |
|---|---|---|---|---|
| Model count | 300+ (aggregated) | 100+ (hosted) | ~10 (hosted) | Varies per provider |
| API compatibility | OpenAI-compatible | OpenAI-compatible | OpenAI-compatible | Proprietary per provider |
| Model fallbacks | Automatic multi-provider | Not available | Not available | Not available |
| Free tier | Free models + credit allowance | Limited free tier | Generous free tier | Minimal or none |
| Provider selection | Auto, cheapest, fastest | Single provider | Single provider | N/A |
| Agent SDK | Official (TypeScript) | None | None | Provider-specific |
| Client SDKs | TypeScript, Python, Go | Python, TypeScript | Python, JS | Per provider |
| Structured outputs | Yes (with healing) | Basic | No | Varies |
| Workspaces/Budgets | Full support | Limited | No | Varies |
| Inference speed | Depends on provider | Fast (optimized infra) | Very fast (LPU tech) | Depends on provider |