面向智能体的缓存工程

·

研究论文 · 2026/10/22

大语言模型(LLM)智能体处理复杂任务时,轨迹增长导致键值(KV)缓存I/O和存储成为主要成本,且可能超出最大上下文长度。现有上下文工程通过修剪或总结历史来减少缓存大小,但修改轨迹前缀会使后续缓存状态失效并触发昂贵的重新预填充,且保留上下文过少会影响任务准确性。本文提出缓存工程新方案,允许智能体直接控制活跃KV缓存,而不修改轨迹或重新预填充保留上下文。具体地,智能体可主动使用drop操作将选定缓存排除在未来注意力之外,使用repos操作调整保留键的位置编码。作者形式化了这些缓存操作,并指出历史依赖性是一个服务挑战:相同令牌序列在不同缓存操作序列下可能产生不同缓存状态,使得前缀匹配不足以正确重用缓存。因此,他们共同设计了AKV服务引擎,提供与现有服务API兼容的无状态接口,同时在内部管理有状态缓存重用、驱逐和恢复。实验表明,工具响应滚动丢弃缓存策略相比传统阈值触发总结,平均基准分数提高1.7分,平均成本降低27.7%;使用AKV可实现高达1.2倍的输出吞吐量和1.5倍的P95请求延迟加速。缓存策略的递归自我改进进一步比专家设计降低成本7.1%。代码见https://github.com/thu-nics/AKV。

该文章提出了一种新颖的缓存工程方法,直接优化LLM智能体的KV缓存管理,显著降低成本和延迟,同时保持任务准确性,对高效部署LLM智能体具有重要实践意义。

Large Language Model (LLM) agents can handle increasingly complex tasks, navigating long trajectories of observations and actions. As these trajectories grow, key-value (KV) cache I/O and storage become the dominant cost. Trajectories can also exceed the LLM's maximum context length, requiring control over active cache growth. Prior work uses context engineering to reduce cache size by trimming or summarizing history. However, modifying the trajectory prefix invalidates subsequent cached states and triggers costly re-prefilling of retained context. Task accuracy can also suffer when too little context is retained to limit re-prefill costs. We propose cache engineering as a new scheme. It allows agents to directly control their active KV cache without modifying the trajectory or re-prefilling retained context. Specifically, agents can proactively use drop to exclude selected cache from future attention and repos to adjust retained keys' position encodings. We formalize these cache operations and identify history dependence as a serving challenge: identical token sequences can yield different cache states under different cache operation sequences, making prefix matching insufficient for correct cache reuse. Thus, we co-design the AKV serving engine. It provides a stateless interface compatible with existing serving APIs, while internally managing stateful cache reuse, eviction, and restoration. Experiments show that a tool-response rolling-drop cache policy improves average benchmark scores by 1.7 points and reduces average cost by 27.7%, compared with conventional threshold-triggered summarization. With AKV, it achieves up to 1.2x the output throughput and a 1.5x speedup in P95 request latency. Recursive self-improvement of cache policies further reduces cost by 7.1% over expert design. Our code is at https://github.com/thu-nics/AKV.

阅读原文

参与讨论