Ask an autonomous coding agent to track down a bug in a multi-project repository, and watch what it types into its bash tool:
git grep -i "handle_webhook"
To a software engineer staring at a terminal, git grep is second nature. You scan twenty lines of output in two seconds, ignore eighteen of them, and click on the one file you actually care about.
To an LLM agent, that output is context the model has to process before it can choose the next file to inspect.
Because the agent cannot inspect the output outside its request context, every unranked match becomes part of the material it must evaluate. In the benchmark's naive arm, that leads to full-file reads and a 29,662-token single-turn context across five retrieval scenarios.
The effect compounds when the loop retains context across turns.
The compounding math of the Context Tax
In the modeled multi-turn loop, retained conversation context is included in each subsequent request.
If an agent loads a 25,000-token file dump on Turn 2 of a 10-turn task, those tokens remain in the modeled context for the remaining turns. Provider billing and cache behavior are outside this example.
A single careless search at the start of a session, retained across 9 subsequent turns, creates 225,000 redundant token reads in this model.
Large amounts of irrelevant context can also make relevant evidence harder to retrieve. Liu et al.'s "Lost in the Middle" results establish a retrieval limitation in the tested long-context settings; this post does not claim a measured reasoning effect for coding agents.
The solution: The 4-layer retrieval stack
Instead of treating an autonomous agent like a human with a bash prompt, we engineered a layered local retrieval pipeline in Koality-Assured:
- BM25 with scored snippets (
qmd search): Returns structured JSON with exact relevance scores and 30-line snippet anchors, providing immediate line anchors without loading full files. In the cited public reports, BM25 measured a p50 latency of 187.77ms, an MRR of 0.9062, and Precision@1 of 81.2%. - Dense semantic vector expansion (
qmd vsearch): Employs local quantizedEmbeddingGemma-300MandQwen3-Reranker-0.6B, governed by a strict latency circuit breaker when lexical matching returns zero candidates. - Batch line-bounded retrieval (
qmd multi-get -l 30): Consolidates multi-file retrieval into a single tool invocation with strict line limits, measuring 189.3ms versus 400.6ms for two serial reads—about 2.12x faster. - Concrete Syntax Tree slicing (
ast-grep outline): Inspects code symbol outlines before reading functions. The cited report measured estimated savings of 90.5% to 94.5% across its AST fixtures.
The empirical proof: a 60% context cut
Evaluating five canonical developer retrieval tasks on our production codebase produced this payload comparison:
- Single-turn context load: Reduced from 29,662 tokens (Arm A: raw grep + full reads) to 11,746 tokens (Arm B: ranked search + bounded fetch), a 60.40% direct reduction.
- 10-turn cumulative input reads: Dropped from 296,620 to 117,460 in the retained-context model, a difference of 179,160 reads per 5 tasks.
- Provider cost boundary: This benchmark did not send model API requests or observe cache hits. Stable prefixes may help cache reuse, but any dollar impact requires provider-specific measurements.
Three rules for your agent toolchain
- Remove recursive grep from default tools: Replace ungrounded shell searches with indexed BM25 queries returning scored excerpts.
- Require AST outlines before code reads: Enforce an outline step so the model inspects function signatures before requesting line-bounded source ranges.
- Isolate search indexes in detached worktrees: Operating indexing databases in detached worktrees keeps index writes out of developer branches; any prompt-cache effect still needs provider-specific telemetry.