Cache

On this page

An intelligent, multi-tenant, cost-aware semantic caching module for LLM applications.

Cache (module in zerda package: cache) sits in front of an LLM call and answers the question "have we already paid to generate an answer like this one?" It combines four independent caching techniques into a single pipeline:

  • Exact-match caching — an in-process LRU (L1) plus a durable backend (SQLite, Redis, or in-memory) for identical, normalized queries.
  • Semantic caching — a vector similarity index (FAISS or a NumPy fallback) that matches queries which are worded differently but mean the same thing.
  • Reinforcement-learning-based ranking and eviction — a Thompson Sampling multi-armed bandit that learns, from explicit feedback, which cached answers are actually good, and uses that signal both to rank semantic candidates and to decide what to evict.
  • Cost-aware routing — a DecisionEngine that only serves a cache hit when doing so is a better trade-off (cost, quality, latency) than calling the LLM again.

Alongside this, the module ships a second, independent, simpler component — MultiLevelCache — a generic three-tier (memory / memory / disk) cache with pluggable eviction strategies (LRU, LFU, FIFO, TTL, Adaptive). It has no knowledge of LLMs, embeddings, or tenants, and can be used on its own as a general-purpose cache.

Two modules in one idea

CacheManager pipelineMultiLevelCache
PurposeSemantic LLM-response cachingGeneric object caching
Entry pointCacheManagerMultiLevelCache
Key matchingExact + semantic (embeddings)Exact only (SHA-256 of key)
StoragePluggable: Memory / SQLite / RedisBuilt-in: RAM / RAM / Disk (pickle)
Multi-tenantYes (TenantManager)No
Learns over timeYes (Thompson Sampling, see CachePolicyLearner)No
Async supportNative (async def get/put/feedback)Wrapped (aget/aset/adelete via asyncio.to_thread)
Typical use caseCaching answers from an LLM/RAG pipelineCaching any Python object behind a hot code path

Both are exported from the top-level cache package and can be used independently or side by side.

Where to go next

Module map

ModuleContents
cache.cache_managerCacheManager — the semantic-cache pipeline entry point
cache.multi_level_cacheMultiLevelCache — generic L1/L2/L3 cache
cache.modelsIntelligentCacheEntry, BanditArm, CostRecord, CacheLookupResult, TenantQuotaState
cache.cache_entryCacheEntry — the simple entry type used by MultiLevelCache
cache.cache_strategiesCacheStrategy enum
cache.config_cacheCacheConfig and all sub-configuration dataclasses/enums
cache.normalizationQueryNormalizer, NormalizationConfig
cache.embedding_indexEmbeddingIndex, CachedEmbedder, embedding providers
cache.cost_modelCostModel, DecisionEngine
cache.policy_learnerCachePolicyLearner, FeedbackSignal
cache.tenant_managerTenantManager, TenantRegistration, QuotaExceeded
cache.securitySecurityGuard, SecurityViolation
cache.metricsCacheMetricsCollector, LatencyHistogram, RollingCounter
cache.cache_metricsCacheMetrics — simple L1/L2/L3 hit-rate counters used by MultiLevelCache
cache.storageBaseStorage, MemoryStorage, SQLiteStorage, RedisStorage, build_storage