CAG vs Long Context: How AI Models Use and Remember Information
Summary
Long context and Cache Augmented Generation (CAG) are distinct methods for providing Large Language Models (LLMs) with external knowledge at inference time, building upon each other. Long context involves stuffing all relevant documents directly into the prompt, leveraging rapidly expanding context windows like GPT-3's 1,000 tokens (2020), GPT-4 Turbo's 128,000 tokens (2023), and Google Gemini 1.5 Pro's 2 million tokens (2024). While simple, this approach incurs high costs and latency due to reprocessing documents per query, and suffers from a "lost in the middle" effect. CAG addresses this by pre-computing and persisting the LLM's internal Key Value (KV) cache from documents, allowing subsequent queries to load the cache and append new questions for faster, cheaper inference, showing 10x to 40x speedups. CAG is best for stable knowledge bases and repeated queries, while long context suits single-use analyses. Prompt caching, offered by major LLM providers, implements CAG as a service, providing up to a 90% discount on cached token reads.
Key takeaway
For AI Engineers or ML Architects designing LLM applications requiring external knowledge, consider the query frequency and knowledge base stability. If your application involves repeated queries against a stable dataset, prioritize Cache Augmented Generation (CAG) or prompt caching to significantly reduce inference costs and latency. For one-off document analyses, leveraging large context windows directly is simpler and more efficient, avoiding unnecessary pre-computation overhead.
Key insights
Long context and Cache Augmented Generation (CAG) offer distinct strategies for LLM external knowledge access, balancing simplicity, cost, and performance.
Principles
- Context window size impacts LLM knowledge access.
- Repetitive processing increases LLM inference costs.
- KV cache reuse significantly boosts inference speed.
Method
CAG involves three phases: knowledge preparation (formatting documents), pre-computation (generating and saving KV cache), and inference (loading cache, appending query, generating response).
In practice
- Use long context for single-query document analysis.
- Implement CAG for stable, frequently queried knowledge bases.
- Utilize prompt caching for cost-effective API-based CAG.
Topics
- Large Language Models
- Context Window
- Cache Augmented Generation
- Prompt Caching
- LLM Inference
- Key Value Cache
Best for: AI Engineer, Machine Learning Engineer, AI Architect
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by IBM Technology.