← Ai Gateway / ai-gateway / features
Caching
When caching is enabled, AI Gateway can cache responses from your AI model providers, serving them directly from Cloudflare's cache for identical requests.
Benefits of Using Caching
- Reduced Latency: Serve responses faster to your users by avoiding a round trip to the origin AI provider for repeated requests.
- Cost Savings: Minimize the number of paid requests made to your AI provider, especially for frequently accessed or non-dynamic content.
- Increased Throughput: Offload repetitive requests from your AI provider, allowing it to handle unique requests more efficiently.
Default configuration
Caching is disabled by default. To enable caching globally, set the default caching configuration:
To set the default caching configuration in the dashboard:
- Log into the Cloudflare dashboard ↗ and select your account.
- Select AI > AI Gateway.
- Select Settings.
- Enable Cache Responses.
- Change the default caching to whatever value you prefer.
To set the default caching configuration using the API:
- Create an API token with the following permissions:
AI Gateway - ReadAI Gateway - Edit
- Get your Account ID.
- Using that API token and Account ID, send a
POSTrequest to create a new Gateway and include a value for thecache_ttl.
When caching is enabled globally, the default caching behavior applies to all requests that support caching. You can also opt individual requests into caching or override their cache settings with per-request headers.
To check whether a response comes from cache or not, cf-aig-cache-status will be designated as HIT or MISS.
How the cache key works
By default, AI Gateway constructs the cache key by concatenating the following and hashing the result with SHA-256:
- Provider (for example,
openai,anthropic) - Endpoint (the API path)
- Model (for example,
gpt-4o) - Provider authentication header (for example, the
Authorizationbearer token) - Full request body
This means caching is based on exact match of the entire request. Any difference in the body — including messages, tools, or model parameters — will result in a separate cache entry. To override this behavior, use the custom cache key header.
Per-request caching
While your gateway's default cache settings provide a good baseline, you might need more granular control. These situations could include data freshness, content with varying lifespans, or dynamic or personalized responses.
To address these needs, AI Gateway allows you to override default cache behaviors on a per-request basis using specific HTTP headers. This gives you the precision to optimize caching for individual API calls.
The following headers allow you to define this per-request cache behavior:
Skip cache (cf-aig-skip-cache)
Skip cache refers to bypassing the cache and fetching the request directly from the original provider, without utilizing any cached copy.
You can use the header cf-aig-skip-cache to bypass the cached version of the request.
As an example, when submitting a request to OpenAI, include the header in the following manner:
# Run `wrangler whoami` to get your account ID to replace $CLOUDFLARE_ACCOUNT_ID,
# and `wrangler auth token` to get an auth token to replace $CLOUDFLARE_API_TOKEN.
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--header "Content-Type: application/json" \
--header "cf-aig-skip-cache: true" \
--data '{
"model": "openai/gpt-4.1-mini",
"messages": [
{
"role": "user",
"content": "how to build a wooden spoon in 3 short steps? give as short as answer as possible"
}
]
}'Cache TTL (cf-aig-cache-ttl)
Cache TTL, or Time To Live, is the duration a cached request remains valid before it expires and is refreshed from the original source. Use cf-aig-cache-ttl to set the caching duration for a request that already uses caching. To opt an individual request into caching, include cf-aig-cache-key. The minimum TTL is 60 seconds and the maximum TTL is one month.
For example, if you set a TTL of one hour, it means that a request is kept in the cache for an hour. Within that hour, an identical request will be served from the cache instead of the original API. After an hour, the cache expires and the request will go to the original API for a fresh response, and that response will repopulate the cache for the next hour.
As an example, when submitting a request to OpenAI, include the header in the following manner:
# Run `wrangler whoami` to get your account ID to replace $CLOUDFLARE_ACCOUNT_ID,
# and `wrangler auth token` to get an auth token to replace $CLOUDFLARE_API_TOKEN.
# Use a key shared only by requests with equivalent responses.
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--header "Content-Type: application/json" \
--header "cf-aig-cache-key: responseWithCustomTtl" \
--header "cf-aig-cache-ttl: 3600" \
--data '{
"model": "openai/gpt-4.1-mini",
"messages": [
{
"role": "user",
"content": "how to build a wooden spoon in 3 short steps? give as short as answer as possible"
}
]
}'Custom cache key (cf-aig-cache-key)
The cf-aig-cache-key header lets you override the default cache key and opts the request into caching.
Choose a custom key that groups only requests with equivalent responses. Requests with the same custom key share a cached response. When you use the cf-aig-cache-key header for the first time, you will receive a response from the provider. Subsequent requests with the same custom key value will return the cached response. If you include cf-aig-cache-ttl, the request uses that value for its cache TTL. Otherwise, the request uses the default cache TTL configured for the gateway. For requests that include cf-aig-cache-key, the cache TTL is 5 minutes when neither cf-aig-cache-ttl nor a default gateway cache TTL is configured.
As an example, when submitting a request to OpenAI, include the header in the following manner:
# Run `wrangler whoami` to get your account ID to replace $CLOUDFLARE_ACCOUNT_ID,
# and `wrangler auth token` to get an auth token to replace $CLOUDFLARE_API_TOKEN.
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--header "Content-Type: application/json" \
--header "cf-aig-cache-key: responseA" \
--data '{
"model": "openai/gpt-4.1-mini",
"messages": [
{
"role": "user",
"content": "how to build a wooden spoon in 3 short steps? give as short as answer as possible"
}
]
}'