Constructing with AI in the present day can really feel messy. You would possibly use one API for textual content, one other for photos, and a distinct one for one thing else. Each mannequin comes with its personal setup, API key, and billing. This slows you down and makes issues more durable than they have to be. What in the event you may use all these fashions by means of one easy API. That’s the place OpenRouter helps. It offers you one place to entry fashions from suppliers like OpenAI, Google, Anthropic and extra. On this information, you’ll learn to use OpenRouter step-by-step, out of your first API name to constructing actual functions.
What’s OpenRouter?
OpenRouter enables you to entry many AI fashions utilizing a single API. You don’t have to arrange every supplier individually. You join as soon as, use one API key, and write one set of code. OpenRouter handles the remaining, like authentication, request formatting, and billing. This makes it straightforward to attempt totally different fashions. You possibly can swap between fashions like GPT-5, Claude 4.6, Gemini 3.1 Professional, or Llama 4 by altering only one parameter in your code. This helps you select the fitting mannequin primarily based on value, pace or options like reasoning and picture understanding.

How OpenRouter Works?
OpenRouter acts as a bridge between your utility and totally different AI suppliers. Your app sends a request to the OpenRouter API, and it converts that request into a regular format that any mannequin can perceive.

A state-of-the-art routing engine is then concerned. It’s going to discover the perfect supplier of your request in response to a set of rule that you would be able to set. To offer an instance, it may be set to offer desire to probably the most cheap supplier, the one with the shortest latency, or merely these with a specific knowledge privateness requirement resembling Zero Knowledge Retention (ZDR).
The platform retains monitor of the efficiency and uptime of all of the suppliers and as such, is ready to make clever, real-time routing choices. In case your most popular supplier is just not functioning correctly, the OpenRouter fails over to a known-good one routinely and improves the steadiness of your utility.
Getting Began: Your First API Name
OpenRouter can also be straightforward to arrange since it’s a hosted service, i.e. there isn’t any software program to be put in. It may be prepared in a matter of minutes:
Step 1: Create an Account and Get Credit:
First, enroll at OpenRouter.ai. To make use of the paid fashions, you have to to buy some credit.
Step 2: Generate an API Key
Navigate to the “Keys” part in your account dashboard. Click on “Create Key,” give it a reputation, and replica the important thing securely. For greatest apply, use separate keys for various environments (e.g., dev, prod) and set spending limits to regulate prices.
Step 3: Configure Your Surroundings
Retailer your API key in an setting variable to keep away from exposing it in your code.
Step 4: Native Setup utilizing an Surroundings Variable:
For macOS or Linux:
export OPENROUTER_API_KEY="your-secret-key-here"For Home windows (PowerShell):
setx OPENROUTER_API_KEY "your-secret-key-here"Making a Request on OpenRouter
Since OpenRouter has an API that’s appropriate with OpenAI, you should use official OpenAI consumer libraries to make requests. This renders the method of migration of an already accomplished OpenAI challenge extremely straightforward.
Python Instance utilizing the OpenAI SDK
# First, guarantee you have got the library put in:
# pip set up openai
import os
from openai import OpenAI
# Initialize the consumer, pointing it to OpenRouter's API
consumer = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ.get("OPENROUTER_API_KEY"),
)
# Ship a chat completion request to a particular mannequin
response = consumer.chat.completions.create(
mannequin="openai/gpt-4.1-nano",
messages=[
{
"role": "user",
"content": "Explain AI model routing in one sentence."
},
],
)
print(response.selections[0].message.content material)Output:

