Tokens: how models measure text
Explain that models read and write text as tokens (word pieces). Explain that files, command output and conversation all consume tokens, which sets both memory limits and cost.
One sentence, 52,000 of something
You give the agent (an AI tool that can read your files and run commands for you) a first task: find why a login test fails. It opens one file, runs the tests and answers. Your tool then shows the conversation now holds `52k tokens` (52,000), yet you typed one short sentence.
What is being counted? And how did one sentence turn into 52,000 of it?
Models read and write word pieces
Models don't see letters or whole words. Text is chopped into **tokens**. Common words are usually one token, and rarer words split into pieces: `unbelievable` might become `un` + `believ` + `able`. The model takes in all the tokens it's given, then writes its reply one token at a time.
Rule of thumb for English: 1 token ≈ 4 characters ≈ ¾ of a word. Code, symbols, long numbers and non-English text usually need more tokens per word.
Everything it touches is tokens
All of these count:
- your messages
- the agent's replies
- every file it reads
- every command output it sees (the text a command prints)
- hidden instructions: text the tool adds to every conversation, telling the model how to behave and what actions it can take
They pile up in one running conversation, and the model reads all of it again at each step. That has two effects:
1. **Memory limit:** there is a hard maximum of tokens the conversation can hold at once.
2. **Cost:** usage is counted in tokens read (input) and tokens written (output). Because the whole conversation is read again at every step, each step costs more as the conversation grows.
Where the 52k came from
Roughly what the conversation holds:
- Hidden instructions: about 13,000.
- Your sentence: about 10.
- Reading `src/auth.py` (300 lines of code): about 4,000.
- Running the whole test suite: 2,500 lines of log, about 35,000.
- The agent's actions and answer: a few hundred.
Most of that log was passing tests that had nothing to do with login. A better request: run only `tests/test_auth.py` and stop at the first failure. You get the same answer for far fewer tokens, and the agent has more room left for real work.
Same question, very different token bill
Both versions read the same file. Only the test run differs: the first lets the full log into the conversation, while the second asks for just what's needed. Token figures are approximate. Some tools cut very long output short, but don't rely on it.
# Costly version
▸ You: Why does test_login fail?
⚙ Read: src/auth.py
(~4,000 tokens)
⚙ Run: pytest
(2,500 lines of output, ~35,000 tokens)
# Lean version
▸ You: Why does test_login fail? Run only
tests/test_auth.py, stop at the first
failure.
⚙ Read: src/auth.py
(~4,000 tokens)
⚙ Run: pytest tests/test_auth.py -x -q
(40 lines of output, ~600 tokens)
Remember
Models read and write **tokens**, word pieces of roughly 4 characters of English. Everything the agent sees costs tokens: your words, its replies, hidden instructions, the files it opens and the command output it gets back. The whole conversation is re-read at every step, so tokens set both how much it can hold at once and what you pay.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.