DEV Community

qing
qing

Posted on

OpenRouter API: Access Claude, GPT-4, and Gemini for Free with Python

OpenRouter API: Access Claude, GPT-4, and Gemini for Free with Python

OpenRouter gives you access to 100+ AI models including free tiers for Claude and Gemini.

Setup

  1. Sign up at openrouter.ai
  2. Get $1 free credits (enough for ~1000 messages)
  3. Many models have free tiers

Install

pip install httpx
Enter fullscreen mode Exit fullscreen mode

Basic Usage

import httpx
import os

OPENROUTER_KEY = os.getenv("OPENROUTER_API_KEY")

def chat(prompt: str, model: str = "google/gemini-flash-1.5") -> str:
    """
    Free models on OpenRouter:
    - google/gemini-flash-1.5 (FREE)
    - meta-llama/llama-3.2-3b-instruct (FREE)
    - qwen/qwen-2-7b-instruct (FREE)
    """
    with httpx.Client() as client:
        r = client.post(
            "https://openrouter.ai/api/v1/chat/completions",
            headers={
                "Authorization": f"Bearer {OPENROUTER_KEY}",
                "HTTP-Referer": "https://github.com/my-project",
            },
            json={
                "model": model,
                "messages": [{"role": "user", "content": prompt}],
            },
            timeout=30,
        )
        return r.json()["choices"][0]["message"]["content"]

# Compare models
models = [
    "google/gemini-flash-1.5",
    "meta-llama/llama-3.2-3b-instruct",
]

for model in models:
    response = chat("What is Python?", model)
    print(f"\n{model}:\n{response[:200]}...")
Enter fullscreen mode Exit fullscreen mode

Cost-Effective Strategy

# Route based on complexity
def smart_chat(prompt: str) -> str:
    word_count = len(prompt.split())

    if word_count < 50:
        # Simple question - use free model
        return chat(prompt, "google/gemini-flash-1.5")
    elif word_count < 200:
        # Medium - use good free model
        return chat(prompt, "meta-llama/llama-3.1-8b-instruct:free")
    else:
        # Complex - use capable model
        return chat(prompt, "anthropic/claude-3-haiku")

result = smart_chat("What is 2+2?")
print(result)
Enter fullscreen mode Exit fullscreen mode

Free Models on OpenRouter

Model Context Notes
google/gemini-flash-1.5 1M tokens Best free option
meta-llama/llama-3.2-3b 128K Fast responses
qwen/qwen-2-7b 128K Good coding

Follow me for more AI Python tutorials! 🐍

Follow for more Python content!


💡 Related: **Content Creator Ultimate Bundle (Save 33%)* — $30*


If you found this useful, you might like Python Interview Prep Guide — a practical resource that takes things a step further. At $24.99 it's a solid investment for your toolkit.

Top comments (0)