Building with AILesson 15 of 80

Module 2 · Prompting as engineering

Prompt caching, and the ordering rule that pays for itself

In this lesson

  • 8 min read
  • 5 reading sections
  • Knowledge check
  • Lesson 5 of 10 in this chapter

This chapter’s goalTurn a prompt that works once into a versioned, cached, example-driven interface with a stated behaviour when the model does not know, and justify with cost and latency numbers whether a given task needs a longer prompt, a chain of smaller ones, a reasoning budget, or a fine-tune.

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
The same prompt, three orderings, three billsCaching off6000Caching on, timestamp atthe top7350Caching on, static blockfirst1140dollars per million calls1,800 static tokens and 200 variable ones, at $3 per million input, $0.30 cached read and $3.75 cachedwrite. A timestamp at the top gives every call a new prefix, so every call is a cache write andcaching costs more than not caching. The saving is in the ordering, not the feature flag.
1,800 static tokens and 200 variable ones, at $3 per million input, $0.30 cached read and $3.75 cached write. A timestamp at the top gives every call a new prefix, so every call is a cache write and caching costs more than not caching. The saving is in the ordering, not the feature flag.

Figure

A little at a time

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

Step 1 of 6Free · no account needed
Lesson steps

The waste that caching removes

A support assistant sends the same 1,800 tokens on every single call: system prompt, tool definitions, eight examples, a policy document. Only the last 200 tokens (the customer’s message) differ. Across a million calls you have paid for 1.8 billion identical tokens.

Prompt caching removes most of that bill. The mechanism is worth understanding because it dictates how you must write the prompt.

When a model processes your input it computes a key and value vector for every token (the KV cache) and this prefill is most of the cost and latency of a request with a long prompt. Because attention is causal, the KV entries for the first 1,800 tokens depend only on those tokens. If the next request begins with exactly the same 1,800 tokens, that work can be reused verbatim.

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 Python, from Zero, for AI exam.

Stuck on something?Ask Sayali, your AI tutor

How to cite: Addaly (2026) ‘Prompt caching, and the ordering rule that pays for itself’, in Building with AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/building-with-ai/prompt-caching (Accessed: 29 November 2026).