Module 5: OpenRouter - Introduction

Your First Multi-Provider Requests

Overview

You'll make requests to multiple models (GPT, Claude, Gemini, Mixtral) using OpenRouter with the OpenAI SDK.

Time: 20 minutes
Difficulty: Low


🎯 Objectives

  • ✅ Request to GPT-3.5 via OpenRouter
  • ✅ Request to Claude 3 Haiku
  • ✅ Request to Gemini Pro
  • ✅ Compare responses

💻 Basic Setup

from openai import OpenAI
import os
from dotenv import load_dotenv

load_dotenv()

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=os.getenv("OPENROUTER_API_KEY")
)

Change vs Module 2: Only base_url!


🚀 Request 1: GPT-3.5 (OpenAI)

response = client.chat.completions.create(
    model="openai/gpt-3.5-turbo",
    messages=[
        {"role": "user", "content": "What is Python?"}
    ]
)

print("GPT-3.5:", response.choices[0].message.content)

Model ID: openai/gpt-3.5-turbo
Format: provider/model-name


🔮 Request 2: Claude 3 Haiku (Anthropic)

response = client.chat.completions.create(
    model="anthropic/claude-3-haiku",
    messages=[
        {"role": "user", "content": "What is Python?"}
    ]
)

print("Claude 3:", response.choices[0].message.content)

Model ID: anthropic/claude-3-haiku


🌟 Request 3: Gemini Pro (Google)

response = client.chat.completions.create(
    model="google/gemini-pro",
    messages=[
        {"role": "user", "content": "What is Python?"}
    ]
)

print("Gemini:", response.choices[0].message.content)

Model ID: google/gemini-pro


🔥 Request 4: Mixtral (Mistral AI)

response = client.chat.completions.create(
    model="mistralai/mixtral-8x7b-instruct",
    messages=[
        {"role": "user", "content": "What is Python?"}
    ]
)

print("Mixtral:", response.choices[0].message.content)

Model ID: mistralai/mixtral-8x7b-instruct


📊 Full Comparison

from openai import OpenAI
import os

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=os.getenv("OPENROUTER_API_KEY")
)

models = [
    "openai/gpt-3.5-turbo",
    "anthropic/claude-3-haiku",
    "google/gemini-pro",
    "mistralai/mixtral-8x7b-instruct"
]

prompt = "Explain what Python is in 20 words"

for model in models:
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}]
    )
    
    print(f"\n=== {model} ===")
    print(response.choices[0].message.content)

Example output:

=== openai/gpt-3.5-turbo ===
Python is an interpreted programming language, versatile and 
easy to learn, used in web development, data science, and more.

=== anthropic/claude-3-haiku ===
Python: an interpreted, multi-paradigm language with clear syntax, 
widely used for web, data science, automation, and ML.

=== google/gemini-pro ===
Python is a popular, interpreted, high-level programming language,
object-oriented and versatile for many uses.

=== mistralai/mixtral-8x7b-instruct ===
Python: an interpreted, dynamic language with simple syntax,
used in web, data analysis, AI, and automation.

🔍 Available Models

See the full list:

import requests

response = requests.get(
    "https://openrouter.ai/api/v1/models",
    headers={"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}"}
)

models = response.json()["data"]
print(f"Total models: {len(models)}")

# First 10
for model in models[:10]:
    print(f"  - {model['id']}")

Main categories:

  • OpenAI: gpt-3.5-turbo, gpt-4, gpt-4-turbo
  • Anthropic: claude-3-haiku, claude-3-sonnet, claude-3-opus
  • Google: gemini-pro, gemini-pro-vision
  • Meta: llama-2-70b, llama-3-70b
  • Mistral: mixtral-8x7b, mistral-medium
  • Open source: Many more

💰 Pricing Comparison

# Pricing (approximate, Feb 2026)
pricing = {
    "openai/gpt-3.5-turbo": 0.50,  # $/1M tokens
    "anthropic/claude-3-haiku": 0.25,
    "google/gemini-pro": 0.50,
    "mistralai/mixtral-8x7b-instruct": 0.24
}

prompt_tokens = 20
for model, price in pricing.items():
    cost = (prompt_tokens / 1_000_000) * price
    print(f"{model}: ${cost:.8f}")

Output:

openai/gpt-3.5-turbo: $0.00001000
anthropic/claude-3-haiku: $0.00000500  (50% cheaper!)
google/gemini-pro: $0.00001000
mistralai/mixtral-8x7b-instruct: $0.00000480  (52% cheaper!)

🔄 Switch Model Dynamically

def chat(prompt: str, model: str = "openai/gpt-3.5-turbo"):
    """Chat with a configurable model."""
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

# Use different models
print(chat("Hi", model="openai/gpt-3.5-turbo"))
print(chat("Hi", model="anthropic/claude-3-haiku"))
print(chat("Hi", model="mistralai/mixtral-8x7b-instruct"))

Advantage: Same code, multiple models


📊 Model Routing

By query type:

def smart_chat(prompt: str) -> str:
    """Auto-select the model based on the query."""
    
    # Simple queries → cheap model
    if len(prompt) < 50:
        model = "mistralai/mixtral-8x7b-instruct"  # Cheap
    # Complex queries → powerful model
    else:
        model = "openai/gpt-4-turbo"  # Expensive but good
    
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}]
    )
    
    print(f"[Using: {model}]")
    return response.choices[0].message.content

# Test
print(smart_chat("Hi"))  # Uses Mixtral (cheap)
print(smart_chat("Explain the theory of relativity in detail..."))  # Uses GPT-4

🐛 Troubleshooting

Error: Model not found

# List available models
response = requests.get(
    "https://openrouter.ai/api/v1/models",
    headers={"Authorization": f"Bearer {api_key}"}
)
models = [m["id"] for m in response.json()["data"]]
print("Models:", models)

Error: 402 Payment Required

Cause: Credits exhausted

Solution: Dashboard → Add credits


✅ Summary

  • Access to 100+ models with the same API
  • Format: provider/model-name
  • Switch model in 1 line
  • Variable pricing (50%+ savings possible)
  • Same OpenAI SDK code

Next: 04-cost-optimization-1.md

You'll learn strategies to minimize costs by choosing the optimal model.

Time: 25 min