AI Tools #OpenRouter#LLM API#LangChain

OpenRouter: One API for Every Major LLM

Use OpenRouter as a unified LLM gateway for Claude, GPT-4o, Gemini, and Llama with OpenAI-compatible endpoints and integrations.

7 min read

Managing API keys for five different AI providers while keeping track of five different pricing models, five different rate limit schemas, and five different SDK quirks is not a productive use of your time. OpenRouter solves this by acting as a unified gateway that presents a single OpenAI-compatible API endpoint while routing your requests to whichever model you choose — Claude, GPT-4o, Gemini, Llama, Mistral, and dozens more.

This guide covers how OpenRouter works, what it costs, and how to integrate it with LangChain and Continue.dev.

How OpenRouter Works

When you send a request to https://openrouter.ai/api/v1/chat/completions, OpenRouter authenticates you with a single API key, routes the request to the appropriate underlying provider, handles retries and fallbacks if a provider is degraded, and returns a response in the same format regardless of which model handled it.

From your application’s perspective, there is no difference between calling claude-3-5-sonnet and gpt-4o — the response envelope is identical. This makes provider switching a one-line config change rather than an SDK migration.

Supported Models

OpenRouter supports over 200 models. The most commonly used include:

Model IDProviderContext Window
anthropic/claude-3-5-sonnetAnthropic200K tokens
openai/gpt-4oOpenAI128K tokens
google/gemini-pro-1.5Google1M tokens
meta-llama/llama-3.3-70b-instructMeta (via hosted)128K tokens
mistralai/mixtral-8x7b-instructMistral32K tokens
qwen/qwen-2.5-72b-instructAlibaba128K tokens
deepseek/deepseek-r1DeepSeek64K tokens

The full model list is at openrouter.ai/models with real-time pricing and context lengths.

Pricing Model

OpenRouter charges per token at rates very close to (sometimes identical to) the underlying provider’s direct API pricing. You load credits into your OpenRouter account and draw them down as you make requests. There is no subscription fee.

A few things worth knowing:

  • Free tier: Several models are available at zero cost with rate limits. meta-llama/llama-3.2-3b-instruct:free is a good option for development testing.
  • Provider routing: If a provider has multiple hosting options, OpenRouter picks the cheapest by default unless you specify provider.order in your request.
  • Cost tracking: Every response includes usage metadata with token counts and cost in USD, making it easy to build budget alerting into your application.

Getting Your API Key

  1. Create an account at openrouter.ai.
  2. Navigate to KeysCreate Key.
  3. Set an optional credit limit per key for safety.
  4. Copy the key (it starts with sk-or-).

Export it in your shell:

export OPENROUTER_API_KEY="sk-or-v1-your-key-here"

Making Your First API Call

Because OpenRouter is OpenAI-compatible, you can use the openai Python SDK:

from openai import OpenAI

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

response = client.chat.completions.create(
    model="anthropic/claude-3-5-sonnet",
    messages=[
        {"role": "user", "content": "Explain transformer attention in 3 sentences."}
    ],
)

print(response.choices[0].message.content)

Switch the model string to openai/gpt-4o or google/gemini-pro-1.5 with zero other changes.

Using with curl

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "meta-llama/llama-3.3-70b-instruct",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Integrating with LangChain

LangChain has built-in support for OpenAI-compatible endpoints. Use ChatOpenAI with a custom base_url:

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="anthropic/claude-3-5-sonnet",
    openai_api_key="sk-or-v1-your-key-here",
    openai_api_base="https://openrouter.ai/api/v1",
    default_headers={
        "HTTP-Referer": "https://yourapp.com",
        "X-Title": "Your App Name",
    }
)

result = llm.invoke("What are the main differences between RAG and fine-tuning?")
print(result.content)

The HTTP-Referer and X-Title headers are optional but recommended — they appear in the OpenRouter dashboard and help with debugging.

Building a RAG Pipeline with OpenRouter

from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.chains import RetrievalQA

# Use a cheap embedding model for vectors
embeddings = OpenAIEmbeddings(
    model="openai/text-embedding-3-small",
    openai_api_key="sk-or-v1-your-key-here",
    openai_api_base="https://openrouter.ai/api/v1",
)

# Use a powerful model for generation
llm = ChatOpenAI(
    model="google/gemini-pro-1.5",
    openai_api_key="sk-or-v1-your-key-here",
    openai_api_base="https://openrouter.ai/api/v1",
)

Integrating with Continue.dev

Continue.dev is the open-source AI coding assistant for VS Code and JetBrains that lets you configure any model backend. Add OpenRouter as a provider in ~/.continue/config.json:

{
  "models": [
    {
      "title": "Claude 3.5 Sonnet (OpenRouter)",
      "provider": "openai",
      "model": "anthropic/claude-3-5-sonnet",
      "apiBase": "https://openrouter.ai/api/v1",
      "apiKey": "sk-or-v1-your-key-here"
    },
    {
      "title": "Llama 3.3 70B (OpenRouter)",
      "provider": "openai",
      "model": "meta-llama/llama-3.3-70b-instruct",
      "apiBase": "https://openrouter.ai/api/v1",
      "apiKey": "sk-or-v1-your-key-here"
    }
  ]
}

You can now switch between any model from the Continue.dev sidebar dropdown without reconfiguring anything else.

Model Routing and Fallbacks

OpenRouter supports automatic fallback routing if a primary provider is down:

response = client.chat.completions.create(
    model="anthropic/claude-3-5-sonnet",
    messages=[{"role": "user", "content": "Hello"}],
    extra_body={
        "provider": {
            "order": ["Anthropic", "AWS Bedrock"],
            "allow_fallbacks": True
        }
    }
)

This retries on AWS Bedrock if Anthropic’s direct API is degraded — useful for production applications where uptime matters.

When to Use OpenRouter vs Direct APIs

Use OpenRouter when you are experimenting with multiple models, building an application that needs to switch providers without code changes, or want unified billing and cost tracking across models. Use direct provider APIs when you need features specific to one provider (like Anthropic’s extended thinking budget parameters) or when you are at a scale where the marginal markup matters.

For most developers and indie projects, OpenRouter’s convenience is worth far more than any per-token overhead.

#AI gateway #Continue.dev #LangChain #LLM API #OpenRouter