Exploring Fashions and Superior Routing
OpenRouter reveals its true energy past easy requests. Its platform helps dynamic and clever AI mannequin routing.
Programmatically Discovering Fashions
As fashions are repeatedly added or up to date, you aren’t alleged to hardcode mannequin names in considered one of your manufacturing apps, as a substitute openrouter has a /fashions endpoint that returns the checklist of all obtainable fashions with advised pricing, context limits and capabilities.
import os
import requests
# Fetch the checklist of accessible fashions
response = requests.get(
"https://openrouter.ai/api/v1/fashions",
headers={
"Authorization": f"Bearer {os.environ.get('OPENROUTER_API_KEY')}"
},
)
if response.status_code == 200:
fashions = response.json()["data"]
# Filter for fashions that assist device use
tool_use_models = [
m for m in models
if "tools" in (m.get("supported_parameters") or [])
]
print(f"Discovered {len(fashions)} whole fashions.")
print(f"Discovered {len(tool_use_models)} fashions that assist device use.")
else:
print(f"Error fetching fashions: {response.textual content}"Output:

Clever Routing and Fallbacks
You’ll be able to handle the way in which OpenRouter chooses a supplier and may set backups in case of a request failure. That is the crucial resilience of manufacturing techniques.
- Routing: Ship a supplier object into your request to rank fashions by latency or value, or serve insurance policies resembling zdr (Zero Knowledge Retention).
- Fallbacks: When the previous fails, OpenRouter routinely makes an attempt the next within the checklist. Solely the profitable try can be charged.
Here’s a Python instance demonstrating a fallback chain:
# The first mannequin is 'openai/gpt-4.1-nano'
# If it fails, OpenRouter will attempt 'anthropic/claude-3.5-sonnet',
# then 'google/gemini-2.5-pro'
response = consumer.chat.completions.create(
mannequin="openai/gpt-4.1-nano",
extra_body={
"fashions": [
"anthropic/claude-3.5-sonnet",
"google/gemini-2.5-pro"
]
},
messages=[
{
"role": "user",
"content": "Write a short poem about space."
}
],
)
print(f"Mannequin used: {response.mannequin}")
print(response.selections[0].message.content material)Output:

Mastering Superior Capabilities
The identical chat completions API can be utilized to ship photos to any imaginative and prescient succesful mannequin to research them. All that’s wanted is so as to add the picture as a URL, or a base64-encoded string to your messages array.
Structured Outputs (JSON Mode)
Want a dependable JSON output? You possibly can instruct any appropriate mannequin to return a response that conforms to a particular JSON schema.The OpenRouter even has an elective Response Therapeutic plugin that can be utilized to restore malformed JSON because of fashions which have points with strict formatting.
# Requesting a structured JSON output
response = consumer.chat.completions.create(
mannequin="openai/gpt-4.1-nano",
messages=[
{
"role": "user",
"content": "Extract the name and age from this text: 'John is 30 years old.' in JSON format."
}
],
response_format={
"kind": "json_object",
"json_schema": {
"identify": "user_schema",
"schema": {
"kind": "object",
"properties": {
"identify": {"kind": "string"},
"age": {"kind": "integer"}
},
"required": ["name", "age"],
},
},
},
)
print(response.selections[0].message.content material)Output:

Multimodal Inputs: Working with Pictures
You should utilize the identical chat completions API to ship photos to any vision-capable mannequin for evaluation. Merely add the picture as a URL or a base64-encoded string to your messages array.
# Sending a picture URL for evaluation
response = consumer.chat.completions.create(
mannequin="openai/gpt-4.1-nano",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRmqgVW-371UD3RgE3HwhF11LYbGcVfn9eiTYqiw6a8fK51Es4SYBK0fNVyCnJzQit6YKo9ze3vg1tYoWlwqp3qgiOmRxkTg1bxPwZK3A&s=10"
}
},
],
}
],
)
print(response.selections[0].message.content material)Output:

