Skip to main content

How a request gets its cache action

The gateway decides one cache action for every request, in this order:
  1. Cache rules. If the organization has cache rules, the gateway evaluates them against the request and the virtual key. The first rule that matches sets the action.
  2. Provider default. If no rule matches and the request resolves to an Anthropic model, the gateway applies force on its own. This default only runs when the body is 4096 bytes or larger and the client is not already managing its own breakpoints: on /v1/messages any cache_control marker in the body disables it, on /v1/chat/completions only a top-level cache_control does.
  3. Passthrough. Otherwise the body reaches the provider unchanged.
The response header X-LangWatch-Cache-Mode reports the outcome: respect, disable or force when a rule fired, auto when the provider default fired. The header is absent when neither a rule nor the provider default changed the body.

Actions

There is no time-to-live action. The TTL and salt fields on a rule are stored but the gateway does not read them.

What force injects

On POST /v1/messages (Anthropic shape), the gateway sets cache_control: {"type": "ephemeral"} on the last system block and on the last content block of the last message. Any cache_control the client already set is left in place. On POST /v1/chat/completions (OpenAI shape), the gateway sets a top-level cache_control: {"type": "ephemeral"} on the body. For Anthropic models this becomes the Messages API request-level cache_control, which places a breakpoint after the last cacheable block, so tools, system prompt and the conversation so far are reused turn over turn. Providers that cache automatically (OpenAI, Azure OpenAI, Gemini) ignore the field. On every other endpoint force leaves the body unchanged.

Cache rules

A cache rule is an organization-wide match plus an action. Rules apply to every virtual key in the organization; narrow a rule with its matchers. Manage rules under AI Gateway, Cache Rules in the sidebar (/gateway/cache-rules).
The Cache Rules list with two rules, their match, action and enabled state

The Cache Rules list. The rule with the lower priority number is checked first.

Matchers

All matchers you set must match for the rule to fire. A matcher you leave empty matches everything.
The Match when block of the New cache rule drawer with the virtual key id, display prefix, tags, principal, model and request metadata fields

The Match when block of the New cache rule drawer. Every field you fill must match. With only the model pattern filled, the rule matches every key.

The model matcher is compared with the model string as the caller sent it, before any alias or provider prefix is resolved. A rule on claude-* matches claude-haiku-4-5 and does not match anthropic/claude-haiku-4-5. Write the pattern for the names your callers send, or add a second pattern with the prefix. Leave the principal field empty. The gateway does not pass a principal to the rule evaluation, so a rule that sets one never fires. The request metadata key and value are not evaluated: a rule that sets them matches as if they were empty.

Action

The Then do block sets one of the three actions.
The Then do block of the New cache rule drawer with Cache control mode set to force

The Then do block with the action set to force.

Priority

Priority is an integer from 0 to 1000, default 100. The gateway evaluates rules in ascending priority number: the rule with the lowest number is checked first, and the first rule that matches wins.

Propagation

Creating, editing or archiving a rule writes a change event in the same transaction. The gateway picks the event up on its next change-feed poll and rebuilds the organization’s key bundles, so the next request runs against the new rules. No restart is needed. Archiving a rule stops it from matching. The audit log keeps the rule’s history.

Verify a rule from the response

Send a request through a key the rule matches and read the response headers:
The response header is the only per-request signal of which action fired. The span carries no cache rule attribute; the provider-reported cache token counts are on it as gen_ai.usage.cache_read.input_tokens and gen_ai.usage.cache_creation.input_tokens. On the response side, Anthropic reports cache_read_input_tokens and cache_creation_input_tokens in usage. The gateway forwards these counts unchanged and records them on the span and in the spend record, so cached and uncached input tokens are priced separately.

Rollout recipes

Force caching for one team

  1. Tag the team’s virtual keys, for example team=support.
  2. Create a rule: priority 50, virtual key tags team=support, model claude-*, action force.
  3. Send one request through a tagged key and check X-LangWatch-Cache-Mode: force.
  4. Compare cache_read_input_tokens on the second identical request against the first.

Disable caching for evaluation traffic

Evaluation runs should not reuse a cached prefix, so that every run pays the same cost and sees the same behavior.
  1. Give the evaluation keys a tag, for example suite=evals.
  2. Create a rule: priority 10, virtual key tags suite=evals, action disable.
  3. Verify with X-LangWatch-Cache-Mode: disable on a request through one of those keys. Every cache_control block the client set is gone from the body the provider receives.

Roll back

Archive the rule from the row menu on the Cache Rules page. The next change-feed poll removes it from every key bundle. The rule’s create, edit and archive events stay in the audit log.

Boundaries

  • A cache mode set on a virtual key’s own configuration is not applied. The gateway reads cache rules and the provider default only.
  • Rules change the request body only. The gateway has no cache of its own; the provider decides whether a cached prefix is served.
  • OpenAI, Azure OpenAI and Gemini cache automatically on the provider side. force and disable do not change their behavior; X-LangWatch-Cache-Mode still reports the action that fired.
  • The provider default (auto) never overrides a client that manages its own breakpoints. On /v1/messages any cache_control in the body disables it; on /v1/chat/completions only a top-level cache_control does, because message-level markers do not survive the translation to the Anthropic shape.
Also check: Virtual Keys for tags and the display prefix, Observability for the span attributes, Audit for the rule change events.
Last modified on September 6, 2026