We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

OpenRouter API

Large language models (LLMs) are the fancy-schmancy AI technology that has been making all the waves in the AI world in recent years. Products like:

  • ChatGPT
  • Claude
  • Cursor
  • Google Gemini

... are all powered by LLMs. For the purposes of this course, you can think of an LLM as a smart text generator. It works just like ChatGPT: you give it a prompt, and it gives you back some text that it believes answers your prompt.

We're going to use OpenRouter to power our agent. OpenRouter is a single API gateway that gives you access to hundreds of models from many providers - and crucially for us, a rotating selection of free models. Even better, OpenRouter is compatible with the OpenAI API, so we can talk to it using the official, widely-used openai Python SDK. That's a skill that transfers directly to almost every other LLM provider you'll meet.

The Free Router

Instead of hard-coding one specific model, we'll use the special model ID openrouter/free. It's a router: for each request, OpenRouter automatically picks an available free model that supports the features our request needs (like the tool calling we'll add later). This keeps us on the free tier without having to babysit which models happen to be available on any given day.

The free tier is rate-limited to roughly 50 requests per day (and 20 per minute). That's enough to complete this course if you're a little patient. If you want a smoother experience, adding $10 of credit to your OpenRouter account (a one-time top-up) raises the limit to roughly 1,000 requests per day. The free tier still works without spending anything - the $10 is purely for convenience.

Tokens

You can think of tokens as the currency of LLMs. They're the way that LLMs measure how much text they have to process. Tokens are roughly 4 characters for most models. It's important when working with LLM APIs to understand how many tokens you're using.

Assignment

  1. While creating the key you can set a credit limit on it (for example, $1). This caps how much the key could ever spend, so even if you add credit later you won't be surprised by a big bill. On the pure free tier the key can't spend anything anyway, but it's a good habit to build.

  2. OPENROUTER_API_KEY='your_api_key_here'
    
  3. We never want to commit API keys, passwords, or other sensitive information to Git.

  4. import os
    from dotenv import load_dotenv
    
    load_dotenv()
    api_key = os.environ.get("OPENROUTER_API_KEY")
    
  5. from openai import OpenAI
    
    client = OpenAI(
        base_url="https://openrouter.ai/api/v1",
        api_key=api_key,
    )
    
    • model: the model ID, openrouter/free
    • messages: a list of message objects. For now, just a single user message. Each message is a dictionary with a role and content. Hard-code the prompt exactly like this:
      messages = [
          {
              "role": "user",
              "content": "Why is Boot.dev such a great place to learn backend development? Use one paragraph maximum.",
          }
      ]
      

If everything is working as intended, you should be able to run your code and see the model's response in your terminal!

Because openrouter/free picks a different model on each request, you may occasionally get a model that misbehaves. If that happens, just retry - or swap openrouter/free for a specific free model ID (anything ending in :free, e.g. openai/gpt-oss-20b:free) from the models page.

Submit the CLI tests.