A Price-Conscious, Multi-Supplier Agent
The precise power of OpenRouter lies within the improvement of superior, reasonably priced, and excessive availability functions. As an illustration, we are able to develop a practical agent that can dynamically select the perfect mannequin to accomplish a particular job with the help of a tiered strategy to cheap-to-smart technique.
The very first thing that this agent will do is to try to reply to a question offered by a person utilizing a quick and low cost mannequin. In case that mannequin is just not adequate (e.g. in case the duty includes deep reasoning) it will upwardly redirect the question to a extra highly effective, premium mannequin. This can be a typical pattern in relation to manufacturing functions which should strike a stability between efficiency, value, and high quality.
The “Low-cost-to-Sensible” Logic
Our agent will comply with these steps:
- Obtain a person’s immediate.
- Ship the immediate to a low value mannequin at first.
- Study the response to decide whether or not the mannequin was ready to reply to the request. One straightforward technique of doing that is to request the mannequin to supply a confidence rating with its output.
- When the boldness is low, the agent will routinely repeat the identical immediate with a high-end mannequin which leads to a great reply to a posh job.
This strategy ensures you aren’t overpaying for easy requests whereas nonetheless having the facility of top-tier fashions on demand.
Python Implementation
Right here’s how one can implement this logic in Python. We’ll use structured outputs to ask the mannequin for its confidence stage, which makes parsing the response dependable.
from openai import OpenAI
import os
import json
# Initialize the consumer for OpenRouter
consumer = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ.get("OPENROUTER_API_KEY"),
)
def run_cheap_to_smart_agent(immediate: str):
"""
Runs a immediate first by means of an inexpensive mannequin, then escalates to a
smarter mannequin if confidence is low.
"""
cheap_model = "mistralai/mistral-7b-instruct"
smart_model = "openai/gpt-4.1-nano"
# Outline the specified JSON construction for the response
json_schema = {
"kind": "object",
"properties": {
"reply": {"kind": "string"},
"confidence": {
"kind": "integer",
"description": "A rating from 1-100 indicating confidence within the reply.",
},
},
"required": ["answer", "confidence"],
}
# First, attempt a budget mannequin
print(f"--- Trying with low cost mannequin: {cheap_model} ---")
attempt:
response = consumer.chat.completions.create(
mannequin=cheap_model,
messages=[
{
"role": "user",
"content": f"Answer the following prompt and provide a confidence score from 1-100. Prompt: {prompt}",
}
],
response_format={
"kind": "json_object",
"json_schema": {
"identify": "agent_response",
"schema": json_schema,
},
},
)
# Parse the JSON response
end result = json.masses(response.selections[0].message.content material)
reply = end result.get("reply")
confidence = end result.get("confidence", 0)
print(f"Low-cost mannequin confidence: {confidence}")
# If confidence is under a threshold (e.g., 70), escalate
if confidence < 70:
print(f"--- Confidence low. Escalating to sensible mannequin: {smart_model} ---")
# Use a less complicated immediate for the sensible mannequin
smart_response = consumer.chat.completions.create(
mannequin=smart_model,
messages=[
{
"role": "user",
"content": prompt,
}
],
)
final_answer = smart_response.selections[0].message.content material
else:
final_answer = reply
besides Exception as e:
print(f"An error occurred with a budget mannequin: {e}")
print(f"--- Falling again on to sensible mannequin: {smart_model} ---")
smart_response = consumer.chat.completions.create(
mannequin=smart_model,
messages=[
{
"role": "user",
"content": prompt,
}
],
)
final_answer = smart_response.selections[0].message.content material
return final_answer
# --- Take a look at the Agent ---
# 1. A easy immediate that a budget mannequin can deal with
simple_prompt = "What's the capital of France?"
print(f"Remaining Reply for Easy Immediate:n{run_cheap_to_smart_agent(simple_prompt)}n")
# 2. A fancy immediate that can probably require escalation
complex_prompt = "Present an in depth comparability of the transformer structure and recurrent neural networks, specializing in their respective benefits for sequence processing duties."
print(f"Remaining Reply for Complicated Immediate:n{run_cheap_to_smart_agent(complex_prompt)}")Output:

This hands-on instance goes past a easy API name and showcases the way to architect a extra clever, cost-effective system utilizing OpenRouter’s core strengths: mannequin selection and structured outputs.
Monitoring and Observability
Understanding your utility’s efficiency and prices is essential. OpenRouter gives built-in instruments to assist.
- Utilization Accounting: Each API response comprises detailed metadata about token utilization and price for that particular request, permitting for real-time expense monitoring.
- Broadcast Function: With none further code, you’ll be able to configure OpenRouter to routinely ship detailed traces of your API calls to observability platforms like Langfuse or Datadog. This gives deep insights into latency, errors, and efficiency throughout all fashions and suppliers.
Conclusion
The period of being tethered to a single AI supplier is over. Instruments like OpenRouter are basically altering the developer expertise by offering a layer of abstraction that unlocks unprecedented flexibility and resilience. By unifying the fragmented AI panorama, OpenRouter not solely saves you from the tedious work of managing a number of integrations but in addition empowers you to construct smarter, less expensive, and sturdy functions. The way forward for AI improvement is just not about choosing one winner; it’s about having seamless entry to all of them. With this information, you now have the map to navigate that future.
Often Requested Questions
A. OpenRouter gives a single, unified API to entry lots of of AI fashions from numerous suppliers. This simplifies improvement, enhances reliability with computerized fallbacks, and permits you to simply swap fashions to optimize for value or efficiency.
A. No, it’s designed to be an OpenAI-compatible API. You should utilize present OpenAI SDKs and sometimes solely want to alter the bottom URL to level to OpenRouter.
A. OpenRouter’s fallback function routinely retries your request with a backup mannequin you specify. This makes your utility extra resilient to supplier outages.
A. Sure, you’ll be able to set strict spending limits on every API key, with every day, weekly, or month-to-month reset schedules. Each API response additionally consists of detailed value knowledge for real-time monitoring.
A. Sure, OpenRouter helps structured outputs. You possibly can present a JSON schema in your request to pressure the mannequin to return a response in a legitimate, predictable format.
Login to proceed studying and revel in expert-curated content material.
