Learn & Build Agents

    ↑ ↓ to choose · Enter to open · Esc to close

    01 FoundationsLesson 3 of 610 minBeginner

    Tokens, context and the cost of a call

    Tokens in this call5.006
    Cost of this call—
    Whole conversation—
    • + instructions, tools and policy
    • + the question
    Customerin the store chatAgent codebuilds each callbuscar_pedidotoolCall 1illustrative sizeswhat goes into the call5.006 tokensInstructions1.200Tools (description)800Return policy3.000Customer question6LLMClaude Sonnet 5.5where is 4805?builds the call
    LLMcodedataperson

    Step 1 of 10: Three words, five thousand tokens

    You will learn
    • What goes into a call to the model
    • What a token is, and why you cannot estimate it in your head
    • What counts in the context window
    • How the cost of a call is calculated
    • Why the bill grows with each agent turn
    • How the cache makes it cheaper, and how it breaks
    In the lab

    Average cost per conversation across the store's 50 test conversations, split into input, output and cache

    Coming soon: I will measure this on the example agent and publish the number with the report.

    The whole lesson as textEvery step, to read at once, search or print.
    1. Three words, five thousand tokens

      The customer writes "where is 4805?". The agent code does not send just that to the model: it builds a call with the instructions, the tool descriptions, the return policy and the question.

      The bar shows each piece at its real size. The customer's question is the small green mark at the end. The rest goes along in every call, because the model keeps nothing from one call to the next.

      The customer's message is the smallest part of what the model reads.

    2. The model counts in tokens, not words

      A is a piece of text: a whole word, part of a word, a number or a symbol. The model reads and writes tokens, and the bill is counted in tokens.

      How many characters fit in a token depends on the model and the language. The newest Claude models produce about 30% more tokens for the same text than earlier ones, and text in languages other than English usually takes more. Do not estimate: the API counts for you, for free.

      Measure your texts with the API count, not a rule of thumb.

    3. Everything fits in one window, and everything counts

      The is the most tokens a call can hold. On current Claude models, 1 million.

      Everything counts: instructions, tool descriptions, messages, tool results and the answer the model writes. If the input alone goes over the limit, the API rejects the call with an error.

      The is the sum of everything the model reads in this call, plus what it writes.

    4. Two price tables: input and output

      The price is per million tokens, separate for what goes in and what comes out. On Claude Sonnet 5.5, US$ 2 for input and US$ 10 for output.

      In this call, 5.006 input tokens cost US$ 0,0100 and 200 output tokens, US$ 0,0020. Output is 5 times more expensive per token, but it is short.

      Cost of a call = input tokens × input price + output tokens × output price.

    5. Each agent turn is a new call

      In an , each turn of the is a new call to the model. The code sends everything again and adds what arrived since the last one: the previous answer and the tool result.

      The fixed part does not change. The history grows with each turn, 1.200 tokens per turn in this example. That is why call 8 costs more than call 1, even with the same question.

      The history grows with each turn, and each turn pays for all of it.

    6. What weighs on the bill is repeated input

      Adding up the 8 calls: 73.648 input tokens and 1.600 output tokens. Input is 90% of the bill, even though output costs 5 times more per token.

      And fitting in the window does not mean it is worth putting in. Quality drops when the context swells. Cut whatever does not help the model decide this answer.

      In an agent, the bill is almost all repeated input.

    7. The cache: paying less for the repeated start

      The start of the call repeats unchanged, turn after turn. With the on, the API stores that start. On the next call, reading the stored part costs 5% of the input price on Sonnet 5.5.

      Writing costs 25% more than normal, so the first call costs more. From the second one on, the bill drops. The cache lasts 5 minutes and renews with each use.

      The cache makes the repeated part cheaper, as long as it comes unchanged and at the start.

    8. A clock at the top breaks the cache

      The cache only hits when the start of the call is identical, down to the last character. Change one line, and everything from there on is written again.

      A time with seconds on the first line of the instructions changes on every call. Whatever varies goes at the end, after the fixed part. Changing the tool descriptions also wipes the whole cache.

      Fixed part at the start and unchanged; what changes, at the end.

    9. Manus measures the cache before anything else

      What the source says: Manus, which builds agents for long tasks, published in July 2025 that in its agents input is close to 100 times larger than output. And that the cache hit rate is the most important metric of an agent in production.

      Our reading: it is this lesson's picture at a larger scale. And today, on Sonnet 5.5, reading from the cache costs 20 times less than paying full price.

      In a real agent, looking after the cache is looking after the bill.

    10. How much a conversation costs

      Each call carries everything again: instructions, tools, policy and the history, which grows with each turn. The bill is counted in tokens, input and output separately, and in an agent almost everything is repeated input.

      The cache makes the repeated part cheaper, if it comes unchanged and at the start. The question that remains: with all of this in the text, why does the same question still get different answers?

    Questions in this lesson

    What counts in the context window?
    Instructions, tools, messages, tool results and the answer. Everything the call carries counts, and so does the answer.
    Why does call 8 cost more than call 1?
    Because it sends again the history, which grew with each turn. The model does not remember. Call 8 carries everything from the 7 before.
    The cache is on, but the bill did not drop. What is the first suspect?
    The start of the call changes every time, like a time at the top. The cache only hits with an identical start. What changes goes at the end.