AI Systems Reference/Cost & Capacity

Semantic Caching & Optimization

Exact vs semantic cache keys, embedding similarity thresholds, prompt caching from vendors, and model routing for cost-quality Pareto fronts.

4/5Overview: 35m

Caching layers for LLM apps

Cache typeKeyHit conditionStaleness risk
ExactHash(prompt + model + params)Identical requestLow
SemanticEmbedding of normalized queryCosine similarity > θMedium–high
Prompt prefixVendor-side prefix matchShared system promptLow (vendor feature)
RetrievalQuery embeddingSame KB stateInvalidates on corpus update

Semantic caching mechanics

Query → embed → nearest neighbor in cache index → if sim > 0.92, return cached response

Parameters to tune:

  • Similarity threshold — higher = fewer false hits, lower hit rate
  • TTL — FAQ content: hours; dynamic data: minutes or none
  • Normalization — lowercase, strip whitespace, canonicalize entities

GPTCache and Redis + vector extensions are common implementations.

Stale answer risk

Semantic cache will serve wrong answers when:

  • Underlying facts changed ("CEO of X")
  • User intent differed subtly ("cancel subscription" vs "pause subscription")
  • Policy rules updated

Mitigations:

  • Never cache high-stakes actions (payments, medical)
  • Include corpus_version in cache key metadata
  • Shorter TTL for time-sensitive domains

Vendor prompt caching

OpenAI and Anthropic discount repeated input prefixes (system prompt, few-shot examples). Structure prompts as:

[Static prefix — cached] + [Dynamic user context — not cached]

Can cut input cost 50–90% for agent loops with large system prompts.

Model routing for cost-quality

RouteLLM / custom classifiers send easy queries to cheap models:

Query classModel$/1M tokens (illustrative)
FAQ, extractionSmall$0.10
General chatMedium$3
Complex reasoningFrontier$15

Train router on labeled query difficulty; monitor quality regression on routed traffic.

Prompt compression

Techniques:

  • Summarize conversation history instead of full verbatim
  • Retrieve top-3 chunks, not top-20
  • Use smaller model to compress context before frontier call

Measure quality impact on eval set — compression is lossy.

When not to cache

  • Personalized responses (user-specific state)
  • Regulated audit requirements (must log fresh inference)
  • Adversarial surface (cache poisoning)

Interview framing

"Reduce inference cost 40% without killing quality":

  1. Prompt prefix caching for system prompts
  2. Semantic cache for top 20 FAQ intents (measure hit rate)
  3. Router: 60% traffic to small model with eval gate
  4. Trim retrieval from 10k to 3k tokens

Senior signal: Quote expected hit rate and stale risk — not just "add Redis."

Link forward

Shipping & Operations covers rolling out routing and cache config safely with feature flags.

Further Reading

Hands-On Tasks (Optional)

Design drills and architecture sketches — gateway SLOs, eval gates, rollout plans. Assumes AI Engineering fundamentals are already in place.

  • Design a semantic cache layer

    Specify: cache key (embedding of normalized query), similarity threshold, TTL, invalidation on corpus update, and expected hit rate for a FAQ-style workload. Note stale-answer risk.

    25m