Introduction

Say you are constructing a support-ticket assistant, and also you need to evaluate how GPT, Claude, and Llama every deal with the identical buyer message earlier than choosing one. Usually meaning signing up with three separate suppliers, studying three totally different authentication schemes, and rewriting your request code 3 times simply to run one comparability.

OpenRouter removes that repetition. It sits in entrance of dozens of suppliers and exposes them by means of a single API, so switching from one mannequin to a different is a one-line change as a substitute of a brand new integration. Your Python code stays precisely the identical no matter which supplier is definitely answering the request. This text covers:

  • Creating an OpenRouter account and getting an API key
  • Sending your first request to a mannequin with plain Python and requests
  • Switching fashions by altering a single string, with no different code adjustments
  • Letting OpenRouter decide a mannequin mechanically, and including backup fashions for reliability

We’ll construct this up one small script at a time, beginning with the account setup.

Stipulations

You do not want any AI or machine studying background to comply with alongside. You do, nonetheless, must be comfy with fundamental Python, putting in packages with pip, and setting an surroundings variable in your terminal. OpenRouter handles the mannequin integration, so your code solely sends textual content and reads the response.

Creating an OpenRouter Account and API Key

Go to OpenRouter and create a free account. When you’re signed in, open the Keys part out of your account menu and generate a brand new API key. Give it a reputation you will acknowledge later, like learning-key, and depart the opposite settings on their defaults.

Copy the important thing someplace secure. Retailer it as an surroundings variable so it by no means finally ends up dedicated to a repository by chance.

On macOS or Linux:

export OPENROUTER_API_KEY="your-key-here"

TL;DR: Some fashions on OpenRouter are free to attempt, so you’ll be able to comply with this complete tutorial with out including a fee technique.

Extra on the why and the way: OpenRouter marks sure fashions with a :free suffix; these route to totally free-to-use variations, with no fee technique required. Each instance on this tutorial makes use of considered one of these, so you’ll be able to run all of it at zero price. If a mannequin ID on this tutorial stops responding by the point you learn it, examine the present checklist at openrouter.ai/fashions filtered to free, or swap in openrouter/free, which we’ll cowl beneath and which at all times resolves to whichever free mannequin is at the moment accessible.

Setting Up Your Challenge

Create a folder for the mission and a digital surroundings inside it:

mkdir support-bot-demo
cd support-bot-demo
python -m venv .venv
supply .venv/bin/activate

With the surroundings energetic, set up requests. It’s the solely dependency you want. OpenRouter is a plain HTTP API, so there is no particular software program improvement package (SDK) required to get began:

pip set up requests

Notice: OpenRouter does have an official SDK too. You’ll be able to set up openrouter with pip, which supplies typed shopper objects as a substitute of uncooked dictionaries.

This tutorial solely makes use of requests so each request stays seen as plain JSON, making it simpler to see precisely what’s being despatched and acquired whilst you’re nonetheless studying how the API is formed. When you get the grasp of it, shifting to the SDK later is a small change.

Sending Your First Request to a Mannequin

Each request to OpenRouter seems the identical no matter which mannequin solutions it: a POST request to at least one endpoint, together with your API key within the headers and an inventory of messages within the physique. Here is a script that sends a buyer assist message to a mannequin and prints the reply:

import os
import requests

API_URL = "
API_KEY = os.getenv("OPENROUTER_API_KEY")

customer_message = (
    "My order #48213 arrived with the improper charger. "
    "I would like the right one earlier than Friday, are you able to assist?"
)

payload = {
    "mannequin": "meta-llama/llama-3.2-3b-instruct:free",
    "messages": [
        {"role": "system", "content": "You are a concise, polite customer support agent."},
        {"role": "user", "content": customer_message},
    ],
}

headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Content material-Kind": "utility/json",
}

response = requests.submit(API_URL, headers=headers, json=payload)
knowledge = response.json()

print(f"Mannequin used: {knowledge['model']}")
print(f"Reply: {knowledge['choices'][0]['message']['content']}")

The payload dictionary is the whole request: mannequin tells OpenRouter which mannequin to make use of, written as supplier/model-name, and messages follows the identical function/content material format you’d use with any chat-style mannequin.

The system message units the tone as soon as, and the person message is the precise ticket textual content. The response comes again as JSON, and the reply textual content is in knowledge["choices"][0]["message"]["content"] — the identical form you’d get from most chat completion APIs, since OpenRouter normalizes each supplier’s response into the identical OpenAI-compatible format.

Switching Fashions With out Altering Your Code

Because the mannequin is only a string within the payload, evaluating suppliers means altering one line. Here is a small operate that takes the mannequin title as an argument, so you’ll be able to name it with totally different fashions again to again:

def ask_model(model_name, message):
    payload = {
        "mannequin": model_name,
        "messages": [
            {"role": "system", "content": "You are a concise, polite customer support agent."},
            {"role": "user", "content": message},
        ],
    }
    response = requests.submit(API_URL, headers=headers, json=payload)
    return response.json()


