# Exact cache for embeddings and moderation

**Last updated:** 2026-11-21T15:06:38-03:00 (UTC-3)

Indexing the same documents again, or moderating the same text twice, doesn't have to cost another call. The exact cache returns the response already obtained for an **embeddings** or **moderations** request that is identical, byte for byte, and made with the same key. Flag: `exact_cache`.

The cache is used only when all three agree: the flag is on, the alias has the policy on and the request carries `X-Gateway-Cache: allow`. For chat responses, use the [chat answer cache](/docs/features/semantic-cache/), which is exact without an embeddings alias; `exact_chat_cache` adds tenant-prefixed, normalized keys, per-request lifetimes and zero-cost hits to it ([exact chat cache rules](/docs/features/semantic-cache/#exact-chat-cache-rules)).

## How to turn it on

```yaml
features:
  embeddings: true
  exact_cache: true
models:
  - id: embedding-default
    capabilities: [embeddings]
    cache:
      enabled: true
      ttl: 30s               # 1 s to 5 min
    targets:
      - {provider: compatible-provider, model: upstream-embedding-id}
```

| Key | Default | What it does |
| --- | --- | --- |
| `models[].cache.enabled` | `false` | Turns the exact cache on for this alias; needs the `exact_cache` flag |
| `models[].cache.ttl` | required in a `cache` block | How long an entry is served, 1 s to 5 min |

- The alias must have a single target and the `embeddings` or `moderations` capability.
- The policy can also be edited in the console's model editor and through `PUT /api/v1/admin/models/{id}`, with versioning, audit and revert to the YAML.
- With [plans](/docs/features/entitlements/), the team needs the `exact_cache` entitlement.

## How to use it

```bash
curl -i http://localhost:8080/v1/embeddings \
  -H "Authorization: Bearer $GATEWAY_API_KEY" \
  -H 'Content-Type: application/json' \
  -H 'X-Gateway-Cache: allow' \
  -d '{"model": "embedding-default", "input": "test text"}'
```

- The response carries `X-Gateway-Cache: hit` or `miss`. Without that header, the request did not go through the cache.
- The request must be **identical**: different whitespace, property order or parameters count as a different request.
- **Moderation with images:** only inputs that carry their whole content are cacheable: text, and images as data URLs. A request with an image `https` URL always reaches the provider and carries no `X-Gateway-Cache` header, because the image behind a URL can change while the request stays byte for byte the same. Part types other than `text` and `image_url` are not cached either. See [Moderation](/docs/features/moderations/).
- Cached responses come with `Cache-Control: private, no-store`, so no proxy or browser stores them.

## Isolation and security

- **Everything is checked before the cache:** key, scopes, team policy, RPM, models and capability. A hit still counts toward RPM, and revoking the key denies access even when the response is stored.
- **Isolated per key:** the cache identity includes the key, the team, the credential presented, the alias, the configuration and the provider credential. Nothing is shared between keys or teams.
- **A configuration change** or a change to the provider credential invalidates the entries.
- The cache does not deduplicate concurrent calls.

## What is stored

Only `200` responses, as complete and valid JSON, without `error`, with `data` (embeddings) or `results` (moderations), up to the configured response limit (default 64 KiB).

Never stored: errors, truncated responses, streams, responses with `Vary: *`, a `Vary` the key does not cover (below), `Cache-Control` or `Pragma`, an authentication challenge (`WWW-Authenticate` and similar), `Authorization`, `Cookie`, `Set-Cookie2`, `Content-Encoding`, `Content-Range` or trailers, and requests with `Cache-Control`, `Pragma`, `Cookie` or `Range`. There is no revalidation and no stale response.

A `Set-Cookie` on the response does not prevent storing it. Providers set cookies for their own edge (OpenAI sets one on every response), and the gateway never sends a provider a client's cookie or one the provider set: it keeps no cookie jar, a client's `Cookie` is never forwarded, and `Cookie` can be neither a forwarded client header nor a fixed connection header. The only `Cookie` a provider can receive is a connection whose own credential is configured that way (`auth.type: header` with `header: Cookie`), and that value is the connection's credential, already part of the cache identity. So the answer cannot depend on a per-client or provider-set cookie. The provider's cookie is never passed on to the client, on a miss or a hit, and is not stored: only the body and its `Content-Type` are.

A `Vary` on the response does not prevent storing it when the key already covers every header it names. OpenAI answers every embeddings call with `Vary: Origin`; before this rule nothing from OpenAI was ever stored, and every lookup was a `miss`. A provider's answer can depend only on the request the gateway sent it, and the only client values that request carries are the body, the query and the [forwarded client headers](/docs/features/providers/#forwarded-client-headers), all part of the key (in memory and in the shared backend alike); everything else is the connection's configuration and credential, part of the cache identity. So a `Vary` naming a header the gateway did not send (`Origin`, which it never sends unless an operator forwards it) or a forwarded client header is stored. Still refused: `Vary: *`, and a `Vary` naming a header the gateway itself set on that call (such as `Content-Type`), whose value the key does not hold.

The content stays in process memory (or in the [shared backend](/docs/operate/cache-backends/)). It does not go to PostgreSQL, logs, telemetry or metrics. The request and trace IDs are always those of the current request.

**Cost:** a hit records no new tokens, and [valuation](/docs/features/usage-valuation/) marks the event as `unknown/cache_hit`, without making up savings.

## Capacity (in-memory backend)

| Resource | Default or policy |
| --- | --- |
| Entries | Default 256, configurable 1–256, with LRU eviction |
| Size per entry | Default 64 KiB, configurable 256–65536 bytes |
| Accounted memory | Default 32 MiB, configurable 2048–33554432 bytes |
| TTL | Required 1 s to 5 min per alias; no backend default |
| Scope | One process; each replica has its own cache |

Restarting the process empties the memory cache. Integer zero selects the existing capacity default. The model editor’s `30s` creation preset does not supply a missing YAML/API TTL. Capacity and TTL constraints are shared by the existing backend and console consumers; see [parameter definitions](/docs/reference/parameter-registry/). To share the cache across replicas, select an optional compatible remote backend as described in [cache backends](/docs/operate/cache-backends/).

## Monitoring and clearing

**Providers → Exact response cache** shows limits, occupancy, counters and the alias policies, and clears the cache after confirmation.

| Endpoint | Permission |
| --- | --- |
| `GET /api/v1/admin/cache` | `providers:read` (admin, operator, viewer) |
| `DELETE /api/v1/admin/cache` | `providers:manage` (admin, operator), with CSRF |

With the in-memory backend behind a load balancer, status and clearing reach the replica that answered (`process_instance_id` tells you which one). The counters are not meant for billing.

With `metrics_endpoint`, `/metrics` adds the fleet-wide view: every lookup by result (`hit`, `miss`, `bypass`) and the reason of a bypass, what became of each miss's answer (`stored`, or `skipped` with the rule that refused it), the shared backend's calls by operation with their latency and errors, and which cache serves (`memory` or `distributed`). The answer's `X-Gateway-Cache` header says only `hit` or `miss`, and is absent on a bypass; the series say why. A cache that answers `miss` every time while `gateway_exact_cache_stores_total{result="skipped"}` grows is refusing what the provider returns, and the `GatewayExactCacheNotStoring` alert fires. Series, reasons and queries: [exact cache series](/docs/features/observability/#exact-cache-series).

## See also

- [Semantic cache](/docs/features/semantic-cache/): chat responses.
- [Cache backends](/docs/operate/cache-backends/).
- Reference: [RFC 9111](https://www.rfc-editor.org/rfc/rfc9111.html). This cache is explicit application memoization, not a transparent HTTP cache.

## Conditional target eligibility

[Conditional routing](/docs/features/conditional-routing/) runs before cache lookup. Exact identity privately includes the eligible target set; a warm entry cannot serve an ineligible caller/tag context. This preserves the existing single-raw-target embeddings/moderations profile and does not enable multi-target Chat caching.
