Skip to content
AISeeker

Search

Search reports, topics, companies…

CH.05Tutorials
4 min read
Signal strength: 4/5Major
Verified · 5 Sources

Prompt caching and cost control: a practical playbook for the Claude and OpenAI APIs

How prompt caching is billed on Claude and OpenAI in late 2026, how to structure prompts so it actually hits, and which cost levers to pull next.

Key figures

0.05x
Cache-read rate, Opus 5.5 and Sonnet 5.5
1.25x
Short-TTL cache write (both providers)
-50%
Batch API discount

In most production LLM features (support bots, RAG, agents with long tool definitions) the bill is dominated by input tokens that repeat on every request: system prompt, reference documents, tool schemas, conversation history. Prompt caching lets the provider reuse its work on that prefix at a fraction of the normal rate. Both Anthropic and OpenAI now bill cache writes and reads explicitly, so structure matters. This tutorial covers measurement, prompt layout, caching on each API and the levers after caching. Prices are list prices checked in November 2026.

1. Measure before you optimise

Log the usage block of every response, per route. On Claude, total prompt size is input_tokens + cache_creation_input_tokens + cache_read_input_tokens; input_tokens is only the uncached remainder after the last breakpoint, so a small value there is good news. On OpenAI's Responses API, cached tokens appear in usage.input_tokens_details.cached_tokens. Per-route totals show which prompts are worth caching and, later, prove that caching works.

2. Design prompts for prefix reuse

Caches match exact bytes from the start of the prompt. On Claude the order is tools, system, messages; a change at one level invalidates it and everything after. Rules:

  • Frozen material first: tool definitions, instructions, reference documents, few-shot examples.
  • Per-request material last: the question, retrieved chunks, timestamps, request IDs.
  • Keep the prefix deterministic: sorted JSON keys, fixed tool order, no now() in the system prompt.
  • Append to conversations instead of rewriting earlier turns; summarising history resets reuse.

3. Caching on the Claude API

Mark the end of the shared prefix with cache_control. This Python example caches a long policy document:

python
import anthropic

client = anthropic.Anthropic()                 # reads ANTHROPIC_API_KEY
POLICY = open("support_policy.md").read()      # ~20K tokens, identical every call

def answer(question: str):
    resp = client.messages.create(
        model="claude-opus-5-5",
        max_tokens=16000,
        output_config={"effort": "low"},
        system=[{
            "type": "text",
            "text": POLICY,
            "cache_control": {"type": "ephemeral"},   # 5-minute TTL
        }],
        messages=[{"role": "user", "content": question}],
    )
    u = resp.usage
    print(f"write={u.cache_creation_input_tokens} "
          f"read={u.cache_read_input_tokens} uncached={u.input_tokens}")
    return resp

The first call writes the cache; calls in the next five minutes read it, and each read resets the timer. Use {"type": "ephemeral", "ttl": "1h"} only when requests sharing the prefix arrive 5 to 60 minutes apart. For multi-turn chat, a top-level cache_control field lets the API place and advance the breakpoint itself. Limits to know: up to four breakpoints per request, a minimum cacheable prefix of 512 tokens on current models (shorter prompts are silently not cached), and a 20-block lookback window for finding earlier entries in long conversations.

ModelInput5-min write1-hour writeCache readOutput
Claude Opus 5.5$4.00$5.00$8.00$0.20$20.00
Claude Sonnet 5.5$2.00$2.50$4.00$0.10$10.00
Claude Haiku 5.5 (prompts ≤100K)$0.10$0.125$0.20$0.01$0.50

Prices per million tokens, Claude API list prices.

The arithmetic for the example above: on Opus 5.5 the 20,000-token policy costs $0.08 per uncached call but $0.004 as a cache read. At 1,000 questions a day with traffic steady enough to keep the cache warm, that part of the bill falls from about $80 to about $4, plus a few $0.10 writes after quiet periods.

4. Caching on the OpenAI API

OpenAI enables caching by default on supported models. For GPT-5.6 and later, the Responses API adds breakpoints: in implicit mode OpenAI places one at the latest eligible message, and in explicit mode you mark them yourself (up to four cache writes per request). The minimum cacheable prefix is 1,024 tokens.

json
{
  "model": "gpt-6.2-sol",
  "prompt_cache_options": { "mode": "explicit", "ttl": "30m" },
  "input": [
    {
      "role": "developer",
      "content": [
        {
          "type": "input_text",
          "text": "<support policy, ~20K tokens>",
          "prompt_cache_breakpoint": { "mode": "explicit" }
        }
      ]
    },
    { "role": "user", "content": "Can I get a refund after 30 days?" }
  ]
}

Writes cost 1.25x the input rate for the 30-minute TTL and 2.5x for the 12-hour option; reads cost 0.1x, or 0.05x on the GPT-6.1 and GPT-6.2 models. Older models keep the earlier scheme: automatic prefix caching, a prompt_cache_key to improve routing, and prompt_cache_retention set to in_memory or 24h.

5. Beyond caching: the next levers

  • Batch anything that can wait. Both providers discount batch jobs by 50% with a 24-hour window. On Claude, caching discounts stack with batch pricing, though hits inside a batch are best-effort; Anthropic cites hit rates of 30% to 98%, and suggests the 1-hour TTL for batches with shared context.
  • Tune reasoning effort per route. Classification and short chat rarely need high effort; coding and agents often do.
  • Route by difficulty, carefully. A cheaper model that needs retries is not cheaper per completed task, and caches are model-scoped, so a cascade loses cache reuse.
  • Trim what you send. Cap tool outputs and drop stale retrieved chunks; cached tokens still cost something on every read.

Pitfalls and next steps

  • Silent invalidators. A timestamp in the system prompt, unsorted JSON or a tool list that varies by user drives reads to zero with no error.
  • Below the minimum. Prompts under 512 tokens (Claude) or 1,024 tokens (OpenAI GPT-5.6+) are processed without caching.
  • TTL counts from the request start. A four-minute generation leaves about a minute before a 5-minute Claude entry expires.
  • Scope. Claude caches are per workspace; traffic split across workspaces writes separate entries.
  • Overpaying for long TTLs. If requests arrive less than five minutes apart, the 1-hour TTL only adds cost.

Add an integration test that sends the same request twice and asserts a non-zero cache read, and chart cached versus total input tokens after every prompt change.

Researched and drafted with AI assistance under editorial review. Report errors to support@aiseeker.dev.

Sources

This report synthesises the sources below. Primary sources are marked.

  1. [1]
    Prompt caching

    Anthropic (Claude Platform Docs)2026.11.28Primary

  2. [2]
    Pricing

    Anthropic (Claude Platform Docs)2026.11.28Primary

  3. [3]
    Batch processing

    Anthropic (Claude Platform Docs)2026.11.28Primary

  4. [4]
    Prompt caching

    OpenAI API Docs2026.11.28Primary

  5. [5]
    Batch API

    OpenAI API Docs2026.11.28Primary

Back to Tutorials →