Skip to main content

Prompt Caching

The objection: a router that switches models mid-conversation must be throwing away the prompt cache.

The measurement: it does not. Router plus caching beat caching alone on one fixed model on every dataset.

EvaluationSampleRouter + caching vs caching alone
WildChat-1M simulation, general chat30,769 multi-turn conversations68.7% cheaper
DevGPT simulation, developer chat1,011 conversations46% cheaper
Real agent traces, provider cache accounting95 sessions, 8,174 API calls37.4% cheaper
TwinRouterBench static track81 multi-step instances44 to 50% cheaper
  • A switch is not an eviction. When a session returns to a model it used earlier, the cache is still there.
  • 4,684 real switch-backs on live gateway traffic: 97.4% found the cache warm at a 5-minute TTL, 99.3% at a 1-hour TTL.
  • The expensive mistake is the opposite one. A router with caching switched off costs about 4x caching one fixed model.
  • Savings accounting is honest about it. The all-frontier baseline is priced with a warm cache on continuing turns, and a fresh cache write after a switch counts against the saving. See Reported savings.

When to pin anyway​

  • session_affinity is off by default, and that is the right setting for most routers. The numbers above show it is not needed for the cache, and pinning forfeits the savings from routing later turns down a tier.
  • Turn it on when a tier switch would change behavior the client depends on. Two kinds of case:
    • Provider state in the history. History produced by one model can fail on another: an Anthropic thinking block or cache_control marker replayed to a non-Anthropic tier, or tool-call formats that differ between providers. Pinning keeps the session on the model that produced the history.
    • Consistency the user can see. Each model has its own style. A design workflow that generates layouts, shapes, or components over many turns keeps one look only if every turn comes from the same model; a writing assistant that should keep one voice is the same case.
  • Single-family ladders rarely need it. When every tier is the same provider (all Claude, all GPT), history replays cleanly and the cache measurements above apply. Leave it off and let follow-ups route down.
  • A tier with several deployments behind it also needs deployment_affinity and the prompt_caching pre-call check in router_settings, so continuing turns return to the deployment holding the cache.
  • Both together: Coding agents with load balancing.

Caching across load-balanced deployments​

Separate from the router: the prompt_caching pre-call check keeps Anthropic caching working when the same model is load balanced across deployments or AWS accounts.

🚅
LiteLLM Enterprise
SSO/SAML, audit logs, spend tracking, multi-team management, and guardrails — built for production.
Learn more →