HridaSpro Docs

Features

Prompt Caching

Speed up responses and cut costs with Spro's gateway-level exact-match and semantic response caching.

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 CachingSpro Gateway Caching (Exact & Semantic)
Where it runsInside the model provider (e.g. Anthropic, OpenAI)In Spro, before the request reaches any provider
What is cachedThe prompt prefix — the provider skips re-processing tokens it has already seenThe complete LLM response — a hit returns the response with zero model invocation
How it matchesExact token-prefix match on the promptExact request hash (exact-match), or similarity on the last message (semantic)
Latency savingsReduces time-to-first-token (TTFT); the model still generates a new completionEliminates the LLM call entirely — response is returned from cache in milliseconds
Cost savingsCached input tokens are billed at a reduced rate (varies by provider)A hit costs nothing — no tokens are consumed for that request
Provider supportProvider-specific flags (cache_control, etc.)Works with every provider routed through Spro
How to enableSet prompt-caching fields in the request body — Spro passes them through to the provider untouchedThe 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 semantic also 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.

RangeBehaviourRecommended for
0.95 – 1.0Very strict — only near-identical queries matchHigh-precision use cases where a wrong hit is costly
0.85 – 0.95Balanced — works well for most conversational applicationsGeneral-purpose chatbots and FAQ systems
< 0.85Broad — may match loosely related queriesExploratory 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:

FieldTypeDefaultDescription
type"exact" | "semantic"exactCaching strategy
ttlnumber (seconds)600How long a cached response stays valid (max 86400)
similarity_thresholdnumber (0 – 1.0)0.9Semantic 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:

HeaderDescriptionExample Values
x-spro-cacheWhat the cache did for this requesthit, miss, bypass, error
x-spro-cache-statusLegacy alias of x-spro-cache (same values)hit, miss, bypass, error
x-spro-cached-request-idRequest id of the original request that populated the cache (hits only)d167b694-b414-...
  • hit — served from cache, no upstream call was made
  • miss — cache was active but nothing matched; the response was fetched from the provider and stored
  • bypass — no x-spro-cache-config header was sent, so caching was not in play
  • error — 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

  1. Hash the request — Spro hashes the messages, resolved model, and sampling parameters, scoped to your project.
  2. Look up the cache — if a matching, unexpired entry exists, it is returned immediately and no provider call is made.
  3. 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

  1. 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.
  2. Return or forward — if the similarity score meets your similarity_threshold and 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 stop finish 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