The gateway can answer a request from a cache of earlier completions instead of calling the provider again. Use it for deterministic workloads — classification, extraction, FAQ bots — where the same prompt recurs and a fresh generation adds cost and latency but no value.
The cache is switched on for a whole deployment by its operator, then a team or a workspace opts in. If X-ManyLayers-Cache on your responses always says disabled, one of those switches is off — see Eligibility.

How it works

The cache has two layers that are checked in order:
  1. Exact match. A SHA-256 key built from the redacted request. A hit returns the stored completion immediately.
  2. Semantic match (optional). On an exact miss, the prompt is embedded and compared with earlier prompts in the same scope (team, workspace and optional namespace) for the same model. A close enough neighbour is returned. A request that uses tools (tools, tool_choice or tool turns) never reads or writes the semantic layer. A semantic hit carries X-ManyLayers-Cache-Similarity (four decimals).
On a miss the request goes to the provider. When it succeeds with HTTP 200, the completion is stored in the exact layer and, if an embedding was computed during lookup, in the semantic layer. Cache lookup runs after PII redaction and input guardrail mutations, so keys and stored entries never contain raw PII. The stored completion is the outbound-redacted text, so a replay never re-exposes PII even when the original response streamed through unredacted. A cache hit still passes through your input and output guardrails, and still counts against request-per-minute limits and spend policies, which are checked first; token-per-minute limits are not charged for a hit. The completion text, any tool calls, its finish reason and its token counts are stored. A non-streamed replay is a freshly built response with an id starting cache-, a single assistant message, the stored finish_reason (stop, or tool_calls when the entry has tool calls) and the stored tool_calls; a streamed request is replayed as a stream. A reply is not stored if a tool call lacks an id or name, has invalid-JSON arguments, or comes from Gemini or Vertex.

Eligibility

A request uses the cache only when all of these hold:
  • cache.enabled: true for the deployment.
  • The cache is switched on for the request: the calling team has cache_enabled: true, or the request’s workspace has the cache switched on (Settings → Cache, or PUT /admin/gateway/cache-config with {"workspace_cache_enabled": true}, which needs gateway.workspaces.manage), or a caller holding gateway.workspaces.manage opts that request in with X-ManyLayers-Cache-Type. A virtual account has no team, so only the workspace switch or the opt-in applies to it. The semantic layer can also be switched on by the workspace’s own semantic-cache settings (PUT /admin/gateway/semantic-cache) without the exact-match switch.
  • The endpoint is /v1/chat/completions, /v1/completions, /v1/responses or /v1/messages. Embeddings are never cached.
  • The request sets temperature to exactly 0, or the model is configured with cache: true.
A request that fails any condition is answered normally with X-ManyLayers-Cache: disabled, and X-ManyLayers-Cache-Reason says why: no_backend (cache off for the deployment), not_enabled (no team, workspace or opt-in switch), embeddings or not_deterministic (no temperature: 0 and no model cache: true).
The model-level cache: true flag is a gateway.yaml model setting. Models registered through Gateway → Providers do not carry it, so for them only requests with "temperature": 0 are cached.
Set a team’s opt-in with cache_enabled on POST /admin/teams (organization Owner; see Keys & Budgets), or switch the cache on for a whole workspace as described above.

Exact key composition

The exact key hashes:
  • the entry’s scope: team, workspace and optional namespace;
  • the resolved logical model name;
  • the text of every message (after redaction), then prompt, then input;
  • these parameters, when present: temperature, top_p, max_tokens, n, stop, tools, tool_choice, parallel_tool_calls, response_format;
  • for a request with tool turns, each tool or call message’s position, role, tool_call_id and call text.
stream is not part of the key, so a streamed and a non-streamed request share one entry; a streamed request is replayed as a stream.
Entries are scoped to the team, the workspace and an optional namespace (X-ManyLayers-Cache-Namespace), in both layers, so teams and workspaces never see each other’s entries. Within one scope every caller shares entries: the same redacted text with the same parameters to the same model name returns the same cached completion. Use a namespace to keep callers inside a team apart.

Configuration

Deployment-wide settings, from gateway.yaml or the administration console under Settings → Cache. Each workspace can also switch the exact cache on (GET/PUT /admin/gateway/cache-config, {"workspace_cache_enabled": bool}) and configure its own semantic cache (GET/PUT /admin/gateway/semantic-cache, {enabled, embedding_model, threshold}; errors unknown_embedding_model and invalid_threshold). The deployment’s cache.enabled must still be on.
gateway.yaml (fragment)

Key fields

boolean
default:"false"
Turns the cache on for the deployment. Teams still opt in individually.
duration
default:"5m"
How long an entry lives, in both layers.
integer
default:"1024"
Capacity of the in-process LRU. Not used when Redis is configured.
string
A model the gateway calls on itself, as the calling identity, to embed prompts. It must be in the requesting team’s allowed models; if the embedding call fails for any reason, the request is treated as a plain miss.
number
default:"0.92"
Minimum cosine similarity for a semantic hit. Raise it to reduce false matches.
string
default:"memory"
memory keeps a per-replica LRU. qdrant shares entries across replicas in the ml_semantic_cache collection and requires rag.qdrant.url.

Backends

Redis errors are treated as cache misses, never as request failures. Backend changes take effect after a restart.

Per-request control

string
no-cache skips the lookup; no-store skips writing the answer. Combine them as no-cache, no-store. Any other value is a 400. The header can only make the gateway cache less — it never makes an ineligible request cacheable — and it is removed before the request is forwarded.
string
exact or semantic: use only that layer. semantic answers 400 semantic_cache_unavailable if the semantic layer is not on. From a caller holding gateway.workspaces.manage (or an operator) it also opts the request in when no team or workspace switch is on.
integer
Lifetime in whole seconds for the entry this request writes, 1 to 604800.
string
1–128 characters of letters, digits and . _ : -. Partitions the team’s cache: entries written under one namespace are never served to another.
number
Minimum similarity for a semantic hit on this request, above 0 and at most 1.
A bad value in any of these is a 400 invalid_header. All are removed before the request is forwarded.
string
On any hit: the trace id of the request that produced the entry.
string
On a semantic hit: the cosine similarity, four decimals.
string
Kept even when a routing config sets strict_openai_compliance, which otherwise strips the other X-ManyLayers-* response headers.

Monitoring

GET /admin/cache/stats aggregates lookups since ?since= (RFC 3339, default the last 24 hours). It is a deployment-operator endpoint: it needs an admin-role credential and is not available to workspace roles, so on the hosted service ask ManyLayers or use the Prometheus metric below.
latency_saved_ms sums the original generation time of each entry that was served. Bypassed and ineligible requests are not counted.

Next steps

PII Detection & Redaction

Redaction runs before the cache key is built.

Metrics

Chart hit rate alongside latency and spend.

Self-Host the Gateway

Add Redis to share the exact cache across replicas.

Routing

What happens on a cache miss.