AI BENCHNOTES
CHAPTER 001Cache tokensContext windows
AI BENCHNOTES
PLAIN-LANGUAGE VIEWChoose a question.
12 CHAPTERS / EVERYDAY EXAMPLES / NO CODE REQUIRED

Start with a familiar question. Each chapter introduces the standard technical term.

00Plain-language topic guide
CHAPTER 001PROMPT CACHING

Prompt caching:reuse the matchinginput prefix.

Prompt caching:reuse the repeatedbeginning.

When eligible requests share an exact input prefix, the service can reuse intermediate work for that prefix. The uncached suffix is processed normally, and the output is generated fresh.When several AI requests begin with the same long instructions, the service may reuse the work from that shared beginning. It still writes a new answer each time.

  • AUTOAUTOMATICon eligible requestswhen the service allows it
  • EXACTSAME STARTprefix matches onlythe beginning must match
  • 1,024+LONG INPUTStokens on OpenAIminimums vary by provider
REQUEST_002.PROMPT LIVE
INPUT PREFIX4,608 TOKENS
systemrules
toolschemas
repocontext
newtask
CACHE LOOKUPHIT

4,352 input tokens reused cached work

09:41:02 prefix hash matched

09:41:02 attention state restored

09:41:02 only fresh tail processed →

01 / OVERVIEW

Prompt caching reuses input processing, not generated output.The repeated reading can be reused. The answer cannot.

For an eligible exact prefix, the service restores reusable intermediate state and processes only the uncached suffix before generation.The service can skip repeating work for the identical beginning, then read the new ending and write a fresh response.

REQUEST#1
System promptInstructions800
Tool schemasTool descriptions1,400
Shared contextShared material2,200
Task A200
MISSFIRST READBuilds cachereads everything
REQUEST#2
System promptInstructions800
Tool schemasTool descriptions1,400
Shared contextShared material2,200
Task B180
HITREUSED4,400 reusedshared start reused
REQUEST#3
System promptInstructions800
Tool schemasTool descriptions1,400
Shared contextShared material2,200
Task C240
HITREUSED4,400 reusedshared start reused
The practical prompt shape:

Put long shared instructions first. Put the request-specific material at the end.

02 / INTERACTIVE EXAMPLE

Put stable content before changing content.Put shared reading before today’s question.

Choose a workload, then move its changing block to the front. Because matching is prefix-based, that one ordering change removes the reusable prefix.Choose an example, then move today’s changing material to the front. The service must reread everything after the first difference.

PROMPT COMPOSERREADING ORDERCoding agentCode helper
REQUEST AFIRST REQUESTwarms the cachereads everything
01System promptInstructionsrole, rules, coding standardsrole, rules, and house style820 tknREADREAD FIRST
02Tool schemasAvailable actionsshell, search, patch, testsearch, edit, and check1,380 tknREADREAD FIRST
03Repository contextProject backgroundrepo map, AGENTS.md, conventionsproject map, notes, and conventions2,250 tknREADREAD FIRST
04Current taskToday’s taskFix the flaky auth testFix an unreliable sign-in check230 tknREADREAD FIRST
REQUEST BNEXT REQUESTtries to reuse ittries to skip rereading
01System promptInstructionsrole, rules, coding standardsrole, rules, and house style820 tknCACHEREUSED
02Tool schemasAvailable actionsshell, search, patch, testsearch, edit, and check1,380 tknCACHEREUSED
03Repository contextProject backgroundrepo map, AGENTS.md, conventionsproject map, notes, and conventions2,250 tknCACHEREUSED
04Current taskToday’s taskAdd CSV export to reportsAdd spreadsheet downloads to reports230 tknFRESHNEW
LONGEST MATCHING PREFIXREUSABLE START4,450 tokens can be reused4,450 tokens do not need rereading

Stable rules, tools, and repository contextThe shared instructions and project background stay the same

95%input cachedreading reused
cached prefixshared beginning4,450tokens
fresh tailnew ending230tokens
prompt shapereading orderstatic → dynamicshared → changingreusable prefix preservedeasy to reuse
03 / PREFIX MATCHING

Reuse stops at the first difference.

Cache matching moves from the start of the request toward the end. It can reuse the exact prefix before the first difference, but it cannot skip a change and resume later.

COMPARE TWO REQUESTSChange any block in Request B.
ACACHED
You area precisesupport agent.Follow policy v4.Use JSON.Tools: lookup, refund.Customer #1842asks about returns.
BNEW
REUSABLE PREFIX7 of 8 blocks

Most of the prefix remains reusable because the first difference is near the end.