ticket = "My order #48213 arrived with the improper charger. I would like the right one earlier than Friday."

for mannequin in ["meta-llama/llama-3.2-3b-instruct:free", "qwen/qwen3-4b:free", "mistralai/mistral-7b-instruct:free"]:
    end result = ask_model(mannequin, ticket)
    reply = end result["choices"][0]["message"]["content"]
    print(f"--- {mannequin} ---")
    print(reply, "n")

ask_model is identical request logic from the primary script (it reuses API_URL and headers from there), solely wrapped so model_name is a parameter as a substitute of a hardcoded worth. The loop calls it 3 times with three totally different fashions and prints every reply subsequent to the mannequin that produced it. That is the core good thing about OpenRouter in follow: you are calling one operate 3 times as a substitute of working with three totally different APIs.

Letting OpenRouter Choose a Free Mannequin for You

Generally you do not care which particular mannequin solutions; you simply need a working reply with out choosing one your self, and with none threat of a paid mannequin slipping in. Setting mannequin to openrouter/free arms that call to OpenRouter’s Free Fashions Router, which at all times selects from at the moment accessible free fashions:

payload = {
    "mannequin": "openrouter/free",
    "messages": [{"role": "user", "content": ticket}],
}
response = requests.submit(API_URL, headers=headers, json=payload)
knowledge = response.json()

print(f"OpenRouter picked: {knowledge['model']}")

That is the most secure default for prototyping: regardless of which free mannequin occurs to be accessible that day, you are by no means charged. It is a totally different setting from openrouter/auto, which picks no matter mannequin most closely fits the immediate and may embrace paid ones.

Individually, if you do specify a mannequin by title, OpenRouter typically has a couple of supplier internet hosting it, and you may select which one it makes use of with a supplier area. For instance, set payload["provider"] = {"type": "worth"} to favor the most cost effective supplier, or {"type": "latency"} to favor the quickest one. This issues most when you’re working with paid fashions at actual quantity; it has little impact on :free fashions, since there is no per-token price to optimize in opposition to within the first place.

Including Fallback Fashions for Reliability

A single mannequin can fail on account of a supplier outage, a fee restrict, a content material filter rejecting the request, and rather more. For something past an excellent small aspect mission, you need a backup prepared mechanically as a substitute of your app crashing on the primary failure. OpenRouter helps this by accepting an inventory of fashions as a substitute of 1:

payload = {
    "fashions": [
        "qwen/qwen3-4b:free",
        "meta-llama/llama-3.2-3b-instruct:free",
        "mistralai/mistral-7b-instruct:free",
    ],
    "messages": [{"role": "user", "content": ticket}],
}
response = requests.submit(API_URL, headers=headers, json=payload)
knowledge = response.json()

print(f"Answered by: {knowledge['model']}")
print(knowledge["choices"][0]["message"]["content"])

Discover the important thing modified from mannequin (singular) to fashions (an inventory). OpenRouter tries every entry so as and returns the primary one which succeeds, so your code would not want any retry logic of its personal. Order the checklist by what most closely fits your app, for instance the best high quality first with cheaper backups, or the quickest first with extra dependable backups additional down.

Wrapping Up

Here is an outline of all the things we have coated on this introductory article and if you’d use every:

Characteristic What It Does When To Use It
A single mannequin string with a :free tag Targets one particular free mannequin by title Once you want a predictable mannequin with zero price
Swapping the mannequin worth Enables you to evaluate fashions with no different code adjustments When testing which mannequin suits your use case greatest
"mannequin": "openrouter/free" Lets OpenRouter decide any accessible free mannequin for you Fast prototyping with a guaranteed-zero-cost default
"supplier": {"type": "worth"} or {"type": "latency"} Picks a supplier to your chosen mannequin based mostly on price or pace Working with paid fashions at a substantial quantity
"fashions": [...] (an inventory) Tries every mannequin so as till one succeeds Apps that want to remain up if one mannequin fails

With these fundamentals, you’ll be able to construct something from a fast one-off script to a assist bot that stays on-line even when a single supplier goes down.

As at all times, listed below are a few assets for additional studying:

Maintain exploring!

 
 

Bala Priya is a developer, educator, and technical author based mostly in India. She writes about programming, knowledge science, machine studying, and AI, with a concentrate on making advanced technical concepts sensible and approachable.

She creates tutorials, how-to guides, and deep dives that assist builders construct, remedy issues, and perceive advanced technical ideas. Her technical pursuits embrace LLM functions, agentic AI, graph algorithms, laptop imaginative and prescient, explainable AI, and ML techniques.

When she’s not writing or coding, Bala is normally studying basic literature, epic fantasy, or speculative fiction… with a cup of espresso shut by. She additionally enjoys speaking about books, studying new issues, and sharing what she learns with the developer neighborhood.