Manuscript Offprint

arXiv:2610.34763

The Physical Cost of Context: Quality-Constrained Resource Accounting for Coding Agents

Zhongming Yu, Yitong Ma, Hengjia Yu, Boqin Yuan, Xiangye Lin, Hejia Zhang, Jishen Zhao

Abstract

Coding agents resend a growing context on every model call, and context policies are usually judged by the prompt tokens they save. Executed work also depends on call count and prefix reuse: a caching runtime prefills only the tokens after the longest cached prefix. A shorter prompt can therefore cost more if it rewrites cached history. Our quality-gated protocol splits executed prefill into logical prompt minus reused prefix and measures KV residency, time, and energy. On 450 localization cases derived from SWE-rebench V2, preloading ten retrieved blocks (EAGER) and adding anchored history compaction (COMPACT) both pass a five-point non-inferiority gate. In a separate 900-cell study, EAGER cuts model calls by 35.7% and the GPU-energy proxy by 33.2% against on-demand search (GREP); COMPACT saves nothing further. Enabling prefix caching leaves EAGER's prompt volume similar (+8.3%; its 95% CI spans zero) but cuts its uncached prefill by 67.7%. Deleting history can turn a token saving into extra work. With mini-SWE-agent on 100 SWE-bench Verified instances, keeping only the last five observations (MASK-5) cuts EAGER's prompt volume by 33.3% but loses 135.9k reusable tokens per task. Uncached prefill rises by 608.5%, the energy proxy by 125.6%, and the policy misses the quality gate. Policies thus save physical work by shortening the trajectory and can multiply it by editing reusable history.

1 Introduction

Repository localization maps an issue report to files, classes, and functions. Retrieval methods rank files using report–code similarity, structure, and history [1–3]. Modern agents combine iterative retrieval [4], structure-aware search [5–7], or graph navigation [8–11].

These agents repeatedly search, read, and reassess the retrieved source. A ReAct-style loop [12] turns a short issue report into a trajectory of tool outputs and intermediate hypotheses. Larger contexts may improve evidence coverage, but every retained observation can add input processing before decoding (prefill) and occupy the model’s key–value (KV) cache on later turns. On a local device, model weights, KV cache, retrieval services, and the host process also share one memory and power budget.

Many context-management studies report tokens, task success, or provider cost [13–17]. Token counts do not determine physical cost: equal reductions in logical tokens can correspond to different amounts of executed prefill, depending on request boundaries and prefix reuse [18, 19]. On a unified-memory device, model and host allocations also share one pool [20], so we track host allocation, paging, and latency together.

Our agent returns answer blocks for an issue-like query. A frozen retrieval-augmented generation (RAG) path can insert ten retrieved code blocks before the first turn. We ask when logical-token savings predict physical savings, and when cache or trajectory effects break that prediction.

40th Conference on Neural Information Processing Systems (NeurIPS 2026). Workshop: Resource-Aware Agentic AI.

(a) Seed construction

(b) Policy-visible trajectory

Figure

(c) From trajectory to cost

Figure 1: Experimental boundary from repository to device. (a) Frozen dense retrieval selects top-10 blocks R. (b) Policies alter evidence delivery and visible history. (c) Request and prefix-reuse boundaries translate logical prompt P into uncached work U and device cost, which we compare only after checking quality.

Measuring this path (Figure 1) raises three challenges. C1: Trajectory attribution. Evidence supply, delivery time, and history retention can each change what the agent does next. Matched contrasts hold the other mechanisms fixed, and we treat the resulting trajectory as an outcome of the policy. C2: Logical-to-physical translation. A policy changes both the number of requests and prefix reuse within each request. Logical tokens can therefore order policies differently from uncached prefill, KV occupancy, latency, or energy. C3: Quality-constrained comparison. A resource comparison must first bound task-quality loss.

We study four fixed context policies on an NVIDIA DGX Spark: GREP, EAGER, COMPACT, and MASK-5. They differ in when retrieved evidence arrives and which observations stay visible. We make two contributions:

  1. A quality-gated physical-accounting protocol. The protocol combines paired task-quality bounds with model calls, uncached prefill, KV time-area, wall time, and idle-subtracted GPU energy. It charges failures, reports index construction separately, and decomposes executed prefill into logical input minus reused prefixes.

  2. A controlled policy analysis. Byte-identical candidates separate evidence delivery from history reduction. On 450 real-issue localization cases derived from SWE-rebench V2, both preload policies satisfy the quality gate. Instrumented runs show that eager evidence saves work by avoiding calls, anchored reduction adds no saving beyond EAGER, and repeated observation masking can erase prompt savings, consistent with reduced prefix reuse.

References (43)