LEARN · PHASE 01 · LESSON 7

Tokens — why text becomes currency

You can look at a bill, a limit, or a "full" warning and read the actual

Type
Learn
Prerequisites
Lesson 6, What a Language Model Is
Time
~30 minutes

Learning Objectives

  • Estimate how many tokens a piece of text costs, within a factor of two
  • Predict which of two versions of a prompt is more expensive before running it
  • Explain why a token count is not a character count
  • Find out how many tokens a session has used, from inside the session

The Problem

Two sessions, same task, same repo, wildly different bills. One agent read six files and edited two. The other read six files, edited two, and re-read the same four files three times each because a search came back with too many candidates.

Nothing was different in difficulty. Nothing was different in outcome. The second session just sent more text. And nobody in the loop could see that, because nothing on screen said "that file cost you 14,000 of your 200,000."

Text feels weightless. You can scroll past a thousand lines without a sensation of expense. But the model does not read — it counts, and everything is priced in the same unit.


The Concept

The model cannot work with characters. It was trained on tokens: the sub-pieces of text produced by a tokenizer, a small trained program that splits input into units. Roughly, a token is about four characters of English or about three-quarters of an English word.

The important part is not the ratio. It is that a token can be part of a word:

TextTokens (roughly)Why
` ind1rare, so the tokenizer keeps it whole
indentation2–3common prefix, split to save space
authenticate3–4leading space merges with the word
supercalifragilistic6+rare enough to shatter

So token count is not characters, not words, and not anything you can eyeball from character length alone. It is a property of the tokenizer, and every model family has its own. That is why a token count from one model is not a valid prediction for another.

flowchart TD
    A["You type: fix the auth bug"] --> B[Tokenizer splits into tokens]
    B --> C["~6 tokens here"]
    C --> D["Everything else already in the conversation"]
    D --> E["Model reads ALL of it, every single turn"]
    E --> F[New response]
    F --> G[Response joins the conversation permanently]
    G --> D
Mermaid source — renders as a diagram where mermaid.js is available

That loop is the whole economics. Every turn re-sends the entire conversation. Tokens are not spent once; they are spent again on every subsequent request. A long session does not cost more because it did more work — it costs more because each of its turns is more expensive than the last.

The reframe: tokens are the only unit that matters, and the conversation

re-pays for itself on every single turn — so a long session is a tax on every

message you send, not a record of work already done.

Where the tokens actually go

In a Claude Code session, the conversation is only part of it. Also in there, from the start:

  • instructions the harness sets for itself
  • your CLAUDE.md and any memory files
  • the tool definitions the agent can call
  • environment details — working directory, platform, shell

And accumulating as you work:

  • every file read, at full length
  • every command's output
  • the agent's own responses

One cat of a large log file can cost more than an hour of conversation. This is why "just read the whole thing to be safe" is the most expensive thing an agent can do.

The number you can actually see

Inside a Claude Code session:

/context

It draws the current window as a coloured grid, broken down by category, with suggestions for what to cut. That grid is the honest answer to "where are my tokens going" — better than any estimate, because it reads the real thing.

Token prices, context sizes, and every command named here move. Check code.claude.com/docs and claude --version rather than trusting a number in this lesson.


Build It

1. Get a real count for a file

wc -c COURSE-CONTEXT.md      # bytes

Characters ÷ 4 is a rough English estimate. Good enough to compare two files. Not good enough to quote as a price.

✅ You pass when you have a character count and a stated estimate, and you know the estimate is approximate.

2. Measure the thing that surprised you

Find the largest file the agent read during a recent session, and the largest command output. Compare them to the total conversation so far:

ls -S | head -5

If one read dwarfs the conversation, you have found why a session cost what it cost.

✅ You pass when you can name the single largest token source in a real session and its rough size.

3. Predict before you run

You are about to ask for a broad sweep: "find every place that calls auth() and report each one with its surrounding function."

Predict: many file reads, each landing in context permanently. Write the prediction down.

Now run it. Then open /context and see what it actually cost.

✅ You pass when your written prediction and the measured result agree on which category grew most.

4. Halve it on purpose

Take the same sweep and constrain it:

Find every place that calls `auth()`. Only report the file path and line number.
Do not read surrounding code — just locate the call sites.

Compare /context before and after. Narrowing what comes back is as effective as narrowing what goes out.

✅ You pass when the second sweep measurably grew context less than the first.

5. Catch the re-read

The classic waste: an agent searches, gets twenty candidates, reads all twenty, finds the answer in the third. Nineteen reads, one result, all twenty still in context.

Watch it happen in /context. Then note what a better ask looks like: give the search enough to be narrow, and say what evidence you need back.

✅ You pass when you have identified one real session where most of the cost was reads that turned out to be unnecessary.


Use It

ConceptWhen you reach for it
/contextAny time you want to know where the tokens went
chars ÷ 4Comparing two files, before asking for either
Predict-then-measureBefore any broad read or sweep
Narrow the output, not just the inputCutting cost without cutting the answer
Repeated broad searchesRecognising the most common silent waste
Longer session = pricier turnExplaining a bill that surprised you

Exercises

  1. Take any file in this repo over 2,000 lines. Estimate its token count from character length, then have the agent report the actual count it saw in context. Record both. ✅ You pass when both numbers are in your notes and you can state the error of your estimate as a percentage.
  1. Run one broad sweep and one narrowed sweep of the same task. Record /context growth for each. ✅ You pass when the second cost measurably less and you can name the single phrase that did it.
  1. Find the highest-token command output you have ever pasted into a session. Count it. Write down what you should have asked for instead. ✅ You pass when your replacement request is three lines or fewer and returns strictly what you needed.

Key Terms

TermWhat people sayWhat it actually means
token"a word"A sub-word unit from a trained tokenizer. Roughly four English characters, but a split point inside a word, so length in characters does not predict it reliably.
tokenizer"the splitter"The trained program producing those units. Each model family has its own, which is why token counts are not portable across models.
input tokens"the prompt"Everything sent, including conversation history, file contents, tool definitions, and system instructions — not just what you typed.
output tokens"the reply"What the model generated, including thinking where extended thinking is on.
context"the chat"The full body of text the model sees on the next turn. Grows with every message, read, and tool result.
re-send"it remembers"The entire conversation is transmitted again on every request. Long sessions tax each new message.

What this lesson skipped

Exact pricing and per-model rates. They change, and quoting them here would be stale within weeks. Read them from the costs page at the time you need them.

The context window as a budget. Tokens are counted here; what the window is, what fills it, and what happens when it fills — that is Lesson 8, and it depends on this lesson.

Prompt caching. Reading a file the same way twice can be cheaper the second time. It is real, it is measured in the /usage output, and it changes how you reason about repeated reads — but explaining it properly needs the context window, so it lands after Lesson 8.

Token counting APIs and libraries. Exact counting is a one-line call in most SDKs. Useful, not foundational, and not needed for anything in this course.

Thinking tokens. Billed as output. Arrives properly with model selection in Phase 03.

Check yourself

3 questions. Answer before you reveal — recognising the right option is not the same as being able to produce it.

  1. A session has been open for three hours. You ask one more short question and the reported cost jumps. Why is the short question not cheap?

    1. Short prompts are billed at a higher per-token rate to discourage them
    2. The whole conversation is sent again on every request, so the short question carries the whole session with it
    3. Three hours of wall-clock time accrues a background charge each time you send anything
    4. The rate limit from Lesson 11 adds a surcharge per request
    Show answer

    B. The whole conversation is sent again on every request, so the short question carries the whole session with it

    There is no per-request premium and no time-based surcharge — the cost is a direct function of tokens sent, and every request re-sends the accumulated conversation plus files read plus tool output. Option C gestures at background requests, which exist but are tiny and would not explain a jump that scales with session length. Option D reaches forward to a lesson not yet taught and describes a limit, not a price.

  2. You must locate every caller of parseConfig(). Which ask is most likely to keep tokens down without losing the answer?

    1. Find every place parseConfig is used and paste each match with 40 lines of surrounding context
    2. Search the whole repo for parseConfig and explain each hit
    3. List the file path and line number of every call site of parseConfig. Do not read surrounding code.
    4. Understand how configuration parsing works in this codebase, including every usage of parseConfig
    Show answer

    C. List the file path and line number of every call site of parseConfig. Do not read surrounding code.

    Cost is driven by what comes back into context, and file reads plus surrounding context are permanent. Asking for path and line only returns the evidence you need. Option A looks narrower than B but the 40 lines of context multiply the read count. Option D is a comprehension task disguised as a locate task, which drags in far more files than the question requires.

  3. You estimate a file's cost at characters divided by four, then the real count comes back noticeably lower. What is the most likely reason?

    1. The /4 rule only applies to prose, and code has a different average density
    2. You counted the wrong file's characters
    3. Common words are split into fewer tokens than the rule assumes, so the rule over-estimates
    4. Some of the file was served from cache and not billed
    Show answer

    C. Common words are split into fewer tokens than the rule assumes, so the rule over-estimates

    The /4 rule assumes roughly four characters per token. High-frequency English collapses into single tokens shorter than that average, which pushes the real count below the estimate. Option A is wrong because code is usually less compressible than prose, so it skews the other way. Option D is a real mechanism but it changes latency, not the token count you are comparing against.