By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Most AI applications spend money and latency on redundant API calls and token processing. A single user might ask the same question twice. A batch of users might ask semantically identical questions. Your system might re-read a 10,000-token system prompt for every request. Caching layers intercept these patterns and eliminate wasted work before it reaches the model.
Caching is the single biggest cost and latency lever in most AI apps, and there are three layers that operate at different points in the request flow. Each catches a different type of redundancy.
request
│
▼
┌─────────────────────┐ hit → return stored answer (no model call)
│ 1. RESPONSE CACHE │ exact same input (after normalising)
└──────────┬──────────┘
▼ miss
┌─────────────────────┐ hit → return stored answer (no model call)
│ 2. SEMANTIC CACHE │ "similar enough" question, found by embeddings
└──────────┬──────────┘
▼ miss
┌─────────────────────┐ hit → model still runs, but skips re-reading
│ 3. PROMPT CACHE │ the repeated PREFIX (cheaper and faster)
└──────────┬──────────┘
▼
MODELNormalise the input by trimming whitespace and lowercasing if it's safe to do so, then hash it and look it up. This works well for FAQs and idempotent lookups where the same exact question returns the same exact answer.
Your cache key must include everything that changes the answer: the model version, the prompt version, the tenant ID, the language, and any relevant settings. Miss any of these and you'll serve the wrong cached answer to the wrong context.
Embed the incoming question, search for a previously answered question that is close enough in embedding space, and return its cached answer. Questions like 'How do I reset my password?' and 'password reset steps?' can share one answer.
This is powerful but risky. A false hit returns a wrong answer with full confidence. Tune the similarity threshold on real data, not theory. Never share cached answers across users when the answer depends on private data or permissions. Set TTLs; stale answers after a policy change become a real incident.
Major providers can reuse internal computation for a repeated beginning of the prompt. The model still generates a fresh answer, but you don't pay full price or wait to re-read the same long prefix again.
Cache hits require an exact prefix match. One changed character early in the prompt breaks the cache for everything after it. Cached input is billed at a large discount, sometimes roughly 90% off, though there may be a small premium for the first write. Entries expire after a short time-to-live, typically a few minutes on some providers, that refreshes on use. Providers also set minimum prompt lengths before caching applies.
Structure your prompt so static content comes first, then dynamic content last. This maximizes the size of the cached prefix.
GOOD (cache-friendly) BAD (cache-hostile) ┌────────────────────────┐ ┌────────────────────────┐ │ system prompt (static) │ │ Time: 14:07:33 ← new │ │ tools (static) │ │ system prompt │ │ policy docs (static) │ │ tools │ │ ── cache boundary ── │ │ user question │ │ user question (dynamic)│ └────────────────────────┘ └────────────────────────┘ the cache never hits
Never put timestamps, request IDs, or user names at the top of the prompt. These change on every request and destroy the cache boundary before it even gets interesting.
Log cache hit rate per route, and calculate the cost delta with and without hits. Watch time to first token before and after a change. If hit rate is low, look for dynamic text near the start of the prompt—that's usually the culprit.
Rules and prices differ across providers and change over time. Check your provider's prompt-caching documentation for current specifics.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.