Skip to main content

When -67% Context Compression Backfires: Re-Reads & Thinking Raised Cost +22.3%

Updated 7 min readMarkdown

💡 AI Labo Benchmark Series — Experiment E58 (Minimal Compressed Target Context vs. UML + NL)

Part of our controlled benchmark series testing AI coding agent harnesses inside network-isolated Proxmox VMs (N=3 counterbalanced AB/BA/AB trials, temperature=0.7, 3-UID grading, 90% prefix-cache cost accounting).

If shrinking an agent's initial code context by -66.85% cuts input tokens by two-thirds, it should slash your API bill—right? Sam-Bodden et al. (arXiv:2607.09691) showed that target + direct-dependency AST slices match full-file edit accuracy at ~1/3 the tokens in single-shot evaluations.

We tested that exact claim inside a tool-using coding loop (inspect_source enabled) on models/gemini-4-carbon (MEDIUM, N=3). Accuracy stayed 32/32—yet mean effective cost jumped +22.32%, revealing when context compression backfires.


TL;DR — Key Empirical Findings

  • 1. 32.0 / 32 Accuracy Parity (0 / 20 Sibling Regressions): Both Baseline (Broad NL Summary + UML Skeletons, 39,817 chars) and Candidate (Minimal AST Slice, 13,198 chars) passed 32.0 ± 0.0 / 32 (100.0%) across all 3 trials on an 8-module SLO credit-settlement pipeline (OBS-512), with 0 decoy reads and 0 broken siblings.
  • 2. The Re-Read Spiral Reversed Trial 1's -13.0% Win into +22.32% Mean Cost: In Trial 1, Candidate trusted the minimal slice (0 dependency inspect_source calls, 4 total calls) and cut effective cost by -13.0% (`$0.2897` vs. `$0.3330`). In Trials 2 & 3, however, Candidate doubted the stripped slice and issued a second inspect_source call to fetch all 4 dependency libraries in full (tool_output_chars exploded 2.43x: 18,463 ➔ 44,867), erasing the -26,619-char head start.
  • 3. +41.36% Higher Thinking Tokens Drove the Net Loss: Even in Trial 1, Candidate spent more internal thinking tokens (13,533 vs. 12,991), averaging +41.36% (18,157.3 ± 5,726.0 vs. 12,845.0 ± 2,067.9). Because thinking/output tokens cost far more than cached input, mean effective cached cost rose from `$0.3150 ± $0.0329` to `$0.3853 ± $0.1151` (+22.32%).
Metric (N=3 Trials, Mean ± SD) Baseline: Broad NL + UML (39,817c) Candidate: Minimal AST Slice (13,198c) Delta / Effect
Hidden Grading Suite (32 Cases) 32.0 ± 0.0 (100.0%) 32.0 ± 0.0 (100.0%) 100% parity (16 edit + 16 guard)
Initial Context Size (vs. 41,569c) 39,817 chars (0.9579x) 13,198 chars (0.3175x) -66.85% (-26,619 chars)
LLM Calls / Dependency inspect 4.00 ± 0.00 / 2.00 ± 0.00 4.67 ± 0.58 / 2.67 ± 2.31 +0.67 extra re-read round (T2/T3)
Tool Output Volume (chars) 30,721.0 ± 0.0 36,065.7 ± 15,244.4 +17.40% (2.43x spike in T2/T3)
Prompt Tokens (prompt) 116,866.0 ± 5,025.2 114,067.7 ± 31,174.8 -2.39% (T1 -31.2%, T2/3 erased)
Thinking Tokens (thoughts) 12,845.0 ± 2,067.9 18,157.3 ± 5,726.0 +41.36% (+5,312.3 tokens)
Total Tokens (total_tokens) 134,330.7 ± 6,970.8 136,952.0 ± 36,471.7 +1.95% (5.2x higher variance)
Elapsed Time (elapsed_seconds) 222.98s ± 39.62s 288.34s ± 110.92s +29.31% (+65.36s)
Effective Cached Cost (90% OFF) `$0.3150 ± $0.0329` `$0.3853 ± $0.1151` +$0.0703 (+22.32%, SD 3.5x)
List-Price Upper Bound (No Cache) `$0.4433 ± $0.0339` `$0.5027 ± $0.1292` +13.40% (`+$0.0594`)
Output Cap Hits (MAX_TOKENS) 0 / 3 0 / 3 Zero truncation

1. Experiment Design & Architecture

