

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Large Language Models
incomplete
2: OpenRouter API Setup
incomplete
3: Spell Correction
incomplete
4: Query Rewriting
incomplete
5: Query Expansion
incomplete
This lesson's interactive features are locked, please to keep using them
For our purposes, we don't need to be super specific about which LLM we use. Any decent, modern model should work. What we want is:
We'll use OpenRouter, a single gateway that exposes hundreds of models - including a rotating selection of free ones - behind an OpenAI-compatible API. That means we can use the official openai Python SDK to talk to it, a skill that transfers to nearly every other provider.
Instead of pinning one specific model, we'll use the special model ID openrouter/free, a router that automatically picks an available free model that supports the features our request needs.
The free tier is rate-limited to roughly 50 requests per day (20 per minute). That's plenty for this course if you're a little patient. If you'd like 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 costs nothing - the $10 is purely for convenience.
So let's get set up with an OpenRouter API key and verify that it works by writing a small test script. High-level steps:
.env file to store the API key, which should be kept secret.dotenv library in your Python code to load the environment variable.openai library (pointed at OpenRouter) to make API requests.While creating the key you can set a credit limit on it (e.g. $1) to cap how much it could ever spend. On the pure free tier it can't spend anything anyway, but it's a good habit.
OPENROUTER_API_KEY="your_api_key_here"
We never want to commit API keys, passwords, or other sensitive information to Git.
uv add python-dotenv==1.1.0
uv add openai==2.44.0
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.environ.get("OPENROUTER_API_KEY")
if not api_key:
raise RuntimeError("OPENROUTER_API_KEY environment variable not set")
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 with a single user message. For now, hardcode the prompt:
messages = [
{
"role": "user",
"content": "Why is Boot.dev such a great place to learn about RAG? Use one paragraph maximum.",
}
]
The method returns a chat completion object. Print response.choices[0].message.content to see the model's answer.
If everything is working as intended, you should be able to run your script and see the model's response in your terminal!
Prompt tokens: X
Response tokens: Y
The response has a .usage property that has both:
prompt_tokens property (tokens in the prompt)completion_tokens property (tokens in the response)OpenRouter is an external web service, and it may rate limit your project (especially on the free tier). On lessons like this one, failed submissions won't penalize you, so it's safe to retry if something goes wrong on the provider's end.
Not every lesson in this course is no-penalty, so read the instructions carefully and follow the submit flow each lesson asks for.
Submit the CLI tests.