How We Cut OpenAI and Anthropic API Bills by 58% Using Redis Vector Semantic Caching and Exact-Match Fingerprinting
Shafikul Islam
Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

Why Calling Upstream LLMs on Repeat Questions Kills Margins
When we inspected 30 days of production AI assistant logs across our B2B workflows, we noticed a stark pattern: 38% of prompts were either exact duplicates (triggered by dashboard refreshes and workflow templates) or near-identical questions phrased slightly differently.
Calling a frontier LLM for every repeat prompt burned $0.015+ per request and forced users to wait 2.8 seconds for tokens that had already been generated ten minutes earlier.
1. Tier 1: Sub-Millisecond Exact-Match SHA-256 Fingerprinting
Before computing embeddings, we normalize whitespace, sort system parameters, and hash tenant_id + model + system_prompt_version + user_prompt with SHA-256:
import hashlib
import json
def build_prompt_fingerprint(tenant_id: str, model: str, messages: list[dict]) -> str:
canonical = json.dumps(
{"t": tenant_id, "m": model, "msgs": messages},
sort_keys=True,
separators=(",", ":"),
)
digest = hashlib.sha256(canonical.encode("utf-8")).hexdigest()
return f"llm:exact:{tenant_id}:{digest}"
Tier 1 alone absorbed 21% of total LLM traffic in 0.8ms directly from Redis RAM.
2. Tier 2: Tenant-Scoped Semantic Vector Cache (Cosine >= 0.96)
If Tier 1 misses, we compute a lightweight embedding in 8ms and query a tenant-partitioned vector index.
The most common mistake with semantic caching is setting the similarity threshold too loose (such as 0.85), which returns wrong answers when a single negation word ("not", "without") changes the meaning. We enforce three strict guards before serving a semantic cache hit:
- Strict Cosine Threshold (>= 0.96): Only near-synonymous queries match.
- Numeric & Negation Token Check: If numbers, dates, or negation words differ between the incoming query and the cached query, bypass the semantic cache.
- Tenant & RBAC Scope Match: A cached answer from Tenant A is never visible to Tenant B.
Combined, Tier 1 and Tier 2 cut our monthly LLM API bill by 58% and reduced overall P95 response latency from 2.8s to 420ms (and 19ms on cache hits).