Module 5: OpenRouter - Introduction

Cost Optimization with OpenRouter

Overview

You'll learn strategies to minimize costs by choosing the cheapest model that meets your requirements.

Time: 25 minutes
Difficulty: Medium


🎯 Objectives

  • ✅ Compare prices per model
  • ✅ Implement cost-aware routing
  • ✅ Track spending
  • ✅ Savings strategies

💰 Pricing per Model (Feb 2026)

Comparison table:

ModelInput ($/1M)Output ($/1M)QualityUse Case
Mixtral 8x7B$0.24$0.24⭐⭐⭐⭐General (cheap)
Claude 3 Haiku$0.25$1.25⭐⭐⭐⭐Fast + cheap
GPT-3.5-turbo$0.50$1.50⭐⭐⭐⭐Standard
Gemini Pro$0.50$1.50⭐⭐⭐⭐Google ecosystem
GPT-4-turbo$10.00$30.00⭐⭐⭐⭐⭐Maximum quality
Claude 3 Opus$15.00$75.00⭐⭐⭐⭐⭐Long texts

📊 Cost Calculator

def calculate_cost(tokens: int, model: str) -> float:
    """Calculate cost for a model."""
    
    pricing = {
        "mistralai/mixtral-8x7b-instruct": 0.24,
        "anthropic/claude-3-haiku": 0.25,
        "openai/gpt-3.5-turbo": 0.50,
        "google/gemini-pro": 0.50,
        "openai/gpt-4-turbo": 10.00,
        "anthropic/claude-3-opus": 15.00
    }
    
    price_per_million = pricing.get(model, 0.50)
    cost = (tokens / 1_000_000) * price_per_million
    
    return cost

# Test
tokens = 1000
for model in ["mistralai/mixtral-8x7b-instruct", "openai/gpt-3.5-turbo", "openai/gpt-4-turbo"]:
    cost = calculate_cost(tokens, model)
    print(f"{model}: ${cost:.6f}")

Output:

mixtral: $0.000240
gpt-3.5: $0.000500 (2x more expensive)
gpt-4: $0.010000 (42x more expensive!)

🎯 Strategy 1: Tiered Routing

By complexity:

def smart_route(prompt: str, context_length: int = 0) -> str:
    """Choose the model based on complexity."""
    
    # Tier 1: Simple (FAQs, greetings)
    if len(prompt) < 50:
        return "mistralai/mixtral-8x7b-instruct"  # $0.24/1M
    
    # Tier 2: Moderate (explanations)
    elif len(prompt) < 200:
        return "anthropic/claude-3-haiku"  # $0.25/1M
    
    # Tier 3: Complex (analysis)
    else:
        return "openai/gpt-4-turbo"  # $10/1M

Savings: 95%+ on simple queries


💡 Strategy 2: Keywords-Based

def keyword_route(prompt: str) -> str:
    """Choose the model based on keywords."""
    
    prompt_lower = prompt.lower()
    
    # Code → code-specialized model
    if any(word in prompt_lower for word in ["python", "código", "code", "function"]):
        return "mistralai/codestral-latest"  # Code-specialized
    
    # Complex analysis → GPT-4
    elif any(word in prompt_lower for word in ["analiza", "explica detalladamente", "razonamiento"]):
        return "openai/gpt-4-turbo"
    
    # Default: Mixtral (cheap)
    else:
        return "mistralai/mixtral-8x7b-instruct"

# Test
print(keyword_route("Hola"))  # Mixtral
print(keyword_route("Escribe función Python"))  # Codestral
print(keyword_route("Analiza este contrato legal"))  # GPT-4

📊 Strategy 3: Budget Limiter

class BudgetAwareChat:
    """Chat with a budget limit."""
    
    def __init__(self, daily_budget: float = 1.0):
        self.daily_budget = daily_budget
        self.spent_today = 0.0
        self.client = OpenAI(
            base_url="https://openrouter.ai/api/v1",
            api_key=os.getenv("OPENROUTER_API_KEY")
        )
    
    def chat(self, prompt: str) -> str:
        """Chat with a budget check."""
        
        # Estimate cost
        estimated_tokens = len(prompt.split()) * 1.5  # Rough estimate
        
        # If near the limit → cheap model
        if self.spent_today > self.daily_budget * 0.8:
            model = "mistralai/mixtral-8x7b-instruct"
            print(f"[Budget warning: Using cheap model]")
        else:
            model = "openai/gpt-3.5-turbo"
        
        response = self.client.chat.completions.create(
            model=model,
            messages=[{"role": "user", "content": prompt}]
        )
        
        # Track spending
        cost = calculate_cost(response.usage.total_tokens, model)
        self.spent_today += cost
        
        print(f"[Cost: ${cost:.6f} | Total today: ${self.spent_today:.6f}]")
        
        return response.choices[0].message.content

