OpenRouter

OpenRouter — User Guide

One API for many models.

Visit website VPN may be required Freemium Sign-up required
Strengths
  • One API Key to access 200+ AI models
  • Automatic routing to the cheapest or fastest available provider
  • Real-time price comparison, transparent fee display
  • Support OpenAI compatible API format
  • Some models are completely free
Best for
  • Unified management of API calls for multiple AI models
  • Automatically select the cheapest model provider
  • Test and compare the output of different models
  • Build applications that support multi-model switching
  • Access models that cannot be called directly in China

quick start

OpenRouter uses an OpenAI compatible format, requiring little modification to existing code.

Scenario

Call any model

Prompt example
from openai import OpenAI

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key="your-openrouter-key"
)

# You can call any model, just modify the model parameters
response = client.chat.completions.create(
    model="anthropic/claude-3.5-sonnet", # or any other model
    messages=[{"role": "user", "content": "Hello"}],
    extra_headers={
        "HTTP-Referer": "https://your-site.com",
        "X-Title": "Your App Name"
    }
)
print(response.choices[0].message.content)

# Just change one line to switch models
# model="openai/gpt-4o"
# model="google/gemini-pro-1.5"
# model="meta-llama/llama-3.1-70b-instruct"
Output / what to expect

One API Key to access all models,

To switch models, just modify the model parameters.

Billing is managed in OpenRouter.

Tips

At openrouter.ai/models you can view real-time prices for all models and choose the one that suits you best.

Scenario

Use free models

Prompt example
Free model on #OpenRouter (2025)
free_models = [
    "google/gemini-flash-1.5-8b", # Google Free
    "meta-llama/llama-3.2-3b-instruct:free", # Meta free
    "mistralai/mistral-7b-instruct:free", #Mistral free
    "deepseek/deepseek-r1:free", # DeepSeek free
]

# Use free models
response = client.chat.completions.create(
    model="deepseek/deepseek-r1:free",
    messages=[{"role": "user", "content": "Explaining Machine Learning"}]
)
Output / what to expect

Free models have rate limits,

But it is completely sufficient for testing and low-frequency use.

DeepSeek R1 free version is of high quality.

Tips

Free models usually have stricter rate limits, and paid models are recommended for production applications.

Automatic routing and cost optimization

OpenRouter automatically selects the cheapest provider, reducing API costs.

Scenario

Use the automatic routing feature

Prompt example
# Use "auto" routing, OpenRouter automatically selects the optimal model
response = client.chat.completions.create(
    model="openrouter/auto",
    messages=[{"role": "user", "content": "Write a poem"}]
)

# View the actual used model
print(response.model)

#Set cost cap
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
    extra_body={
        "provider": {
            "order": ["OpenAI", "Azure"], # Priority order
            "allow_fallbacks": True # Allow downgrades
        }
    }
)
Output / what to expect

Automatic routing selects the cheapest or fastest available provider at the moment,

Automatically switches to backup when primary provider is unavailable,

Improve application reliability.

Tips

For production applications, it is recommended to set allow_fallbacks=True to avoid service interruption caused by a single provider failure.

Compared with similar tools

ToolStrengthBest forPricing
OpenRouter This toolUnified access to all models, automatic price comparison, some freeNeed to use multiple models and want to manage the API in a unified wayPay as you use (transparent pricing)
OpenAI APIDirect access with minimal latencyOnly use OpenAI models to pursue the lowest latencyPay by token
Together AIOpen source models are cheaperExtensive use of open source modelsPay by token
GroqThe fastest reasoning speedExtremely high speed requirementsFree quota/paid version

Sources & references: