arXiv(opens in a new tab)cs.CLOct 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2610.28577

Cache Engineering for Agents

Tianyu Fu, Ruoxi Wang, Zihan Min, Zhongqi Ren, Zhihao Li, Tengxuan Liu, Shengxu Mu, Hanling Zhang, Yu Wang

ABSTRACT

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.2×1.2\times the output throughput and a 1.5×1.5\times 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.

1 INTRODUCTION

Large Language Model (LLM) agents are capable of increasingly long-horizon tasks. Agents are generally composed of an LLM for reasoning and tool calling, and a harness that orchestrates tool execution and context. Through repeated reasoning, tool use, and environment interactions, they can carry out complex work spanning hundreds of rounds (Ye et al., 2025; Liu et al., 2025b).

As agent trajectories grow longer, Key-Value (KV) cache growth poses challenges to both inference cost and task accuracy. In terms of cost, retaining and repeatedly accessing accumulated KV caches incur substantial storage and I/O overhead (Wu et al., 2026; Anthropic, 2026). As shown in Figure 13, KV cache reads take up 82.4% of the total API cost. In terms of task accuracy, agents can suffer severe degradation if either the number of attended KV states or their positional range exceeds what LLM handles reliably (Hsieh et al., 2024; Chen et al., 2026b; Zhou et al., 2025). Therefore, it requires delicate control over the active KV cache for efficient and reliable agent execution.

Prior work reduces the growing cache via context engineering, typically by trimming or summarizing historical messages. While older messages are often less useful, editing them changes the trajectory prefix, invalidates subsequent KV states, and triggers re-prefill. The earlier the edit, the more retained trajectory suffix that must be re-prefilled. Common harnesses therefore defer reductions until an active context length threshold is reached, then trim or summarize most of the trajectory and

*Equal contribution. †Corresponding author: Yu Wang (yu-wang@tsinghua.edu.cn).

Figure

Figure 1: Full-context inference, context engineering, and cache engineering. Full-context inference reuses all historical KV. Context engineering removes irrelevant text and recomputes the retained history. Cache engineering keeps the text history but drops selected KV entries, reusing retained states without recomputation. Nested color blocks illustrate how retained KV can still carry information from dropped history.

retain only a few recent rounds to keep re-prefill short (Pi, 2026; OpenAI, 2026). Such aggressive reductions risk losing useful information from intermediate rounds, exposing a dilemma between re-prefill cost and task accuracy.

In this paper, we introduce cache engineering, a new scheme for agents to directly control their active KV cache. As shown in Figure 1, instead of modifying the text trajectory, it excludes only selected KV entries from future attention, and reuses all retained KV states without re-prefill. This allows agents to incrementally drop older KV entries while retaining more suffix KV within a given cost budget. To support this control, alongside the usual append operation that adds KV states for new tokens, we introduce two additional cache operations. Drop removes selected KV entries from future attention, and repos adjusts the positions encoded in retained keys. Throughout execution, the LLM and harness choose when and how to apply these operations, repeatedly interleaving them with append to shape the active cache. This programmable control also enables agents to search, evaluate, and refine cache engineering strategies through recursive self-improvement (RSI).

However, the history-dependent nature of cache engineering challenges current LLM serving systems. With standard KV caching, the token prefix fully determines the cached states (Kwon et al., 2023; Zheng et al., 2024). In contrast, the same token prefix and retained positions can result in different cache states under different cache operation histories. As shown in Figure 5, trajectories ① and ② both retain KV entries for B and C, but C's KV is computed with attention to A only in ①, due to the different ordering of drop and append operations. The serving system must therefore account for both token and cache operation histories to correctly track or reuse cache states.

Thus, we co-design AKV, a user-friendly and efficient serving system for cache engineering. It provides a stateless interface compatible with existing API-based serving, while handling the complexity of stateful cache management internally. Agents simply submit messages as usual, accompanied by cache operation instructions, without tracking which KV states remain resident on the server. The system uses an extended radix tree for correct reuse and timely eviction of history-dependent cache states, while segment-batch attention efficiently reconstructs missing states in a single prefill pass. AKV turns the theoretical efficiency potential of cache engineering into lower wall-clock latency and higher throughput for real-world agent workloads.

• Cache engineering formulation. We formalize cache engineering through explicit cache operations and their trajectories, providing a clear and deterministic description of history-dependent cache states. This formulation also provides a programmable interface for agents to define and refine their own cache engineering strategies.

• Serving system for cache engineering. We propose the first serving system that allows agents to directly control their caches, which were previously managed solely by the system. We co-design a stateless interface, an extended radix tree, and segment-batch attention to support efficient cache reuse, eviction, and restoration.

• Improving efficiency and benchmark performance. Across three LLMs and four agent benchmarks, a simple tool-response rolling-drop cache policy reduces average cost by

27.7% and improves average benchmark scores by 1.7 points, compared with conventional threshold-triggered summarization. With AKV server, this translates to 1.18×1.18\times the output throughput and a 1.51×1.51\times speedup in P95 request latency. An RSI-based policy additionally reduces cost by 7.1% on held-out tasks.

References (48)