How to Set Up OpenRouter for Model Access
Access multiple AI models through OpenRouter unified marketplace.
Jay Banlasan
The AI Systems Guy
Managing five different API keys, five different SDKs, and five different billing accounts for five AI providers is a systems tax that compounds every month. OpenRouter solves it. One key, one endpoint, one bill, and access to 200+ models from every major provider. Getting openrouter setup multi-model access running takes about 30 minutes, and the productivity gain is immediate.
Beyond simplicity, OpenRouter gives you model fallback, per-request routing, and real-time pricing in a single interface. For operators running AI across multiple clients or workflows, this is infrastructure worth standardizing on.
What You Need Before Starting
- An OpenRouter account at openrouter.ai
- Your OpenRouter API key from the dashboard
- Python 3.10+ with
requestsinstalled (oropenaiSDK, which OpenRouter is compatible with) - A rough sense of which models you want to use
Step 1: Get Your API Key and Understand the Billing Model
Sign up at openrouter.ai, add credits to your account, and copy your API key. OpenRouter charges you per-token at rates that are close to (sometimes below) direct provider rates. You pay OpenRouter, they pay the providers.
Store your key safely:
# .env file
OPENROUTER_API_KEY=sk-or-v1-your-key-here
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
Never hardcode keys in scripts. Load from environment every time.
Step 2: Make Your First Call Using the OpenAI-Compatible Endpoint
OpenRouter uses the OpenAI API format. If you already use the openai Python SDK, you can swap the base URL and use any model in the OpenRouter catalog.
from openai import OpenAI
import os
from dotenv import load_dotenv
load_dotenv()
client = OpenAI(
api_key=os.getenv("OPENROUTER_API_KEY"),
base_url="https://openrouter.ai/api/v1"
)
response = client.chat.completions.create(
model="anthropic/claude-haiku-3", # OpenRouter model ID format
messages=[{"role": "user", "content": "What's the capital of France?"}],
extra_headers={
"HTTP-Referer": "https://yoursite.com", # Optional but recommended
"X-Title": "Your App Name" # Shows in OpenRouter dashboard
}
)
print(response.choices[0].message.content)
# Paris
The model ID format is {provider}/{model-name}. Browse the full catalog at openrouter.ai/models.
Step 3: Build a Model Reference Map
Keep a local reference of the models you use most. This makes it easy to swap models without hunting through docs.
MODELS = {
# Fast and cheap — daily operations
"fast": "anthropic/claude-haiku-3",
"fast-alt": "openai/gpt-4o-mini",
"fast-free": "mistralai/mistral-7b-instruct",
# Balanced — most production tasks
"balanced": "anthropic/claude-sonnet-4-5",
"balanced-alt": "openai/gpt-4o",
# Powerful — complex reasoning, long documents
"powerful": "anthropic/claude-opus-4-5",
"powerful-alt": "openai/o3-mini",
# Specialized
"code": "deepseek/deepseek-coder",
"vision": "openai/gpt-4o",
"long-ctx": "google/gemini-pro-1.5",
}
def get_client():
return OpenAI(
api_key=os.getenv("OPENROUTER_API_KEY"),
base_url="https://openrouter.ai/api/v1"
)
def call(prompt: str, tier: str = "fast", system: str = None) -> str:
client = get_client()
messages = []
if system:
messages.append({"role": "system", "content": system})
messages.append({"role": "user", "content": prompt})
response = client.chat.completions.create(
model=MODELS[tier],
messages=messages,
extra_headers={"X-Title": "My AI System"}
)
return response.choices[0].message.content
Step 4: Use OpenRouter's Built-In Fallback Feature
This is OpenRouter's best feature. Pass a list of models and it automatically tries the next one if the first is unavailable or over capacity.
def call_with_fallback(prompt: str) -> dict:
client = get_client()
response = client.chat.completions.create(
# Pass a list — OpenRouter tries them in order
model="anthropic/claude-haiku-3",
messages=[{"role": "user", "content": prompt}],
extra_headers={"X-Title": "My AI System"},
extra_body={
"models": [
"anthropic/claude-haiku-3",
"openai/gpt-4o-mini",
"mistralai/mistral-7b-instruct"
],
"route": "fallback" # try in order, not randomly
}
)
return {
"text": response.choices[0].message.content,
"model_used": response.model # tells you which model actually responded
}
Step 5: Check Real-Time Pricing Before Committing to a Model
OpenRouter exposes a pricing API. Use it to build a cost estimator before running bulk jobs.
import requests
def get_model_pricing(model_id: str) -> dict:
url = f"https://openrouter.ai/api/v1/models"
headers = {"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}"}
resp = requests.get(url, headers=headers).json()
for model in resp.get("data", []):
if model["id"] == model_id:
pricing = model.get("pricing", {})
return {
"model": model_id,
"input_per_1k": float(pricing.get("prompt", 0)) * 1000,
"output_per_1k": float(pricing.get("completion", 0)) * 1000,
"context_length": model.get("context_length", 0)
}
return {}
def estimate_cost(prompt: str, expected_output_tokens: int, model_id: str) -> float:
pricing = get_model_pricing(model_id)
input_tokens = len(prompt.split()) * 1.3 # rough estimate
cost = (input_tokens / 1000 * pricing["input_per_1k"]) + \
(expected_output_tokens / 1000 * pricing["output_per_1k"])
return round(cost, 6)
# Before running 5,000 enrichments:
cost_per_item = estimate_cost("Summarize this company...", 150, "anthropic/claude-haiku-3")
total_estimate = cost_per_item * 5000
print(f"Estimated cost: ${total_estimate:.2f}")
Step 6: Monitor Usage via OpenRouter Dashboard and API
OpenRouter tracks every call in your dashboard. You can also pull usage data via the API for your own reporting.
def get_usage_stats() -> dict:
headers = {"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}"}
resp = requests.get(
"https://openrouter.ai/api/v1/auth/key",
headers=headers
).json()
return {
"label": resp.get("data", {}).get("label"),
"usage_usd": resp.get("data", {}).get("usage", 0) / 100, # in credits
"rate_limit": resp.get("data", {}).get("rate_limit"),
"is_free_tier": resp.get("data", {}).get("is_free_tier")
}
print(get_usage_stats())
What to Build Next
- Extend the
MODELSmap with cost-per-task benchmarks so you always pick the cheapest model that meets quality bar - Build a model comparison script that runs the same 10 prompts across your most-used models and scores quality vs. cost
- Wire OpenRouter's fallback feature into your main AI dispatcher as your reliability layer
Related Reading
- How to Build a Multi-Model AI Router - add smart routing logic on top of OpenRouter's model access
- How to Build Automatic Model Failover Systems - OpenRouter handles provider-level failover, this handles your application layer
- How to Build AI Request Throttling Systems - rate-limit requests to stay within OpenRouter tier limits
Want this system built for your business?
Get a free assessment. We will map every system your business needs and show you the ROI.
Get Your Free Assessment