Context EngineeringLesson 41 of 87

Module 5 · Cost, caching and latency

Where the milliseconds go

In this lesson

  • 9 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 2 of 9 in this chapter

This chapter’s goalCut the price and the time-to-first-token of a call without changing what it returns: order the prefix by stability so the cache actually hits, measure the hit rate rather than assuming it, move non-interactive work to a batch lane, route by context size, and cap the paths through which a single user can spend an unbounded amount.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableNot available
Full audiobookNot availableNot available
Input sets the wait, output sets the rest0250050004120thousands of input tokensmilliseconds—— Time to first token– – Decode of four hundred output tokensOne model, four hundred output tokens, ten runs a point, medians. Prefill is compute-bound and growswith input; decode is bandwidth-bound and barely notices how long the input was. Streaming hides thelower line and does nothing for the upper one, which is why a five-second silence is fixed by cachingthe prefix or cutting the input, never by streaming.
One model, four hundred output tokens, ten runs a point, medians. Prefill is compute-bound and grows with input; decode is bandwidth-bound and barely notices how long the input was. Streaming hides the lower line and does nothing for the upper one, which is why a five-second silence is fixed by caching the prefix or cutting the input, never by streaming.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 7Free · no account needed
Lesson steps

Two phases with different physics

A model call has two stages and they are bottlenecked by different hardware.

Prefill processes your entire input. Every token is handled in parallel, so this stage is compute-bound: it saturates the arithmetic units of the accelerator. Its duration scales with how many input tokens there are. Prefill is what you are waiting for during time-to-first-token.

Decode produces the output one token at a time. Each token requires reading the whole model’s weights from memory, so this stage is memory-bandwidth-bound and largely indifferent to how long your input was. Its duration scales with how many output tokens you asked for.

That split explains most latency behaviour people find confusing.

Text
time_to_first_token  ≈ queue + prefill(input_tokens)
total_time           ≈ time_to_first_token + output_tokens × time_per_token

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Data, SQL and Getting to the Answer exam.

Stuck on something?Ask Sayali, your AI tutor

How to cite: Addaly (2026) ‘Where the milliseconds go’, in Context Engineering. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/context-engineering/prefill-decode-and-where-the-time-goes (Accessed: 29 November 2026).