Constructing with AI immediately can really feel messy. You would possibly use one API for textual content, one other for pictures, and a special 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 must be. What when you may use all these fashions by means of one easy API. That’s the place OpenRouter helps. It provides you one place to entry fashions from suppliers like OpenAI, Google, Anthropic and extra. On this information, you’ll discover ways to use OpenRouter step-by-step, out of your first API name to constructing actual purposes.
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 completely 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 correct mannequin primarily based on value, pace or options like reasoning and picture understanding.

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

A state-of-the-art routing engine is then concerned. It’s going to discover the most effective supplier of your request based on a set of rule which you could set. To offer an instance, it may be set to present desire to probably the most cheap supplier, the one with the shortest latency, or merely these with a selected knowledge privateness requirement equivalent to Zero Knowledge Retention (ZDR).
The platform retains observe of the efficiency and uptime of all of the suppliers and as such, is ready to make clever, real-time routing selections. In case your most popular supplier shouldn’t be functioning correctly, the OpenRouter fails over to a known-good one routinely and improves the soundness of your software.
Getting Began: Your First API Name
OpenRouter can be straightforward to arrange since it’s a hosted service, i.e. there is no such thing as a 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 will want 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 Atmosphere
Retailer your API key in an surroundings variable to keep away from exposing it in your code.
Step 4: Native Setup utilizing an Atmosphere 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 need to use official OpenAI shopper libraries to make requests. This renders the method of migration of an already accomplished OpenAI undertaking extremely straightforward.
Python Instance utilizing the OpenAI SDK
# First, guarantee you've got the library put in:
# pip set up openai
import os
from openai import OpenAI
# Initialize the shopper, pointing it to OpenRouter's API
shopper = 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 = shopper.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 exhibits 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 presupposed to hardcode mannequin names in one in every of your manufacturing apps, as an alternative openrouter has a /fashions endpoint that returns the record of all out there fashions with steered pricing, context limits and capabilities.
import os
import requests
# Fetch the record 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 help device use
tool_use_models = [
m for m in models
if "tools" in (m.get("supported_parameters") or [])
]
print(f"Discovered {len(fashions)} complete fashions.")
print(f"Discovered {len(tool_use_models)} fashions that help 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 worth, or serve insurance policies equivalent to zdr (Zero Knowledge Retention).
- Fallbacks: When the previous fails, OpenRouter routinely makes an attempt the next within the record. Solely the profitable try could 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 = shopper.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 pictures to any imaginative and prescient succesful mannequin to investigate 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 non-obligatory Response Therapeutic plugin that can be utilized to restore malformed JSON resulting from fashions which have points with strict formatting.
# Requesting a structured JSON output
response = shopper.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": {
"title": "user_schema",
"schema": {
"kind": "object",
"properties": {
"title": {"kind": "string"},
"age": {"kind": "integer"}
},
"required": ["name", "age"],
},
},
},
)
print(response.selections[0].message.content material)
Output:

Multimodal Inputs: Working with Photographs
You need to use the identical chat completions API to ship pictures 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 = shopper.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, inexpensive, and excessive availability purposes. As an illustration, we are able to develop a practical agent that may dynamically select the most effective mannequin to accomplish a particular activity with the help of a tiered method to cheap-to-smart technique.
The very first thing that this agent will do is to try to reply to a question supplied by a consumer utilizing a quick and low-cost mannequin. In case that mannequin shouldn’t be ok (e.g. in case the duty entails deep reasoning) it will upwardly redirect the question to a extra highly effective, premium mannequin. It is a typical pattern on the subject of manufacturing purposes which must strike a steadiness between efficiency, worth, and high quality.
The “Low-cost-to-Good” Logic
Our agent will observe these steps:
- Obtain a consumer’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 offer 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 ends up in reply to a posh activity.
This method 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 are going to 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 shopper for OpenRouter
shopper = 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 affordable 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 = shopper.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": {
"title": "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 an easier immediate for the sensible mannequin
smart_response = shopper.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 = shopper.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"Ultimate Reply for Easy Immediate:n{run_cheap_to_smart_agent(simple_prompt)}n")
# 2. A posh immediate that may possible 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"Ultimate Reply for Advanced Immediate:n{run_cheap_to_smart_agent(complex_prompt)}")
Output:

This hands-on instance goes past a easy API name and showcases learn how to architect a extra clever, cost-effective system utilizing OpenRouter’s core strengths: mannequin selection and structured outputs.
Monitoring and Observability
Understanding your software’s efficiency and prices is essential. OpenRouter offers built-in instruments to assist.
- Utilization Accounting: Each API response comprises detailed metadata about token utilization and value for that particular request, permitting for real-time expense monitoring.
- Broadcast Characteristic: With none additional 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 offers 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, more cost effective, and sturdy purposes. The way forward for AI improvement shouldn’t be 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.
Steadily Requested Questions
A. OpenRouter offers a single, unified API to entry tons of of AI fashions from varied suppliers. This simplifies improvement, enhances reliability with computerized fallbacks, and means that you can simply swap fashions to optimize for value or efficiency.
A. No, it’s designed to be an OpenAI-compatible API. You need to use present OpenAI SDKs and sometimes solely want to vary 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 software extra resilient to supplier outages.
A. Sure, you’ll be able to set strict spending limits on every API key, with each day, weekly, or month-to-month reset schedules. Each API response additionally contains 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.
