Caching
When the same question comes in twice, why pay to answer it twice? Caching remembers a model’s reply and serves it again instantly: no provider call, no token cost, and a response in milliseconds instead of seconds. VectorAxis offers two flavours: exact-match for identical requests, and semantic for ones that simply mean the same thing.
- Lower cost. A cache hit doesn’t call the provider, so it costs nothing in tokens.
- Faster responses. Hits return immediately instead of waiting on the model.
- Steadier load. Repeated traffic (FAQs, popular prompts, retries) is absorbed before it ever reaches a provider’s rate limit.
| Mode | Reuses an answer when… | Best for |
|---|---|---|
| simple Exact | a new request is byte-for-byte identical to one seen before. | repeated identical calls, idempotent retries, deterministic prompts. |
| semantic Meaning | a new request means the same thing as one seen before, even if worded differently. | FAQ-style traffic and chat where users ask the same thing many ways. |
| Header | What it does | Accepted values | Default |
|---|---|---|---|
| x-cache-mode | Enables caching and chooses the flavour. Without it, caching is off. | simple / semantic | off (DISABLED) |
| x-cache-ttl | How long a stored answer stays fresh before it’s recomputed (its “time to live”). | a number of seconds | platform default |
| x-cache-force-refresh | Skip any stored answer for this call, fetch a fresh one, and overwrite the stored entry. Use after content changes. | true | false |
Every response, streamed or not, carries an x-cache-status header so you always know whether the answer came from cache or the provider:
| Status | Meaning |
|---|---|
| HIT | Served from the exact-match cache. No provider call. |
| SEMANTIC_HIT | Served from the semantic cache (a meaning-match). No provider call. |
| MISS | Caching was on, but nothing matched; the provider answered and the reply was stored for next time. |
| REFRESH | Force-refresh: the provider was called and the stored entry overwritten. |
| DISABLED | No cache mode was sent, so caching didn’t run. |
| BYPASS | Caching was skipped for this request (a multimodal request on the semantic cache, a plan without semantic caching, or a routing config). |
A hit calls no provider, so it costs nothing: its log entry shows $0 cost, and what the call would have cost (the same tokens at the model’s rate, with your markup on a platform key) appears separately as Saved $. Nothing is charged to the virtual key. Totals stay honest: the Analytics Total cost is what you actually spent, and Saved by cache sits next to it, with a Saved chart and a column in the breakdown. Through the API: cost_saved_usd on each log entry and total_cost_saved_usd on the analytics summary and breakdown.
Under the hood
For the engineers wiring this up: exactly how each mode decides a match.
VectorAxis builds a fingerprint (a SHA-256 hash) over the parts of the request that change the answer: provider, model, system + messages, temperature, top_p, max_tokens, and stop. Two requests with the same fingerprint share an answer; change any of those and it’s a new entry. The store is an in-process cache with a per-entry TTL, so hits are returned with effectively zero added latency.
The request is turned into an embedding (a numeric “meaning vector”) and compared against previously stored requests by similarity (cosine distance, via the pgvector extension with an HNSW index for fast nearest-neighbour search). If the closest match scores above the similarity threshold (high by default, around 0.90), its cached answer is returned. A pre-filter on provider and model ensures a match only counts within the same model, so a question answered by one model is never served from another.
On a miss, the embedding and the fresh answer are written after the response is sent (fire-and-forget), so storing never adds to the caller’s latency.
Freshness & eviction. A stored answer is served only while it’s still within its TTL (the x-cache-ttl you set, or the platform default of about an hour). The window is sliding: every time an entry is served, its clock resets, so answers that keep getting used stay warm while ones that fall out of use quietly go stale. Separately, to keep storage bounded, any entry that hasn’t been served for 7 days is physically deleted from the cache. So the TTL governs whether a given request gets a cached reply; the 7-day idle window governs how long the stored row itself lives before it’s reclaimed.
Caching & streaming
Streamed requests (stream: true) use the cache with the same headers. On a hit the stored answer is sent as a stream straight away: the text in one chunk, then any tool calls, the finish reason and the token usage, ending with [DONE] (or, on the Claude SDK’s Messages API, as Anthropic’s events). Output guardrails run on it, it costs nothing, and the log shows what it saved. On a miss the answer streams from the provider as usual and, once the stream has finished, the whole answer is stored. A stream that fails, is blocked or that the client stops reading is not stored.
Caching embeddings
Exact-match caching also works on the Embeddings API: POST /v1/embeddings and Cohere’s POST /v2/embed, with the same headers, TTLs and statuses as chat. Re-embedding an unchanged document (a re-index, a retried pipeline step) is then served from the cache instead of the provider.
- A hit needs the same body: model, texts, dimensions, input type and every other field (the order of fields does not matter). The same text in OpenAI’s format and in Cohere’s own format are cached separately, since the answers differ in shape.
- Guardrails run first, so the cache holds the vectors of what was actually embedded (after any redaction).
- semantic is not used for embeddings: a similar text must never get another text’s vectors. A request that asks for it is answered by the provider with x-cache-status: BYPASS.
- Very large answers (over about 1 MB, such as a big batch of high-dimension vectors) are returned but not stored.
- A hit answers x-operation-cost-usd: 0.0 and x-cache-saved-usd, what the call would have cost (see what you saved).
Provider prompt caching
Separate from the gateway’s response cache, model providers cache the prompt: a long, repeated prefix (a system prompt, a document, tool definitions) is stored on the provider’s side, and later requests that start with it are billed at a much lower rate for that part. The model still answers every request, so this saves cost and latency on the input, not the call. The two work together.
| Provider | How it is turned on |
|---|---|
| Anthropic (and Claude on Vertex AI) | A cache_control breakpoint on the block the cached prefix should end at: a system block, a message part, an image, a document, a tool result or a tool, or once at the top level (automatic caching). {"type": "ephemeral"} keeps it 5 minutes, "ttl": "1h" an hour. On the Claude SDK as Anthropic documents it; on the OpenAI SDK on the content part or tool. |
| OpenAI | Automatic for long prompts; nothing to send. On models from GPT-5.6, a cache_control breakpoint becomes OpenAI’s explicit cache breakpoint when the conversation ends on a user message (that message gets one too, as OpenAI’s automatic caching would have placed it); otherwise, and on older models and other OpenAI-format providers, it is removed, which OpenAI would reject, and the automatic caching applies. |
| OpenRouter | cache_control is passed on, for the Claude models it serves. |
Usage and cost. On /v1/chat/completions, prompt_tokens includes the cached tokens and prompt_tokens_details.cached_tokens is the part read from the cache, as OpenAI reports it; Claude’s cache writes are cache_creation_input_tokens. On the Claude SDK the usage keeps Anthropic’s own split. Cache reads, writes and 1-hour writes are charged at the model’s own cache rates (on Claude, a read costs about a tenth of fresh input), and that is the cost Logs, Analytics and a key’s budget see.