This article explores two distinct caching strategies for Large Language Models (LLMs): prompt caching and semantic caching. It details how each method addresses the challenges of high inference costs and latency in LLM applications by reusing computation or generated answers, thereby improving efficiency and user experience. Understanding these strategies is crucial for designing performant and cost-effective AI systems.
Read original on ByteByteGoCaching is a fundamental system design principle for large-scale systems, particularly relevant to Large Language Models (LLMs) due to their inherent processing costs (tokenization, input/output generation). LLM requests can be expensive, often involving thousands of input tokens (instructions, context, chat history) for a simple user question, and output tokens also accrue costs. Reducing repetitive work is key to optimizing both cost and response time.
Prompt caching focuses on reusing the prefill stage of an LLM's text generation process. When an LLM processes input, it converts text into numerical representations (KV states). If multiple requests share an identical prefix (e.g., system instructions, a long product manual), the KV states for that prefix can be cached and reused, avoiding redundant computation. This requires the shared content to be placed at the *beginning* of the input, as token representations are context-dependent and influenced by preceding tokens.
Key Constraint for Prompt Caching
For effective prompt caching, the shared prefix must be exactly identical in wording, order, and settings. Even minor changes, like a timestamp or a different question placed before the shared context, can prevent cache hits because they alter the internal representation of the subsequent tokens.
Prompt caching primarily reduces input processing work and can lower the wait time before response generation. It doesn't eliminate the cost of generating new output tokens. LLM providers often offer discounted rates for cache reads, but initial cache writes may incur higher costs. The more reads per write, the greater the savings.
Semantic caching operates at the application layer, aiming to reuse previously generated LLM answers for semantically similar questions, even if the phrasing differs. This is achieved using an embedding model to convert questions into numerical vectors (embeddings). When a new question arrives:
Semantic caching can bypass the LLM entirely on a cache hit, significantly reducing both input and output token costs. However, embedding generation, vector database lookups, and storage still consume resources. The main challenge lies in determining the optimal similarity threshold to balance recall (returning relevant answers) and precision (avoiding irrelevant answers).