Lesson 02 · Foundations

Tokens, Context & the Cost of a Call

The three numbers behind every scoping conversation — plus your first real API call.

FDE skill · size and price a solution before you build it
🎧 Listen to this lesson · ~5 min · narrated audiobook edition

⏱ ~6 min read · 🎧 5 min listen · ✎ 3 quizzes · 🧪 ~30–45 min lab

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

Why JSON is pricey Tokenizers learn frequent character sequences from prose, so braces, quotes, colons, and indentation each tend to become their own token. Karpathy's tokenizer lecture (in the recommended learning below) demos this live — the same data as YAML tokenizes measurably cheaper than JSON. When a customer pipeline ships structured data on every call, format choice is a real line item.

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:

ModelTierInput $/1MOutput $/1MContext window
Claude Opus 5Complex work$5.00$25.001M tokens
Claude Sonnet 5Balanced$3.00$15.001M tokens
Claude Haiku 4.5Fast & cheap$1.00$5.00200K tokens
Check the rate before you quote it These are list rates, and Anthropic runs introductory pricing on new tiers — Sonnet 5 is $2/$10 per 1M through 31 August 2026 before it settles at the $3/$15 above. The arithmetic in this lesson works either way; the habit that matters is pulling live numbers off the pricing page on the day you put a figure in front of a customer.
Worked example — the scoping math Customer support bot on Opus 5: each conversation sends ~2,000 input tokens (system prompt + ticket + history) and generates ~500 output 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 pricing footnote worth knowing Anthropic's pricing page lists prompt caching: when calls repeat the same large prefix (a system prompt, a policy document), the cached read is billed at a fraction of the normal input rate. For the support-bot math on the left, that mostly-identical 2,000-token prefix is exactly the shape caching discounts — check it before quoting a monthly figure.

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.

🤖 Get your work reviewed

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?

Primary source — read this
The canonical book for this exact career path. Chapter 2 covers tokens, sampling, and the model-as-a-service economics behind this lesson. The companion repo (linked) is free; the book itself is worth owning for the whole journey.
Your one tangible win Give yourself the test: a customer wants ticket summarization at 50,000 tickets/month, ~1,500 input + 300 output tokens each. You can now estimate the monthly cost on two models, say which of input or output dominates, and check it fits the context window — before writing a line of code. That's a scoping conversation you can lead.
Stuck? Lab won't run? API key trouble? Curious why output tokens cost more? Paste the error (or the relevant lesson section) into any AI assistant (Claude, ChatGPT, Gemini…) and ask — and for feedback on your lab output and scoping math, use the review-prompt box in the lab section above.

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.

References

  1. Chip Huyen, AI Engineering (O'Reilly, 2025) — tokens, sampling, and inference economics.
  2. Anthropic, Claude models & pricing documentation — model tiers, per-token pricing, and context windows (verified July 2026).