# Usage
bot = BudgetAwareChat(daily_budget=0.10)
print(bot.chat("Hi"))
print(bot.chat("What is Python?"))

🔄 Strategy 4: Fallback Chain (Cheap to Expensive)

def chat_with_fallback(prompt: str) -> str:
    """Try the cheap model, fall back to expensive if it fails."""
    
    models = [
        "mistralai/mixtral-8x7b-instruct",  # Cheap
        "anthropic/claude-3-haiku",  # Medium
        "openai/gpt-3.5-turbo"  # Standard
    ]
    
    for model in models:
        try:
            response = client.chat.completions.create(
                model=model,
                messages=[{"role": "user", "content": prompt}]
            )
            print(f"[Used: {model}]")
            return response.choices[0].message.content
        except Exception as e:
            print(f"[{model} failed: {e}]")
            continue
    
    return "Error: All models failed"

# Test
print(chat_with_fallback("Hi"))

📊 Cost Tracking Dashboard

import json
from datetime import datetime

class CostTracker:
    """Track spending per model."""
    
    def __init__(self):
        self.log_file = "cost_log.json"
        self.load_log()
    
    def load_log(self):
        try:
            with open(self.log_file) as f:
                self.log = json.load(f)
        except:
            self.log = []
    
    def track(self, model: str, tokens: int, cost: float):
        """Record usage."""
        entry = {
            "timestamp": datetime.now().isoformat(),
            "model": model,
            "tokens": tokens,
            "cost": cost
        }
        self.log.append(entry)
        self.save_log()
    
    def save_log(self):
        with open(self.log_file, "w") as f:
            json.dump(self.log, f, indent=2)
    
    def summary(self):
        """Spending summary."""
        total_cost = sum(entry["cost"] for entry in self.log)
        total_tokens = sum(entry["tokens"] for entry in self.log)
        
        print(f"Total requests: {len(self.log)}")
        print(f"Total tokens: {total_tokens:,}")
        print(f"Total cost: ${total_cost:.6f}")
        
        # Per model
        by_model = {}
        for entry in self.log:
            model = entry["model"]
            if model not in by_model:
                by_model[model] = {"requests": 0, "cost": 0}
            by_model[model]["requests"] += 1
            by_model[model]["cost"] += entry["cost"]
        
        print("\nBy model:")
        for model, stats in by_model.items():
            print(f"  {model}: {stats['requests']} requests, ${stats['cost']:.6f}")

# Usage
tracker = CostTracker()
tracker.track("mixtral", 1000, 0.00024)
tracker.track("gpt-3.5", 500, 0.00025)
tracker.summary()

💡 Best Practices

1. Default to a cheap model:

DEFAULT_MODEL = "mistralai/mixtral-8x7b-instruct"

Only scale up to GPT-4 when necessary.


2. Cache common responses:

cache = {}

def cached_chat(prompt: str) -> str:
    if prompt in cache:
        print("[Cache hit - $0]")
        return cache[prompt]
    
    response = chat(prompt)
    cache[prompt] = response
    return response

Savings: 100% on repeated queries


3. Batch queries:

If you have multiple similar queries, use a single request:

# ❌ Bad: 3 requests
for q in ["What is Python?", "What is Java?", "What is Go?"]:
    chat(q)  # $0.0015 total

# ✅ Good: 1 request
batch_prompt = """
Answer each question in 20 words:
1. What is Python?
2. What is Java?
3. What is Go?
"""
chat(batch_prompt)  # $0.0005 total (3x cheaper)

✅ Summary

Savings strategies:

  1. Tiered routing (simple → cheap)
  2. Keywords-based selection
  3. Budget limits
  4. Fallback cheap-to-expensive
  5. Cost tracking
  6. Caching
  7. Batching

Potential savings: 80-95% vs using GPT-4 only


Next: 05-fallback-strategies.md

You'll learn to implement robust fallback for high availability.

Time: 20 min