+-----------------------------------------------------------------------+
| Proxmox VM262 (0 NICs, gemini-4-carbon MEDIUM, temp=0.7, N=3 AB/BA)   |
| Task: 8-Module SLO Credit Pipeline (4 Target Fns + 8 Direct Deps)     |
|                                                                       |
| Baseline (Broad NL Summary + UML Skeletons, 39,817 chars):            |
|   Always 4 calls: inspect(4 mods + 2 UML libs) -> replace -> test ->  |
|   submit | 12,845 thoughts | $0.3150 eff. (SD $0.0329) | 32/32 PASS   |
|                                                                       |
| Candidate (Minimal AST Slice, 13,198 chars, -66.85%):                 |
|   T1: Trusts slice -> 4 calls, 79.5k prompt, $0.2897 (-13.0%)         |
|   T2 & T3: Doubts stripped slice -> re-reads 4 full libs via inspect  |
|   -> 5 calls, 18,157 avg thoughts (+41.4%) -> $0.3853 eff. (+22.3%)   |
+-----------------------------------------------------------------------+

Notice the stark contrast with single-shot slicing benchmarks (E21, arXiv:2607.09691): once an autonomous agent has inspect_source, stripping docstrings and sibling structure without guaranteeing slice completeness triggers defensive re-reads and double-checking thoughts.


2. Drop-In Implementation Recipe: Attested AST Slice Manifest

Never hand an agent a stripped AST slice in silence—attach an explicit Slice Completeness Manifest telling the model what is guaranteed complete so it avoids redundant inspect_source round-trips:

import ast


def build_attested_ast_slice(
    target_sources: dict[str, str],
    direct_dep_sources: dict[str, str],
    target_symbols: set[str],
) -> str:
    """Projects target + direct callee bodies and attests completeness to prevent re-reads."""
    extracted, included_names = [], []
    for mod_map in (target_sources, direct_dep_sources):
        for fname, src in mod_map.items():
            tree = ast.parse(src)
            for node in tree.body:
                if isinstance(node, (ast.FunctionDef, ast.ClassDef)) and node.name in target_symbols:
                    extracted.append(f"# {fname}:{node.lineno}\n{ast.unparse(node)}")
                    included_names.append(f"{fname}::{node.name}")
    manifest = (
        "# SLICE_COMPLETENESS_ATTESTATION: COMPLETE_DIRECT_CALLGRAPH\n"
        f"# Included symbols ({len(included_names)}): {', '.join(included_names)}\n"
        "# Rule: All direct callees & constants are included verbatim below.\n"
        "# Do NOT re-read full library files unless a test error cites a missing symbol.\n"
    )
    return manifest + "\n\n".join(extracted)
  • Why this works: Adding a 4-line completeness attestation removes the uncertainty that drove Trials 2–3's extra inspect_source calls (+26,404 chars of tool output) and +41.36% thinking overhead.

3. Threats to Validity & When NOT to Use This

  1. Repo Scale (41,569 chars) vs. Large Monorepos: On an 8-module (41.5k-char) codebase, Baseline's 39,817-char broad projection easily fit in context and gave calming global visibility (SD $0.0329). In 100k+ line monorepos where broad dumps exceed context budgets or dilute attention, minimal slicing becomes mandatory.
  2. Trial 2 Cache Miss & Split Timing: Trial 1 ran the night before Trials 2–3 (--resume-dir), and Trial 2's Candidate hit a 0-cached-token miss on seq=3 ($0.5130). Even in Trial 3 with 65.6% cache hits, Candidate still cost more ($0.3531 vs. $0.3349) due to re-reading libraries.
  3. Evaluation Boundary: Measuring cumulative multi-turn cost + thinking tokens—not just Turn-0 input compression—is essential whenever agents have file-read tools.
📋 Experiment Metadata & Reproducibility
  • Experiment ID: E58 (C054 / arXiv:2607.09691 / Benchmark v2.1)
  • Run ID: e58-20261121-vm262-01 (N=3 trials: AB ➔ BA ➔ AB, temperature=0.7)
  • Sandbox Environment: Proxmox Isolated VM VM262 (0 NICs, Debian 13, 3-UID isolation: labexp58 / labrunner58 / labjudge58)
  • Model & Thinking Level: models/gemini-4-carbon (thinkingLevel="MEDIUM")
  • Cost Accounting: Google 90% implicit prefix-cache discount (effective_cached_micro_usd) as primary metric; list-price upper bound reported alongside.

Discussion: When you pass stripped AST slices or Tree-sitter skeletons to a coding agent, how often does it immediately call read_file to re-fetch the full file anyway?

🔗 Continue Reading the Series

No comments yet. Be the first to comment.