ContextCache: Context-Aware Semantic Cache for Multi-Turn Queries in Large Language Models

Jianxin Yan, Wangze Ni, Lei Chen, Xuemin Lin, Peng Cheng, Zhan Qin, Kui Ren · DOI

Introduction

Large Language Models (LLMs) have revolutionized natural language processing but remain computationally intensive and latency-heavy. To optimize performance and reduce costs, developers often employ semantic caching. This technique involves storing previous query-response pairs and serving the cached answer when a semantically similar query is encountered, bypassing the need for a full LLM inference. However, traditional semantic caches, such as GPTCache, suffer from a significant limitation: context-blindness.

In multi-turn dialogues, the meaning of a specific query is often inextricably linked to the preceding conversation. For instance, a query like "What are its features?" is ambiguous in isolation. If the previous turn discussed "Relational Databases," the answer should pertain to SQL systems; if the context was "Cloud Computing," the answer changes entirely. Traditional caches often trigger "false positive" hits by matching the literal query without considering the dialogue history, leading to irrelevant or incorrect responses and a poor user experience.

The ContextCache Architecture

To address these challenges, the authors propose ContextCache, a system designed to integrate conversational context into the semantic retrieval process. The architecture is built upon four primary innovations:

1. Two-Stage Dynamic Retrieval

ContextCache employs a tiered approach to balance speed and accuracy:

  • Coarse-grained stage: The system first performs a high-speed vector search using the current query to identify a set of potential candidates from the cache. This stage efficiently filters out obvious mismatches.
  • Fine-grained stage: For the top-ranked candidates, the system applies a hierarchical self-attention mechanism. This mechanism processes the embeddings of the current query alongside the embeddings of historical dialogue turns. By analyzing these inter-turn relationships, ContextCache generates a "unified contextual representation" that captures the specific intent within the current dialogue state.

2. Hierarchical Self-Attention and Embedding Reuse

A common pitfall in context-aware systems is "attention dilution," where concatenating long dialogue histories causes the model to lose focus on the most recent, relevant turn. ContextCache’s hierarchical approach preserves turn-specific features while modeling their dependencies. To maintain efficiency, the system implements embedding reuse: it only generates a new embedding for the latest query, retrieving pre-calculated embeddings for previous turns from the cache.

3. LLM-Enhanced Training

The model's ability to distinguish between linguistically similar but contextually distinct queries is bolstered by "difficult negative sample mining." During training, the authors used LLMs to generate samples where queries are nearly identical but appear in different contexts, forcing the model to learn the nuances of contextual shifts.

4. Dual-Storage Architecture

The system utilizes a hybrid storage strategy. A relational database manages responses and metadata, while a specialized vector database (FAISS) indexes the query and global contextual embeddings. This separation ensures that retrieval remains performant even as the cache size scales.

Experimental Evaluation

The researchers evaluated ContextCache using a combination of real-world and synthetic datasets, including ShareGPT (1,000 human-AI conversations), GPT-4 Paraphrasing (to test linguistic robustness), and Synthetic Context Shifts (to specifically target context-blindness scenarios).

Key Results

ContextCache demonstrated superior performance across all critical metrics compared to traditional methods like GPTCache:

  • Improved Accuracy: The system achieved a 10.9% improvement in precision and a 14.8% improvement in recall.
  • Massive Speedup: Responses served from the cache were approximately 10 times faster than direct LLM calls (milliseconds versus seconds).
  • Lower Overhead: Despite the added complexity of context processing, ContextCache showed a 3% reduction in average cache hit time compared to GPTCache, likely due to the efficiency of the two-stage filtering and dual-storage design.
  • Cost Efficiency: By successfully serving more accurate hits, the system significantly reduced API token consumption and associated costs.

Discussion and Future Work

ContextCache proves that context is a "first-class citizen" in semantic caching. By moving beyond simple query-to-query matching, it provides a more reliable foundation for conversational AI.

However, several avenues for future research remain. The current implementation is dependent on the ALBERT embedding model; future iterations could explore more modern or domain-specific encoders. Additionally, while the system excels at multi-turn sessions, extending this logic to "long-term memory"—where context spans multiple sessions over days or weeks—presents a unique challenge. Finally, as cache sizes reach millions of entries, further optimization of the fine-grained matching stage will be necessary to prevent it from becoming a computational bottleneck.

Summary updated

BibTeX
@article{yan2025contextcache,
  author = {Jianxin Yan and Wangze Ni and Lei Chen and Xuemin Lin and Peng Cheng and Zhan Qin and Kui Ren},
  title = {{ContextCache: Context-Aware Semantic Cache for Multi-Turn Queries in Large Language Models}},
  journal = {Proc. VLDB Endow.},
  year = {2025},
  doi = {10.14778/3750601.3750679},
}
Figure