🎁 New Sign up free, 10 calls on us. Up to $1, no card needed.
LLM Prompt Caching: The Complete 2026 Guide (Cut Input Cost 50-90%)

LLM Prompt Caching: The Complete 2026 Guide (Cut Input Cost 50-90%)

Contents
  1. Where to enter
  2. Part 1 — How LLM Prompt Caching Works
  3. Part 2 — Compare LLM Prompt Caching Across Providers
  4. Part 3 — Working Python Tutorial
  5. Part 4 — Best Model by Use Case
  6. Part 5 — LangChain Integration
  7. How to read this
  8. Numbers in this series

If you ship a chatbot, a RAG app, or an AI agent against a large language model, prompt caching is the single optimization that gives you back 50–90% of input cost and 3–10× of time-to-first-token at no quality cost. It isn’t a bolt-on trick — it falls directly out of how Transformer attention is defined. Once you understand that, the rest of the stack (TTLs, provider differences, prompt structure) lines up cleanly.

TL;DR

  • Prompt caching returns 50-90% of input cost and 3-10x faster time-to-first-token at no quality cost.
  • Measured on 2026-05-25: Claude cache_control markers cut input cost 88-89%; GPT-5.4-mini auto-cache took TTFT from 3.6s to 0.73s; DeepSeek-v4-flash gave 74% off with a disk-backed cache.
  • The short TTLs exist because KV state is huge: roughly 10 GB for a 32K-token context on a 70B-class model.
  • DeepSeek caches at 64-token granularity vs the usual 1,024-token floor, so partial prefix matches still earn discounts.

This page is the index to a five-part series that takes you from the theory to a production decision matrix, and into the framework layer where prompts actually get assembled. Pick where to enter based on what you already know.


Where to enter

If you want to…Start at
Understand why caching exists and what KV cache actually isPart 1 — How KV Cache & TTL Work
Pick a provider and know what’s different about eachPart 2 — Compare Claude, GPT, Gemini, DeepSeek
Copy-paste working Python and measure your own numbersPart 3 — Working Python Tutorial
Match a chatbot / RAG / agent workload to the right modelPart 4 — Best Model for Chat, RAG & Agents
Cache correctly through LangChain (templates, tools, agents)Part 5 — LangChain Setups That Actually Hit

Each part stands alone but they’re written so reading them in order builds the picture without redundancy.


Part 1 — How LLM Prompt Caching Works

How LLM Prompt Caching Works: KV Cache & TTL Explained →

The architectural article. Walks through self-attention as a single equation, explains why the K and V vectors of a stable prefix are mathematically reusable, and shows how the memory-vs-compute tradeoff produces the TTL behavior every developer has to design around.

Key takeaways:

  • Prompt caching isn’t an optimization layered on top — it’s a direct consequence of causal-masked attention. K/V at position i is a deterministic function of tokens 1…i, so identical prefixes give bit-identical K/V.
  • Prefill (compute-bound, O(N²)) is what caching saves; decode (memory-bandwidth-bound, O(N) per token) is what every inference engine already optimizes.
  • TTLs exist because KV cache is enormous (~10 GB for a 32K context on a 70B model). 5 minutes is the GPU memory-pressure horizon; hours-to-days are only possible with disk-backed caches (DeepSeek’s MLA architecture).
  • Caching wins both cost (50–90% off input on cache hits) and latency (TTFT drops 3–10× for prompts in the 5–10K-token range and much more for 100K+).

Part 2 — Compare LLM Prompt Caching Across Providers

Prompt Caching Compared: Claude, GPT-5, Gemini, DeepSeek, Qwen (2026) →

The buyer’s guide. Five providers expose prompt caching in five very different shapes — explicit markers (Claude), fully automatic (GPT-5, DeepSeek-v4), hybrid implicit+explicit (Gemini, Qwen), or architectural disk-backing (DeepSeek’s MLA). The article gives a feature-by-feature comparison plus a 5-dimension evaluation framework to score them for your specific workload.

Key takeaways:

  • Don’t compare base prices — compare effective cost weighted by your hit rate (formula in §4.1); the live LLM price comparison and the cost calculator make this concrete for your workload.
  • Claude has the deepest single-call discount (~90%) but requires explicit cache_control markers.
  • DeepSeek-v4 is the only provider with disk-backed caches at scale; partial-prefix matches earn discounts because the granularity is 64 tokens instead of 1,024.
  • Gemini’s explicit cache costs hourly storage fees — break-even depends on call frequency.
  • API ergonomics, hit-rate predictability, TTL fit, latency under miss, and migration cost are the five dimensions that actually distinguish providers once you control for hit rate.

Part 3 — Working Python Tutorial

