Introduction
Large Language Models (LLMs) such as GPT-4 have transformed the technological landscape, yet their operational costs remain a significant barrier. A single query to a high-end LLM can be orders of magnitude more expensive than a traditional search engine query. Caching—storing previously generated responses for reuse—presents a natural solution to mitigate these costs.
However, traditional caching mechanisms typically rely on exact-match lookups (e.g., string or token identity). This approach is inefficient for natural language: two queries like "What is LLM caching?" and "How does caching work in large language models?" are semantically identical but would be treated as distinct by an exact-match cache, leading to redundant and costly LLM invocations.
While "semantic caching" has emerged as a concept, existing implementations often rely on ad-hoc heuristics. These methods frequently struggle with two core challenges:
- The Mismatch Cost: Providing a "similar" cached answer instead of a fresh one may result in a loss of utility. Balancing this loss against the cost of a fresh LLM response is non-trivial.
- Uncertainty: In production environments, the query arrival distribution and the specific serving costs (e.g., latency or token usage) are often unknown a priori.
System Architecture
The authors propose a principled framework that treats semantic caching as an optimization problem under uncertainty. The system utilizes embeddings to evaluate the similarity between incoming queries and cached entries.
As shown in the architecture above, the system processes a query $q_t$ by generating an embedding and comparing it against the cache $M_t$. A decision is made based on a cost-benefit analysis: if a sufficiently similar response exists in the cache such that the mismatch cost is lower than the estimated serving cost, the cached response is served. Otherwise, the LLM is invoked, and the cache may be updated according to an eviction policy.
Methodology
The framework defines a mismatch cost based on the distance between query embeddings (e.g., Euclidean distance in a vector space). The problem is formulated as a combinatorial multi-armed bandit.
Core Algorithms
- Oracle Setting (
Reverse_Greedy): In scenarios where all parameters are known, the problem is NP-hard. The authors demonstrate that the loss function is supermodular, exhibiting diminishing returns. They propose a "Reverse Greedy" approach that starts with a full cache and iteratively removes the least beneficial items. - Offline Learning (
CUCB-SC): When historical logs are available, the algorithm employs a pessimistic approach using Lower Confidence Bounds (LCB) to estimate costs, ensuring robustness against infrequent data points. - Online Adaptive Learning (
CLCB-SC-LS): For real-world deployment, this algorithm learns from live interactions using the "Optimism in the Face of Uncertainty" principle. Critically, it incorporates a low-switching constraint: because updating a vector database is expensive, the cache is only updated in stages once sufficient new information is gathered.
Experimental Setup
The study validates these algorithms using a synthetic dataset to ensure theoretical consistency:
- Queries: 20 distinct query types generated by ChatGPT (e.g., "Rome attractions," "AI papers").
- Embeddings: 384-dimensional vectors generated via a pre-trained sentence transformer.
- Costs: Derived from token counts of ChatGPT responses, with Gaussian noise added to simulate real-world latency fluctuations.
Key Insights
- Supermodularity: Proving supermodularity allows the use of efficient greedy algorithms with provable approximation guarantees.
- Exploration-Exploitation: The system must balance serving a "good enough" cached answer (saving cost) versus querying the LLM to better estimate true serving costs (learning).
- Practicality of Low-Switching: By explicitly modeling the cost of cache updates, the framework is better suited for production environments where constant database churn is undesirable.
Results
The proposed CLCB-SC-LS algorithm demonstrated significant improvements over standard baselines like Least Frequently Used (LFU) or Epsilon-Greedy:
- Cost Reduction: Average regret (total cost loss) was reduced by 11.75% to 54.04%.
- Operational Efficiency: The low-switching design reduced cache updates by up to 90.91% and slashed running time by up to 85.40% compared to standard adaptive baselines.
- Theoretical Soundness: The suboptimality gap scales with $O(1/\sqrt{n})$, matching state-of-the-art performance for simpler exact-match caching problems.
Conclusion and Future Work
This research provides a rigorous foundation for semantic caching in LLM serving. While the current experiments focused on a controlled set of 20 queries, future work will address the challenges of "open-set" query distributions in infinite natural language spaces. Additionally, hybrid approaches combining offline pre-training with online adaptation offer a promising path toward highly responsive and cost-effective LLM infrastructure.
Summary updated