Chapter C45 · Interactive Course

Why responses take time and cost money

Prefill, decode, caches, memory limits and end-to-end budgets.

Start chapter in web app

Core Learning Concept

Read the receipt by separating input processing, output generation, cache use, and repeated attempts. Then calculate the cost of the completed task rather than one isolated call. Use the supplied measurements to identify a practical saving and state which other costs or delays the receipt does not expose.

What You’ll Learn in This Chapter

  1. Prefill reads the input

    Activity 1 of 8 · 4 min

    Identify processing that happens before the first generated token.

    Prefill is the processing of supplied input before autoregressive output generation. Longer inputs can require more work, though hardware, batching, and caching affect the time. Time to first output can also include network and queue delays. Use the trace to separate these contributors rather than attributing the whole wait to reading.

    Start activity →

  2. Decode generates the continuation

    Activity 2 of 8 · 4 min

    Distinguish generation steps from input processing.

    Decode is the repeated generation of continuation tokens in an autoregressive model. Each new token becomes context for later steps. Streaming can expose output while this process continues, but products may buffer chunks. A visible pause may come from generation, tools, or delivery, so inspect the relevant measurements.

    Start activity →

  3. A KV cache reuses attention work

    Activity 3 of 8 · 4 min

    Explain why stored keys and values can avoid repeated computation.

    A key-value cache stores attention vectors from prior token positions. In standard causal decoding, appending a token reuses past keys and values without recomputing them. This runtime cache uses GPU memory to accelerate generation.

    Start activity →

  4. Prompt caching and answer caching differ

    Activity 4 of 8 · 4 min

    Distinguish reused prefix computation from reusing a previous response.

    Prompt or prefix caching reuses compatible computation for repeated input while still allowing a newly generated response. Answer caching returns a previously stored result. The two have different invalidation and freshness requirements. Inspect which cache is involved before expecting a cost saving or trusting an old answer.

    Start activity →

  5. Memory capacity can limit a run

    Activity 5 of 8 · 4 min

    Identify weights and temporary state as separate memory demands.

    Runtime memory can include model parameters, activations, key-value caches, and implementation overhead. Longer context and more concurrent requests can increase requirements. A model’s supported context limit does not mean every device can run that context comfortably. Evaluate memory under the actual model, precision, hardware, and workload.

    Start activity →

  6. A local interface can call a remote model

    Activity 6 of 8 · 5 min

    Trace where inference and tools actually run.

    A local interface is a program running on your device. Its model inference may still occur remotely, and connected tools may contact additional services. These are separate facts. Trace the configured provider and connections before concluding that installing an app locally keeps all processing and data on the device.

    Start activity →

  7. Budget the whole task

    Activity 7 of 8 · 4 min

    Account for input, output, tool work and retries.

    True task cost includes failed runs, retries, input context, outputs, and external tool calls. A cheaper token price often ends up costing more overall if success rates are poor. Always calculate total cost per successful outcome.

    Start activity →

  8. Read a request receipt

    Activity 8 of 8 · 5 min

    Locate prefill, decode, cache use and repeated work in two fictional receipts, and explain the tradeoff without naming a winning model.

    Start activity →