LLM Prompt Caching in Python: A Working Code Tutorial →

The hands-on article. One OpenAI SDK + one Anthropic SDK against a single gateway, with measured numbers from 2026-05-25 across the full Claude family (haiku-4-5 through opus-4-7), GPT-5.x, Gemini 2.5, DeepSeek-v4, and Qwen3.

Key takeaways:

  • Claude with cache_control markers: measured 88–89% cost reduction uniformly across haiku/sonnet/opus 4-x. Use the Anthropic SDK with base_url="https://synthorai.io/".
  • GPT-5.4-mini auto-cache: 5× TTFT improvement (3.6 s → 0.73 s on a 7K-token prompt), 93% cache hit rate on the system tokens.
  • Gemini 2.5-flash implicit: 88% cost reduction on cache hits when streaming usage is captured.
  • DeepSeek-v4-flash: 74% off, disk-backed (cache survives hour-scale idle).
  • TTL-aware patterns: keep-alive heartbeat for cron, prefix stability rules, what to log per call.

Part 4 — Best Model by Use Case

Best LLM for Chat, RAG & Agents: 2026 Model + Cost Decision Matrix →

The decision article. Different workloads pull the cost/latency levers differently — chat is naturally cache-friendly, RAG fights the prefix-stability problem, agents depend on cumulative prefix discipline. The article gives a model recommendation by workload shape with cost estimates.

Key takeaways:

  • Chatbots: any model with auto-cache works; sessions hit naturally. Pick on cost/quality. gpt-5.4-nano cheapest, gpt-5.4-mini fastest cached TTFT, claude-haiku-4-5 best instruction-following at modest premium.
  • RAG: retrieved-doc reordering kills mid-prompt cache hits. Three fixes — push references to the end, deterministic chunk ordering, or Claude’s multi-cache_control breakpoints.
  • Agents: tool calls and results must be append-only and byte-identical step-to-step. claude-sonnet-4-5 with 4 cache_control markers gives the strongest cumulative-prefix discount; gpt-5.4-mini works without code changes at 50% savings.
  • TTL match: 5 min for chat, 1 hour for agents with human-in-the-loop steps, disk-backed for sporadic batch.

Part 5 — LangChain Integration

LangChain Prompt Caching: Setups That Actually Hit the Cache →

The framework article. Everything in Parts 1–4 assumes you control the prompt bytes; LangChain assembles them for you, and its most convenient syntax silently disables Claude’s cache. Measured against langchain-core 1.4.8 with a marked-up system prefix.

Key takeaways:

  • The ("system", "...") string-tuple template cannot carry cache_control: measured zero cache activity on identical calls. The fix is a SystemMessage with content blocks.
  • Prompt order is the hit-rate lever: retrieved RAG context placed before the static rules made every call a cold write, which costs more than not caching at all on Claude’s write premium.
  • A marker on the system block covers bound tools too; bind_tools serializes byte-stable, and a marker on an Anthropic-format tool dict passes through verbatim.
  • Multi-turn agents: slide the marker to the latest message and each turn re-reads the whole prior prefix while writing only the delta (measured: read 1,864, write 15).
  • On automatic-cache models (GPT, GLM, DeepSeek) mis-ordering fails silently: no premium, no error, just a discount that never arrives. Watch the usage fields.

How to read this

  • Engineer new to the topic: read in order. The architecture in Part 1 makes Parts 2–4 click instantly.
  • PM or architect doing vendor selection: jump to Part 2 + Part 4. Reference Part 1 if a teammate asks “but why TTL exists”.
  • Engineer with a specific workload to ship today: Part 4 first (find your row in the matrix), then Part 3 for the exact code.
  • Already on LangChain: Part 5 directly — the raw-SDK patterns in Part 3 translate, but the pitfalls (string templates, variable placement, usage-field names) are framework-specific.
  • Anyone optimizing an existing app: Part 3 §6 cross-provider benchmark — reproduce it against your own prompt; that’s a one-day exercise, not a multi-week migration.

Numbers in this series

Measured numbers in Parts 1–4 were captured on 2026-05-25, and Part 5’s LangChain measurements on 2026-07-04, against the Synthorai gateway (https://synthorai.io/v1 for OpenAI-compat, https://synthorai.io/ for Anthropic-native), single-tenant, single sequential run, no concurrent load. Your numbers will move with region, time-of-day, and competing tenant load — treat them as a starting point and reproduce against your own traffic before quoting them.

Pricing tables and TTL behavior reflect vendor public documentation as of 2026-05. Providers update these every few months; the architectural reasoning (Part 1) is stable, the comparative numbers (Part 2 & 3) drift.

← Back to blog