04 / COST EXAMPLE

Repeated prefixes make the savings accumulate.

The first request processes the full input. Later matching requests can receive the provider's cached-input rate for the shared prefix.

Fixed example: a 6,000-token shared beginning. Cached reads cost 25% of a normal read. Every request also has a fresh 400-token tail.

64%
LESS INPUT COST

in this simplified 12-request example

Excludes output tokens, cache-write premiums, minimum cacheable lengths, and provider-specific pricing. This is intuition math, not a provider quote.

05 / WHERE CACHING HELPS

Look for long, repeated beginnings.

The ideal workload repeats a long, identical setup and changes only a small tail. In AI engineering, that pattern is everywhere.

STRONG FIT

Coding agents

Reuse the system prompt, tool definitions, repo map, and coding conventions. Append today’s task at the end.

rulestoolsrepotask
static setup → “fix flaky test”
STRONG FIT

Support copilots

Keep policies, product facts, tone rules, and tool schemas stable. Add the customer conversation last.

policycatalogexampleschat
stable playbook → customer issue
STRONG FIT

Batch extraction

Reuse a large extraction schema and worked examples. Put each new document after that fixed instruction set.

schemaexamplesdocument
schema + examples → new document
Likely to benefit
  • Long system prompts
  • Stable tool definitions
  • Repeated few-shot examples
  • Shared reference documents
  • Multi-turn conversations with a stable beginning
Less likely to benefit
  • Short one-off prompts
  • Everything changes every request
  • Personalization injected at the top
  • Randomly reordered tools or examples
  • Traffic too sparse for the cache to stay warm
06 / CACHE HIT SEQUENCE

The anatomy of a cache hit.What happens when the beginning matches.

  1. 1
    ROUTELABEL

    Fingerprint the beginningGive the beginning a matching label

    The service hashes an initial prompt prefix and uses it to route similar requests toward the same cache neighborhood.The service turns the start of the request into a short label. Requests with the same label can be sent toward the same reusable work.

  2. 2
    LOOK UP

    Find the longest exact prefixFind the longest identical beginning

    The cache checks for matching prompt state. If several prefixes match, the longest useful one wins.The service compares the request from left to right. If several earlier beginnings match, it reuses the longest one.

  3. 3
    RESUMECONTINUE

    Restore state, process the tailReuse the shared reading, then read the new ending

    Cached attention state is loaded. The model processes only the uncached suffix, then generates a brand-new response.The service reuses the work from the shared beginning, reads the new ending, and then writes a completely fresh answer.

07 / PRACTICAL RULES

Five rules for cache-friendly prompts.

STATICFIRST.DYNAMICLAST.
  1. 01

    Put reusable content first.

    System instructions, tool schemas, examples, and shared context lead the prompt.

  2. 02

    Put user-specific content last.

    Names, timestamps, live data, current tasks, and conversations belong at the tail.

  3. 03

    Keep serialization deterministic.

    Keep tool order, object keys, examples, and formatting stable. The serialized prefix must match exactly.

  4. 04

    Group related traffic when your API supports it.

    A stable prompt cache key can improve routing for requests that share the same long prefix.

  5. 05

    Measure actual hits.

    Log cached-token counts and hit rates. Prompt structure only matters if requests actually reuse it.

08 / API EXAMPLE

What it looks like in an API response.

You usually don’t “fetch” a prompt cache yourself. Send a stable prefix, then inspect usage metadata to see what was reused.

const response = await client.responses.create({
  model: "your-cache-capable-model",

  // Keep this stable for requests with the same prefix.
  prompt_cache_key: "coding-agent:repo-42:v3",

  input: [
    { role: "system", content: SYSTEM_PROMPT },
    { role: "developer", content: TOOL_SCHEMAS },
    { role: "developer", content: REPO_CONTEXT },

    // The changing part goes last.
    { role: "user", content: currentTask }
  ]
});
09 / MISCONCEPTIONS

Three common misconceptions.

MYTH #1

“It caches the answer.”

Prompt caching reuses work done while processing the input. Output generation still runs again.

INPUT
ONLY
MYTH #2

“Semantically similar is close enough.”

Similar meaning is not enough. Reuse applies only to the exact matching prefix.

EXACT MATCH
MYTH #3

“More cached tokens means lower quality.”

The computation is reused, not approximated. Same prefix, same state—just less repeated prefill work.

SAME RESULT
10 / CHECK YOUR UNDERSTANDING

Where does the cache stop?

Request B changes one character inside the tool schema, then returns to content identical to Request A.

Choose an answer.
RELATED CHAPTERS

Continue with a related concept.