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.

  1. 0–5 minutes: get your course key and open the notebook.
  2. 5–40 minutes: complete the core API experiments.
  3. 40–60 minutes: open HW4, choose Colab or local setup, and run tiny_rnn.py.
  4. 60–70 minutes: discuss pseudocode expectations and attempt one midterm practice question.

Part 1

Call a model from Python

Start here · core path about 35 minutes

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 file

Download 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.

Download .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.

Open fund.som.chat/account/tokens →

Part 2

Get Homework 4 running

Method 1 · no installation

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 Colab starter →
Method 2 · local Python

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.

Read the setup instructions →

Part 3

Practice showing your reasoning

Midterm practice · questions first

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 attempting

Practice 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

Interactive lecture

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 experiment

Download 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 →