ADR-0054: Intent Classification Cache
Date: 2026-05-12 | Status: Accepted | Relates to: ADR-0031 Semantic Query Cache, ADR-0030 LLM Entity Extraction
Context
Per-stage telemetry showed intent classification dominating chat-pipeline latency at p50 ≈ 2,300 ms × every query — a single OpenAI gpt-4.1 call that runs on every turn before retrieval. The Semantic Query Cache already eliminates the full pipeline cost for exact-string answer repeats, but on a Tier-1 miss every step (including intent classification) is rerun fresh.
Production traffic analysis (2026-05-11) showed a meaningful fraction of queries are repeat phrasings of common questions. The intent classifier returns deterministic output at a low temperature: identical input produces identical output. There is no reason to pay 2,300 ms for the same answer twice.
Decision
A separate cache layer keyed on (tenant, normalized query, language) storing the full intent-classification result. The cache is deliberately decoupled from the semantic query cache because their failure modes differ:
| Cache | What it stores | Wrong cache hit looks like |
|---|---|---|
| Semantic query cache | Full LLM answer | Wrong content in the response |
| Intent classification cache | Routing decision (intent class + strategy) | Wrong route — retrieval gets bad top-K, prompt uses wrong template |
Two backends share one asynchronous interface:
Memory backend (default)
A per-worker bounded LRU cache with a time-to-live (TTL). Process restart clears the cache — a safety property that bounds poisoning to one container lifecycle. Used on the single-worker pilot.
Redis backend (opt-in via configuration)
Shared across worker replicas via the existing Redis connection pool. Values are stored as validated JSON. Keys carry a dedicated prefix so a clear operation targets only this cache without disturbing rate-limiter or token-blacklist keys.
The Redis backend persists across container restarts — meaning the "restart fixes poisoning" remedy that works for the memory backend no longer applies. The compensating control delivered in the same change is an operator "Clear Cache" button on the platform settings page, which wipes both the intent cache AND the semantic query cache in a single click through an authenticated administrative function.
Poisoning Guard
The intent classification service writes to the cache only when:
- the classification confidence is at or above a configurable threshold (default 0.85)
- the classified intent is not "unknown"
These guards live in the caller, so they apply regardless of backend choice.
Resilience
Every Redis operation is wrapped in error handling and falls back to a cache miss on failure. A Redis outage degrades the system to "every query pays the 2,300 ms LLM cost" — not "the system crashes." If the Redis connection pool is uninitialised when the cache is first requested with the Redis backend selected, the cache falls back to the memory backend with a startup-time warning.
Configuration
Five settings are configurable without a code change: a master on/off switch (default on), the backend (memory by default, or Redis), the maximum size of the memory backend (default 1,000 entries), the time-to-live for both backends (default one hour) and the confidence threshold used as the poisoning guard (default 0.85).
Consequences
Positive
- Cache hit removes ~2,300 ms from the per-turn latency budget. Stacks with the semantic query cache — if both hit, the full pipeline collapses to ~50 ms.
- Backend selection is a runtime knob, not a code change. Single-worker pilot uses memory; multi-worker production flips to Redis without rebuilding the image.
- Poisoning has a one-click remedy via the existing UI button — operators don't need shell access to recover from a cache-poisoning event.
Negative
- Two caches now share the same poisoning failure mode. In Redis mode, container restart no longer self-heals poisoning. This is the entire reason the UI kill switch was bundled in the same PR as the Redis backend, rather than deferred to a follow-up.
- Marginal Redis pool pressure under heavy traffic — one additional GET per request that isn't already cached at the semantic layer. Pool defaults are sized for rate-limiting + token-blacklist + ingestion; the Redis connection-pool size may need adjustment under future load profiles.
- Cross-worker observability is per-process today. The statistics call returns per-worker counters; aggregate hit rate requires aggregation across workers. Future work would emit metrics to the existing pipeline telemetry stream.
Alternatives Considered
| Alternative | Rejected because |
|---|---|
| Extend the semantic query cache to also cache intent results | Conflates two different failure modes and value types. The semantic cache key is a 1536-dim embedding of the reformulated query; the intent cache key is the raw user input. The semantic cache stores full answers; the intent cache stores classification objects. Reusing the table would have forced shared schema, eviction policy, and TTL semantics. |
| Cache intent results in PostgreSQL | Adds a database round-trip on every classification — opposite of the goal. Redis hits in 1–2 ms locally; PG hits in 5–10 ms. |
| Embed the cache inside the intent classification service | Would couple the cache to the service and prevent the clean Protocol-based backend swap. The current factory-plus-interface design keeps the service agnostic to backend choice. |
Verification
Verified on the pilot on 2026-05-12: a fresh classification wrote an entry to the Redis cache under the dedicated key prefix.
Test coverage (the project's own tests): 12 unit tests on the memory backend, 13 integration tests on the Redis backend against a Redis 7 test container, and 4 integration tests on the cache-clear function.
Related
- ADR-0031: Semantic Query Cache — the other cache layer; both share the same UI kill switch
- System Overview — where this cache fits in the layered architecture