Spro caches LLM responses so repeated or similar requests are served instantly — without calling the model provider at all. Two caching strategies are supported:
- Exact-Match — returns the cached response only when the request is identical
- Semantic — returns the cached response when requests are similar in meaning, even if phrased differently
Caching is opt-in per request: it activates only when you send the x-spro-cache-config header. No header means the cache is fully bypassed — no lookups, no stores.
Prompt Caching vs AI Gateway Caching
Model providers and Spro cache at different layers, and the two are easy to confuse. Understanding the difference helps you pick the right approach — or combine both for maximum savings.
| Prompt Caching | Spro Gateway Caching (Exact & Semantic) | |
|---|---|---|
| Where it runs | Inside the model provider (e.g. Anthropic, OpenAI) | In Spro, before the request reaches any provider |
| What is cached | The prompt prefix — the provider skips re-processing tokens it has already seen | The complete LLM response — a hit returns the response with zero model invocation |
| How it matches | Exact token-prefix match on the prompt | Exact request hash (exact-match), or similarity on the last message (semantic) |
| Latency savings | Reduces time-to-first-token (TTFT); the model still generates a new completion | Eliminates the LLM call entirely — response is returned from cache in milliseconds |
| Cost savings | Cached input tokens are billed at a reduced rate (varies by provider) | A hit costs nothing — no tokens are consumed for that request |
| Provider support | Provider-specific flags (cache_control, etc.) | Works with every provider routed through Spro |
| How to enable | Set prompt-caching fields in the request body — Spro passes them through to the provider untouched | The x-spro-cache-config header (see Configuration) |
The two layers compose: a request can use provider prompt caching and be served from the Spro cache. You can see both in your usage — prompt-cache discounts appear in the token pricing on each usage log row, and gateway cache hits are recorded with cache_status = hit and zero tokens consumed.
Cache Types
Exact-Match Cache
Exact-match caching stores responses keyed by a hash of the request — the messages, the model, and the sampling parameters that affect output. A cached response is returned only when every part of the key matches exactly.
The cache key includes:
- The full message history
- The resolved model
- Sampling parameters:
temperature,top_p,max_tokens,max_completion_tokens,stop,frequency_penalty,presence_penalty,seed,n,response_format
Everything else in the body — stream, user, metadata, request ids — is excluded on purpose, so it does not fragment your cache.
Best for:
- API calls with identical parameters
- Deterministic queries that always need the same response
- Development and testing environments
- Applications with predictable, repetitive queries
Semantic Cache
Semantic caching matches requests that express the same intent even when phrased differently. For example, "How do I reset my password?" and "What's the password reset process?" can return the same cached response.
Spro extracts the last message of the request and compares it against cached last-messages using semantic similarity. All other request parameters — model, prior messages, sampling settings — are hashed separately and must still match exactly. Only the last message is compared fuzzily.
Best for:
- Customer support chatbots
- FAQ systems where users phrase questions differently
- Conversational AI applications
- Any scenario where query variations express the same intent
Semantic cache is a superset of exact-match cache. Setting the type to
semanticalso returns exact-match hits — you don't need to configure both.
Controlling match strictness
The similarity_threshold parameter (0 – 1.0) controls how close two last-messages must be for a semantic hit.
| Range | Behaviour | Recommended for |
|---|---|---|
| 0.95 – 1.0 | Very strict — only near-identical queries match | High-precision use cases where a wrong hit is costly |
| 0.85 – 0.95 | Balanced — works well for most conversational applications | General-purpose chatbots and FAQ systems |
| < 0.85 | Broad — may match loosely related queries | Exploratory or low-risk scenarios |
Start with the default of 0.9 and watch your hit rate. Raise it for precision, lower it for coverage.
Cache Isolation
Every cache entry is scoped to the project that created it. This happens automatically — a cached response stored for one project is never served to another project, even if they send the exact same request. Nothing to configure.
Configuration
Enable caching by adding the x-spro-cache-config header to your requests. The header is a JSON object:
| Field | Type | Default | Description |
|---|---|---|---|
type | "exact" | "semantic" | exact | Caching strategy |
ttl | number (seconds) | 600 | How long a cached response stays valid (max 86400) |
similarity_threshold | number (0 – 1.0) | 0.9 | Semantic cache only — minimum similarity for a hit |
Examples
curl https://spro.hridaai.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer your-spro-api-key" \ -H 'x-spro-cache-config: {"type": "exact", "ttl": 600}' \ -d '{ "model": "gpt-oss-20b", "messages": [ {"role": "user", "content": "What is the capital of France?"} ] }'Using the OpenAI SDK
The cache header is just a request header, so it works with the OpenAI SDK out of the box via extra_headers:
from openai import OpenAI
client = OpenAI( base_url="https://spro.hridaai.com/v1", api_key="your-spro-api-key",)
response = client.chat.completions.create( model="gpt-oss-20b", messages=[{"role": "user", "content": "What is the capital of France?"}], extra_headers={ "x-spro-cache-config": '{"type": "exact", "ttl": 600}', },)
# x-spro-cache tells you what happenedprint(response.headers.get("x-spro-cache")) # "hit" or "miss"Response Headers
Every response carries cache information:
| Header | Description | Example Values |
|---|---|---|
x-spro-cache | What the cache did for this request | hit, miss, bypass, error |
x-spro-cache-status | Legacy alias of x-spro-cache (same values) | hit, miss, bypass, error |
x-spro-cached-request-id | Request id of the original request that populated the cache (hits only) | d167b694-b414-... |
hit— served from cache, no upstream call was mademiss— cache was active but nothing matched; the response was fetched from the provider and storedbypass— nox-spro-cache-configheader was sent, so caching was not in playerror— the cache lookup itself failed; the request proceeded normally against the provider
The same status is recorded on every usage log row (the cache_status column), so you can track hit rates and savings per project, model, or API key from the dashboard.
How It Works
Exact-Match
- Hash the request — Spro hashes the messages, resolved model, and sampling parameters, scoped to your project.
- Look up the cache — if a matching, unexpired entry exists, it is returned immediately and no provider call is made.
- Forward on miss — the request goes to the provider as usual, and the successful response is stored for future hits before being returned to you.
Semantic
- Extract and compare — Spro takes the last message and compares it against cached last-messages using semantic similarity. Everything else (model, prior messages, sampling parameters) is hashed and must match exactly.
- Return or forward — if the similarity score meets your
similarity_thresholdand the parameter hash matches, the cached response is returned. Otherwise the request goes to the provider and both the response and its last-message are stored.
What Gets Cached
To keep the cache trustworthy, Spro only stores responses that are safe to replay:
- Only successful completions with a
stopfinish reason are cached — errors, guardrail blocks, empty responses, and tool-call / length finishes are never stored - Both streamed and non-streamed requests can be cached; a hit is served in whichever format the new request asked for
- Cache lookups and stores are fire-and-forget: the cache can never fail a request, and a cache error degrades to a normal provider call
