The Physical Cost of Context: Quality-Constrained Resource Accounting for Coding Agents
Organizations: University of California, San Diego · University of Massachusetts Amherst
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.
Figures & tables
| Metric | Grep | Eager | Compact |
|---|---|---|---|
| control | vs. | vs. | |
| Task quality | |||
| Recall@5 | .470 | .478 | .494 |
| Full@5 | .307 | .318 | .329 |
| Format success | .904 | .976 | .987 |
| Runtime completion | 1.000 | 1.000 | 1.000 |
| Metric | Mini | Eager | Compact | Mask-5 |
|---|---|---|---|---|
| control | vs. | vs. | vs. | |
| Task outcome (of 100 attempts) | ||||
| Resolved | 30 | 29 | 30 | 26 |
| Submitted / call-capped | 45 / 55 | 44 / 56 | 45 / 55 | 40 / 60 |
| Generation resource per attempt | ||||
| Prompt ktok | 220.7 | 225.6 (+2.2%) | 210.8 (−6.5%) | 150.5 (−33.3%) |
Appendix figures & tables4 assets
Supplementary material from the paper’s appendix.
Appendix
| Field | Captured value |
|---|---|
| Host | NVIDIA GB10, 128 GB unified memory, aarch64 |
| OS / driver | Ubuntu 24.04.4 LTS; kernel 6.17.0-1026-nvidia ; NVIDIA driver 580.159.03; CUDA 13.0 |
| Primary target | Qwen3.6-35B-A3B-FP8, revision 95a723d08a94 |
| Primary runtime | vLLM 0.19.2rc1.dev134+gfe9c3d6c5 |
| Cache intervention | Same Qwen3.6/vLLM stack; treatment adds only --enable-prefix-caching , with a reset before each policy–repository block |
| External target | Qwen3.8-27B-FP8 [ 49 ] , revision 017b9c7af6b5 |
| Policy | Recall@5 | Prompt | Uncached | Req. | Wall | KV % s | kJ |
|---|---|---|---|---|---|---|---|
| Grep | .772 | 98.779k | 18.245k | 10.34 | 56.76 | 165.8 | 1.474 |
| Eager | .800 | 68.456k | 13.749k | 7.15 | 42.71 | 118.9 | 1.080 |
| Compact | .802 | 84.226k | 19.192k | 8.87 | 52.16 | 133.8 | 1.362 |
| Regime | Policy | Final % | Output/call | Uncached/call | TTFT s | E2E/call s |
|---|---|---|---|---|---|---|
| Primary | Grep | 7.9 | 160.4 | – | 2.288 | 5.218 |
| Eager | 12.0 | 190.2 | – | 2.312 | 5.721 | |
| Compact | 10.7 | 182.3 | – | 2.244 | 5.468 | |
| Exploratory (SGLang) | Grep | 9.7 | 126.2 | 1.765k | 4.049 | 5.410 |
| Eager | 14.0 | 137.7 | 1.923k | 4.161 | 5.840 | |
| Compact | 11.3 | 126.5 | 2.164k | 4.288 | 5.692 |
| Repository | Lang. | Commit | Repository | Lang. | Commit | |
|---|---|---|---|---|---|---|
| astropy/astropy | Py | 0df94ff70979 | tokio-rs/tokio | Rust | ab53bf0c4727 | |
| matplotlib/matplotlib | Py | 6786f437df54 | uutils/coreutils | Rust | a40884758329 | |
| pydata/xarray | Py | 45c0a114e2b7 | axios/axios | TS/JS | c6cce43cd944 | |
| scikit-learn/scikit-learn | Py | 97523985b39e | babel/babel | TS/JS | 35116224d21e | |
| sympy/sympy | Py | d7c304511569 | facebook/docusaurus | TS/JS | 5e9e1d051b22 | |
| caddyserver/caddy | Go | 4512be49a9fa | preactjs/preact | TS/JS | c77e6284c199 |