Lab 04 · API activity
Your first model API calls.
Use a small course-funded account to try language and embedding models from Python. No prior API experience is expected.
What are we doing?
An application programming interface (API) lets one program ask another program to do something.
When you use a chatbot website, the website handles the request for you. In this lab, your Python code will send the request directly: choose a model, provide some text, and inspect the result that comes back. This is how developers add model capabilities to their own programs.
An API key is the pass that tells the service which account should pay for a request. Treat it like a password and do not commit it to GitHub or paste it into a shared document. But do not panic: this is a practice account with course credit capped at $40. It cannot expose your bank account, Yale password, or computer. If you accidentally share the key, replace it and let us know.
This lab is a safe place to experiment. Try small changes, look at the responses, and notice how each request uses a little of the available credit.
A 70-minute plan
Three short chunks, plus an optional deep dive.
- 0–5 minutes: get your course key and open the notebook.
- 5–40 minutes: complete the core API experiments.
- 40–60 minutes: open HW4, choose Colab or local setup, and run
tiny_rnn.py. - 60–70 minutes: discuss pseudocode expectations and attempt one midterm practice question.
Part 1
Call a model from Python
Open the API lab in Colab
Store your key as a Colab secret, check your budget, see how many models your key can reach, make one chat request, and compare text embeddings. Continue into the optional experiments if you finish early, including one prompt sent to five models from frontier to tiny.
Open a runnable copy → Notebook fileDownload the notebook
Use this copy if you prefer Jupyter or VS Code. Set OPENROUTER_API_KEY in your environment rather than writing the key into the notebook.
.ipynb →
Get your course key
Yale SOM Token Fund
This portal is brand new, and our class is the first to use it—tell us if anything is confusing or breaks. Sign in and copy the OpenRouter key beginning with sk-or-. The separate token beginning with ytf is for the portal itself and cannot call a model.
Part 2
Get Homework 4 running
Run the HW4 starter in Colab
Open a ready-to-run notebook that downloads and executes tiny_rnn.py. Use this route if you want to begin the assignment immediately.
Open the HW4 starter repository
The README walks through cloning or downloading the starter, creating a local .venv, and running the same program from a terminal.
Part 3
Practice showing your reasoning
What counts as good pseudocode?
Read the short expectations guide, then attempt one question without notes or AI. Clear logic matters more than exact Python syntax.
Open the practice questions → Check after attemptingPractice answer key
Compare the ideas and sequence of steps, not just the surface syntax. Several pseudocode forms can express the same correct procedure.
Open the answer key →Optional deep dive
Open or rebuild the Week 4 RNN explainer
Guess what comes next
Revisit tokenization, bigrams, hidden states, training, temperature, and fading memory in the interactive Week 4 explainer.
Open the explainer → Local build experimentDownload the explainer source
Unzip the project to inspect how the page is assembled. The included build notes describe the local workflow used to regenerate its narrated video.
Download the source bundle →