Skip to Content

Cloudflare AI Gateway caching

Cloudflare AI Gateway caching serves identical requests from Cloudflare’s edge cache instead of calling the provider again — faster responses and lower provider bills.

Hub: Tokenminning with Cloudflare AI Gateway. Concept: Prompt caching.

Enable caching

In the Cloudflare dashboard → AI Gateway → your gateway → Caching:

  1. Enable caching for the gateway route
  2. Set TTL (time-to-live) for cached responses
  3. Send identical requests — second call hits cache

See Caching docs .

What gets cached

Request typeCache benefit
Identical prompt + model + paramsHigh — full response cached
Same system prompt, different user messageLow — full body differs
Streaming responsesConfig-dependent

Gateway caching deduplicates identical requests. For multi-turn agents with changing user messages, stabilize prefixes and rely on provider-side prefix caching too.

Cache vs provider prefix cache

LayerMatchesSavings
AI Gateway edge cacheIdentical full requestZero provider call
Provider prefix cacheStable system prompt prefixCheaper input tokens
Semantic cacheSimilar meaningApp middleware

Spending guardrails

  • Enable caching on classification and extraction endpoints (high repeat rate)
  • Set reasonable TTL — stale cache is cheaper than stale answers
  • Monitor cache hit rate in Analytics
  • Do not cache user-specific responses with PII unless TTL is very short

Troubleshooting

Cache hit rate near zero — requests differ slightly (timestamps in prompt, random seeds). Normalize prompts before sending.

Stale responses — lower TTL or disable caching for real-time features.

Last updated on