Skip to content

How to Connect Any Model Through OpenRouter

Summary

OpenRouter is a unified access point to hundreds of models: DeepSeek, Claude, Llama, Qwen, and others. One API key, one request format, and model switching with just one line of code.

What is OpenRouter and Why Use It

Typically, each model requires its own account and API key: separate for DeepSeek, Anthropic, and OpenAI. OpenRouter consolidates them under one interface:

  • One key for all models;
  • One request format (OpenAI-compatible);
  • Model switching = changing one line of model;
  • View prices and ratings of models in the catalog.

This is convenient when comparing models or switching between them without rewriting code.

1. Registration and Key

  1. Go to openrouter.ai.
  2. Register (via Google or GitHub).
  3. Open the Keys section and create a key.
  4. Top up your balance — payment is based on the tokens actually used.

The key looks like sk-or-v1-.... Save it: it won't be shown again.

2. Library Installation

OpenRouter works through the OpenAI SDK:

pip install openai

3. First Request

from openai import OpenAI

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

response = client.chat.completions.create(
    model="deepseek/deepseek-chat",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain what RAG is."},
    ],
)

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

Note: The model name is specified with a provider prefix — deepseek/deepseek-chat, anthropic/claude-3.5-sonnet, meta-llama/llama-3.1-70b-instruct.

Model Provider Purpose
deepseek/deepseek-chat DeepSeek Affordable universal chat
deepseek/deepseek-r1 DeepSeek Reasoning
anthropic/claude-3.5-sonnet Anthropic Long context, text generation
meta-llama/llama-3.1-70b-instruct Meta Open model
qwen/qwen-2.5-72b-instruct Alibaba Strong open model
mistralai/mistral-large Mistral European flagship

Full catalog with prices — on the OpenRouter website in the Models section.

5. Switch Models with One Line

The magic is in the model parameter:

def ask(model: str, prompt: str) -> str:
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}],
    )
    return response.choices[0].message.content

print(ask("deepseek/deepseek-chat", "What is a token?"))
print(ask("anthropic/claude-3.5-sonnet", "What is a token?"))

The same code works with any model — just pass a different name.

6. Useful Parameters

  • temperature — from 0 to 1, creativity of the response;
  • max_tokens — limit the length of the response;
  • extra_headers — OpenRouter utility headers (e.g., routing).

Example with length limitation:

response = client.chat.completions.create(
    model="deepseek/deepseek-chat",
    messages=[{"role": "user", "content": "Tell me about transformers"}],
    temperature=0.3,
    max_tokens=500,
)

More about temperature — in the article Temperature.

7. Track Expenses

Expenses are visible in the OpenRouter dashboard: in the Activity section — each request, model, tokens, and cost. This is convenient for budget control without custom analytics.

OpenRouter vs Direct API

OpenRouter Direct API
One key for all models Yes No, each has its own
Price Same + small markup Direct
Model switching One line Whole code changes
Access to new models Instant Requires provider registration

When to Choose OpenRouter

  • Need to compare models in one project;
  • Want one key and one format;
  • Don't want to create accounts with each provider.

When you need the lowest price and maximum control — a direct API DeepSeek or another specific provider is better.

See Also