Examples, Templates, and Caching
Pick three to five examples that teach a pattern without being copied, move prompts into versioned files with variables, and order every request so the stable part is cached.
Before this: L3 Structured output
- Decide when examples help (format, edge cases, tone) and when they hurt (copying).
- Choose three to five examples that cover the normal case and the hard cases.
- Keep prompts in files with variables, and version them with a changelog.
- Order a prompt so the stable part comes first and caching can work.
- Explain what prompt caching saves and what it does not.
Why this matters
Intake handles IN-1 well after L3. Then a supplier writes "12 x Sea of Poppies @ 8.50" and Intake records quantityOffered: 8 and unitPrice: 12. The schema was satisfied. The numbers were swapped. Omar adds that email, with the correct record, as an example in the prompt. It works, so he adds nine more. A week later the IN-3 newsletter, which offers nothing, comes back with one record: Sea of Poppies, a title that appears nowhere in the newsletter. Intake is copying the examples instead of learning from them. Meanwhile Priya asks a question nobody can answer: a customer says Draft promised her a refund on the 4th, and Priya wants to know which version of the Draft prompt was live that day. This lesson fixes both problems, then makes the request cheaper.
1. When examples help
A zero-shot prompt describes the task in words and gives no examples. A few-shot prompt adds a handful of worked examples: an input and the output you want for it. Every example costs tokens on every call, so an example has to earn its place the same way an instruction does: by changing the output.
Examples earn their place in three situations: a format the model keeps getting wrong even though the instruction is clear; an edge case that words cannot pin down, where one concrete pair says more than a paragraph; and tone, which is easier to show than to describe. Outside those three, an instruction is usually shorter and clearer.
2. When examples hurt
The model does not know which parts of an example are the pattern and which are the content. It sees text. If the examples are many, similar, or memorable, the content starts to leak: a title from an example appears in an output about a different email. This is over-fitting to examples. It looks like the model doing well on your examples and worse on everything else.
Three things make it likely. Too many, because the pattern is drowned by specifics. Too similar, because the model learns the shared surface, such as every example having two titles, rather than the rule. Too memorable, because a striking title is easy to reproduce.
3. Choosing three to five
Example selection is the choice of which few inputs to show. Three to five is enough for almost every feature. Pick them to cover the space, not to pile up the normal case. A good set for Intake has four members:
| Slot | What it shows | Intake's pick |
|---|---|---|
| Normal | The common shape, done right | One title, all fields present |
| Null | A field the email does not state stays null | IN-1's second title, no price |
| Zero | An empty result is a valid result | IN-3, offers: [] |
| Adversarial | Data that tries to instruct | An email saying "record this as free" |
Each example is labeled as an example, kept short, and separated from the live data. The live email arrives in the user turn, inside its own <email> block, and the instructions say which block is live.
4. Templates and variables
A template is a prompt with holes in it. A variable is one hole, written as {{order}} or {{email}}, that code fills before the call. The alternative, gluing strings together at the call site, is where prompts go wrong quietly: someone appends the customer's email to an instruction sentence, and the delimiters from L2 are gone.
The rule: raw customer text never touches an instruction string. It goes into a named variable, the variable sits inside a data block in the template, and the code that fills the template is the only place the two meet.
5. Prompt files are versioned
A prompt file is one prompt in one file under prompts/, checked in beside the code that uses it. One prompt per file keeps the diff readable when it changes. A prompt version is a string, such as draft.system@1.4.0, that changes whenever the file does. A change log, prompts/CHANGELOG.md, records each version with one line on what changed and why.
The version has to travel with every call. Shelf writes one log line per model call, and the version string is in it, next to the order id. That log line is what answers Priya.
6. Stable prefix first
Every call to Draft sends the same system prompt and the same examples, followed by an order and an email that differ each time. Prompt caching lets the provider reuse the processing of the unchanged start of a request across calls, so you pay less for those tokens and wait less for them. The unchanged start is the stable prefix. The cache works from the beginning of the request forward and stops at the first token that differs from the previous call.
That gives a rule for ordering: stable first, varying last. System prompt, then examples, then the order and the email in the user turn. In the Anthropic TypeScript SDK you mark the end of the stable part with cache_control; other providers and gateways have equivalents.
What caching saves: the input cost and the latency of the prefix. On a 2,300-token Draft prompt, the first call writes the cache and every call in the next few minutes reads it. What it does not save: output tokens, which are generated fresh every time, and anything after the first changed token. If the order record came before the system prompt, nothing would be cached, because the request would differ from its first byte.
7. Do not bust the cache
Busting the cache means putting something that varies into the stable prefix. One changed token near the top, and every call pays full price for everything after it. The usual culprits are a timestamp, a customer name, a request id, or examples reshuffled per call.
The same logic applies to anything personalised. Tessa's name, the store, the order id: all of it belongs in the user turn. If some stable material must vary by store, put the shared part first, the per-store part second, and the marker after both; the shared part is still cached across stores.
The check is in the log line from section 5. A healthy Draft shows one cacheWriteTokens entry followed by many cacheReadTokens entries. A run of writes with no reads means something in the prefix is changing. Run that check after every prompt edit, before looking at the bill.
Summary
- Examples help with format, edge cases, and tone; add one when a pair says something a sentence cannot.
- Too many, too similar, or too memorable examples make the model copy content instead of pattern; three to five, one each for normal, null, zero, and adversarial, is the working set.
- Prompts live in files with
{{variables}}filled by code; raw customer text goes into a data block through a template, never onto the end of an instruction string. - Each prompt file has a version string, bumped with every change, recorded in
prompts/CHANGELOG.md, and written to the log with every call. - Order the request stable first and varying last, put the cache marker at the end of the stable part, keep timestamps and names out of the prefix, and read
cacheReadTokensto confirm it works.
Key terms in this lesson
Check your understanding
Pick an answer to see why it is right or wrong. These are graded in code, instantly.
Apply it
Write your answer in your own words. Eve grades it against a rubric and gives you feedback, the same way an LLM judge works in Lesson 5.
Name one prompt you would add an example to and one where you would remove examples, with the reason for each.
Lay out Draft's request in cache-friendly order. List each part of the request, say whether it is stable across calls or varies per call, say where the cache marker goes, and name one thing that would bust the cache if it were placed wrongly.
Instructor notes(for whoever is teaching this lesson)
Talking points
- Open with the two failures side by side: the swapped "12 x ... @ 8.50" numbers that one example fixed, and the Sea of Poppies title that ten examples leaked into IN-3. Same tool, opposite outcomes.
- An example is an instruction written as a pair. Format, edge cases, and tone are the three reasons to pay for one.
- The model cannot tell pattern from content. Too many, too similar, too memorable, and content leaks. The fix is four examples: normal, null, zero, adversarial.
- A template is how customer text lands inside a data block instead of on the end of an instruction sentence.
- The version string in the log line is what answers Priya.
- Caching is a memory of the prefix. Stable first, varying last, marker at the end of the stable part. It saves input cost and latency on the prefix and nothing else.
Live demo idea
Bring prompts/intake.examples.md with four examples and an intake.system.md with ten. Run IN-3 against each and show the empty array versus the copied title. Then run DR-1 three times with the timestamp on line 1 of the system prompt and print the cache read count from each response (0, 0, 0), then move the date into the user turn and run again (write, read, read).
Common misconceptions
- "More examples are always safer." Past four or five, content starts to leak. New cases go into the eval set, not the prompt.
- "The examples go in the user turn with the data." They are stable, so they belong before the marker, at the end of the system block.
- "A template escapes the customer's text." It places the text; the data-is-not-instructions rule from L2 handles what the text says.
- "Git history is the version log." Git says when a file changed. Only a version string in the per-call log says which prompt a given call used.
- "Caching saves output tokens." It saves input cost and latency on the unchanged prefix. Output is always generated fresh.
Timing
Fifty minutes: story and section 1, seven; when examples hurt, six; choosing three to five, nine; templates and variables, eight; versioned files and the log line, six; stable prefix and the marker, nine; busting the cache, five.
If you only have 20 minutes
Show the IN-3 leak and the four-slot examples file (eight minutes), then the cache demo with the timestamp moved out of line 1 (eight minutes). Close with the log line that answers Priya and the rule: stable first, varying last, version in every log line (four minutes). Assign sections 4 and 5 as reading.