Lesson 02 · Foundations
Tokens, Context & the Cost of a Call
The three numbers behind every scoping conversation — plus your first real API call.
Here's a moment you will live many times as an FDE: a customer describes a feature — "summarize every support ticket as it comes in" — and looks at you. Can we do it, what will it cost, will it be fast enough? Answering that on the spot takes exactly three numbers: input tokens, output tokens, and the context window. This lesson gives you all three, then you'll make your first real API call and read them off the meter yourself.
Tokens are the meter
From Lesson 01 you know the model reads and writes tokens — chunks of text, usually part of a word. Rough rule for English prose: 1 token ≈ ¾ of a word, so 1,000 tokens is roughly 750 words.1 Everything is denominated in tokens: pricing, speed, and limits. Words don't matter; tokens do.
And tokenization isn't uniform. Code, JSON, and rare words split into more tokens per word than plain prose — a config blob can cost triple what an equivalent-length sentence does. That's why an FDE never estimates from word counts: you count tokens (the API has a free endpoint for exactly this — it's Part 1 of your lab).
What a call costs
Pricing is per million tokens, and here's the part people miss: output tokens cost about 5× more than input tokens. Reading is cheap; generating is expensive — every output token is one pass of the next-token loop.1 Current Claude pricing2 as of mid-2026:
| Model | Tier | Input $/1M | Output $/1M | Context window |
|---|---|---|---|---|
| Claude Opus 5 | Complex work | $5.00 | $25.00 | 1M tokens |
| Claude Sonnet 5 | Balanced | $3.00 | $15.00 | 1M tokens |
| Claude Haiku 4.5 | Fast & cheap | $1.00 | $5.00 | 200K tokens |
Input: 2,000 × $5/1M = $0.010 · Output: 500 × $25/1M = $0.0125 → ~2.3¢ per conversation.
At 10,000 conversations/day: ≈ $225/day ≈ $6,750/month. Now you can discuss whether Haiku at ~$1,350/month is good enough for this task. That conversation is FDE work.
The context window is the model's working memory
The context window caps how much the model can consider at once — prompt plus answer. Modern Claude models take 1M tokens (~2,000 pages); Haiku takes 200K.2 Two practical consequences: anything the model must "know" has to fit, and since input tokens cost money, stuffing the window is a cost decision too. "Just paste in all 10 years of customer docs" is rarely the right answer — that instinct is what leads to RAG, a few lessons from now.
Latency: the answer length is the clock
Reading input is fast and parallel; generating output happens one token at a time through the loop from Lesson 01. So response time is dominated by output length, not input length. Two levers every FDE uses: cap or prompt for shorter outputs, and stream the response so the user sees words immediately — the time to first token is what a user feels, and streaming makes a 20-second answer feel instant.
🧪 Lab: your first API call
Time to touch the metal. The lab file is in your workspace at
labs/0002-first-api-call.py. One-time setup, then run it:
# one-time setup, from the repo root (PowerShell)
pip install -r requirements.txt
copy .env.example .env # then paste your key from console.anthropic.com
python labs/0002-first-api-call.py
Three parts, each proving something from this lesson with a tight feedback loop:
Part 1 counts tokens on four strings — watch JSON cost ~2× the tokens of prose.
Part 2 makes a real call and reads response.usage — the exact meter
you'll use to price customer features — then computes the dollar cost.
Part 3 streams a response and measures time-to-first-token versus total time,
so you see that generation, not reading, is the slow part. The whole lab costs under a cent.
No chat here — this box replaces it. Copy the prompt into any AI assistant (Claude, ChatGPT, Gemini…), then paste your work after it.
You are a senior AI engineer reviewing my work: the output of my first LLM API lab (labs/0002-first-api-call.py) plus my scoping math. My paste includes token counts for four test strings, the response.usage numbers from a real call with my computed dollar cost, and my time-to-first-token vs. total-time measurements from a streamed call. Grade each item as Strong / Adequate / Missing, with one sentence of evidence: - I correctly read input vs. output tokens off the usage meter and my dollar math applies the right per-million rates to each. - I noticed and can explain why JSON/code tokenized heavier than plain prose. - My latency interpretation attributes total time to output generation, and I can say why TTFT matters to a user. - My scoping estimate (cost per call and per month) is arithmetically right and states which of input or output dominates. Be skeptical — challenge my weakest number first. Then ask me 2–3 follow-up questions a customer would ask about cost or latency. Finish with the single highest-leverage improvement. My work follows below.
Check yourself — scope the feature
Same drill as Lesson 01: customer scenarios, and you diagnose from the mechanism. Don't scroll up. Wrong picks stay live.
Scenario A
A customer wants 100-page contracts summarized into one paragraph each. Costs are too high in the pilot. Which lever cuts cost the most?
Scenario B
Users complain the assistant "hangs" for 15 seconds on detailed answers, though short answers feel fine. What's the primary cause?
Scenario C
To save money, a team moves a workflow to Haiku and pastes an entire knowledge base (~350K tokens) into every prompt. Calls now fail. Why?
Recommended learning
Hand-picked follow-ups if you want to go deeper on tokens and cost. None are required — the primary source above comes first.
- Article Why LLM APIs charge by tokens: a clear guide to input, output, and context costs A tidy, practical walkthrough of exactly this lesson's billing model — good second exposure in different words.
- Article LLM API pricing comparison: every major model, ranked by cost — CloudZero Cross-provider pricing in one place. Useful reference when a customer asks "what about GPT/Gemini?" — prices vary >600× across models.
- Article Notes on "Let's build the GPT Tokenizer" — Simon Willison A concise written digest of the video below — the tokenization quirks (spelling, arithmetic, why YAML beats JSON for token count) in five minutes of reading.
- YouTube Let's build the GPT Tokenizer — Andrej Karpathy (~2 hr, optional deep dive) Builds a real tokenizer from scratch, then explains the LLM quirks tokenization causes. Advanced — save it for when tokens feel comfortable.
- YouTube Deep Dive into LLMs — tokenization chapter — Andrej Karpathy The gentler option: the tokenization chapter of the Deep Dive video covers the same ideas at introduction level. Use the chapter markers.
References
- Chip Huyen, AI Engineering (O'Reilly, 2025) — tokens, sampling, and inference economics.
- Anthropic, Claude models & pricing documentation — model tiers, per-token pricing, and context windows (verified July 2026).