概述
对于小型、稳定的语料库而言,这一点很重要,因为它消除了检索错误和索引维护,同时回答速度快于每次重新阅读文档。
深入探讨
Cache-augmented generation, or CAG, replaces the retrieval step with preloading. Instead of searching a knowledge base for each question, the system feeds the entire knowledge base to the model once, saves the resulting key-value (KV) cache, and reuses that cache for every subsequent query. The approach was described in a December 2024 paper titled Don't Do RAG: When Cache-Augmented Generation is All You Need for Knowledge Tasks. The KV cache is the set of intermediate attention keys and values a transformer computes for every token it has read. Normally it is built during a single request and discarded. In CAG, the documents are processed ahead of time, the cache is stored, and each question is appended after the cached prefix. The model only computes the new question tokens before generating, so answers start faster than if the documents were re-read each time. After answering, the appended tokens are truncated so the cache returns to its documents-only state for the next query. The benefits are no retrieval errors, since every document is always present, no embedding index or vector database to maintain, and lower per-query latency than naive long context. The limits follow directly: the whole corpus must fit in the model's context window, the model must still reason well over that much text, and any change to the documents means rebuilding the cache. CAG therefore fits small, stable knowledge bases such as a product manual, a policy set, a syllabus or a fixed regulatory text. It is a poor fit for large, fast-changing or permission-filtered collections. A common misconception is that CAG trains the model. Nothing is fine-tuned; the weights are unchanged. Commercial prompt caching from API providers is a hosted relative of the same idea.
现实世界的实施
一家拥有60页员工手册的公司将其预加载到开源模型的 KV 缓存中,这样每个 HR 问题都能在完整手册在场的情况下得到回答,无需维护向量数据库。
一家软件供应商为支持助手缓存产品手册,并仅在发布新手册版本时重新构建缓存。
教师预加载课程大纲和讲义,以便学习助手在整个学期内能够快速回答学生提问。
合规团队使用托管 API 的提示缓存来保持固定的监管文本作为缓存前缀,并为在多次提问中重复读取支付较低的费用。
缓存增强生成的未来 (CAG)
CAG 的实用范围随着上下文窗口的延长和 KV 缓存压缩的改进而扩大,但仍受内存以及模型对超长输入的推理能力的限制。混合设计是自然的下一步:缓存一套经常需要的核心文档,按需检索长尾部分。对缓存压缩、将缓存卸载到更廉价存储以及选择保留哪些 token 的研究,可能使更大的预加载语料库变得可行。当前最合理的测试是实证的:在你自己的问题、成本和更新频率下,对比 CAG、RAG 和普通的长上下文。
付诸实践
为什么这很重要
- 成本与预算. 多年来,架构决策决定着性能和运营成本。
- 更清晰的判决. 技术教育帮助团队选择正确的堆栈,而不仅仅是最新的堆栈。
- 质量控制. 更好的工程选择可以减少生产中的可靠性事故。
留意
- 优化一项基准测试可以隐藏更广泛的系统弱点。
- 基础设施和维护成本常常被低估。
- 随着系统变得更加复杂,安全性和可观察性差距可能会扩大。
后续步骤
- 在实施之前定义延迟、质量和成本目标。
- 在实际负载和数据条件下进行基准测试。
- 仪器监控错误、漂移和用户影响。
- 在扩展之前准备回滚和事件响应路径。
不断探索
常见问题
什么是缓存增强生成(CAG)?
缓存增强生成(CAG)在语言模型的键值(KV)缓存中预加载整个固定知识库一次,并在每个问题中重复使用该缓存,而不是像 RAG 那样对每个查询检索文档。这对小而稳定的语料库很有意义,因为它消除了检索错误和索引维护,同时比每次重新阅读文档更快地给出答案。
CAG 用什么代替每次查询的检索?
CAG 预先计算语料库的 KV 缓存,并在每次查询时重复使用。
Transformer 中的 KV 缓存是什么?
注意力层的键和值会被缓存,这样之前的 token 就不需要重新计算。
为什么 CAG 系统在每个答案后会截断缓存?
移除追加的 token 会将缓存重置为干净的预加载状态,以便下一个问题使用。
CAG 的主要限制是什么?
因为每份文件都已预加载,语料库受限于上下文窗口和可用内存。
为什么 CAG 的回答速度比将文件粘贴到每个请求中更快?
文件的计算已完成并存储,只在每个查询中处理问题。