Short answer
OpenRouter is a single API that forwards your request to models from many providers, so you can switch models by changing one text string instead of signing up, paying and coding separately for each company. Because it follows OpenAI's API format, you can use the ordinary openai Python library: point it at OpenRouter's address, use an OpenRouter key, and name the model as provider/model.
Follow-up question How do you compare cost and quality across them?
If you build anything with language models, you soon want to try more than one. Maybe a small open model is good enough for your task at a fraction of the price, or another company's model writes better French. Normally that means a separate account, a separate key, a separate bill and often a separate code library for each provider.
OpenRouter removes most of that work. It is a service that sits between your code and many model providers: you send every request to OpenRouter, say which model you want, and it passes the request along and returns the answer. Its own examples describe it as working with most AI software libraries, in a format very close to OpenAI's API, with models from OpenAI, Anthropic, Google and others.
The three things that change
If you already know how to call OpenAI's API from Python, only three things are different:
- The base URL. This is the web address your library sends requests to. For OpenRouter it is
https://openrouter.ai/api/v1. - The key. You create an OpenRouter API key on its website (it starts with
sk-or-v1-) and keep it out of your code, exactly as you would an OpenAI key. - The model name. Models are named with the provider first, then the model, such as
openai/gpt-4o-miniormeta-llama/llama-3.3-70b-instruct. You copy the exact name from the model's page on OpenRouter.
Everything else, including client.chat.completions.create with a list of messages, is the same call you would make to OpenAI.
Worked example: one prompt, three models
This script sends the same request to three models and prints each answer with its token counts and response time. It uses the official openai library (version 1.x) and python-dotenv to read your key from a .env file.
First, install the two libraries and create a file named .env next to your script:
pip install openai python-dotenv
OPENROUTER_API_KEY=sk-or-v1-your-key-here
Then save this as compare.py:
import time
from os import getenv
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=getenv("OPENROUTER_API_KEY"),
)
MODELS = [
"openai/gpt-4o-mini",
"meta-llama/llama-3.3-70b-instruct",
"mistralai/mistral-small-3.1-24b-instruct",
]
PROMPT = ("Rewrite this notice for a library newsletter in three short, friendly "
"sentences: 'The building will be closed Monday for floor repairs. "
"Book returns can be left in the outside drop box.'")
for model in MODELS:
start = time.time()
reply = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": PROMPT}],
temperature=0.3,
)
seconds = time.time() - start
usage = reply.usage
print(f"=== {model} ({seconds:.1f} s)")
print(f"tokens in: {usage.prompt_tokens}, tokens out: {usage.completion_tokens}")
print(reply.choices[0].message.content.strip(), "\n")
Run it with python compare.py. Switching to a fourth model means adding one line to the MODELS list. If a name is misspelled or a model is unavailable, the library raises an error for that request; wrap the call in try/except if you want the loop to continue past it.
Try it: Replace the prompt with a real task from your work, such as a customer reply or a product description, and run the script. Before reading the outputs closely, cover the model names and rank the three answers. Then check which model you preferred.
How do you compare cost and quality across them?
Cost comes from tokens, the small chunks of text models read and write. Each model on OpenRouter has two prices, one per million input tokens and one per million output tokens, and they can differ a lot between models. Your script already prints both token counts, so the cost of one request is:
cost = tokens_in × input_price ÷ 1,000,000 + tokens_out × output_price ÷ 1,000,000
As an illustration with made-up numbers: if a request uses 60 tokens in and 80 tokens out, and a model charges $0.15 per million input tokens and $0.60 per million output tokens, the request costs about $0.000057. Multiply by how many requests you expect per month to see what matters. Use the counts each model actually reports rather than one shared estimate. Copy the current prices from each model's page when you run the test, since they change.
Quality needs a small, honest test rather than a single prompt. Collect ten to twenty real examples of the task, run them through each model, and score the answers against a short checklist you write in advance: is it accurate, does it follow the format, is the tone right, how long did it take? Scoring with the model names hidden keeps you from favouring a brand. A simple spreadsheet with one row per example and one column per model is enough.
Then decide with both numbers in view. Often the most capable model wins on hard examples but a cheaper one is just as good on routine ones, and you can send each kind of request to a different model. With OpenRouter, that decision is one string in your code, so you can revisit it whenever a new model appears.
Sources
- openrouter-examples-python: src/openai_test.py (commit da9992c, April 19, 2024) — OpenRouter (GitHub), April 19, 2024
- OpenRouter Examples (README, commit 5fcd513, November 14, 2023) — OpenRouter (GitHub), November 14, 2023
- OpenAI Python API library (README) — OpenAI (GitHub), September 17, 2024