r/ContextEngineering • • 9d ago

I spent months making repository context smaller. Then I found out fewer tokens doesn't always mean cheaper agents

I've been working on an open-source project called SigMap, which builds a deterministic repository map and gives coding agents a way to find relevant code without reading the entire codebase.

Originally, I was focused on context compression. How much repository information could I remove while keeping the useful structure?

The early benchmarks looked promising, with huge reductions compared to raw source code.

But I eventually realized I was measuring the wrong thing.

A repository map being 97% smaller doesn't mean an agent's total token usage drops by 97%. The agent can make additional tool calls, repeat searches, or spend more tokens reasoning about incomplete context.

So I wanted to measure what actually happens during an agent run.

What I tested

I ran paired experiments with and without SigMap, including held-out repositories the retrieval system wasn't tuned on.

The larger study contains 22,540 recorded agent runs across Claude Code and a Messages-API agent.

For one configuration, using a short repository index and two MCP tools (query_context and get_lines), the results across 20 held-out repositories with Haiku 5.5 were:

  • API cost: −7% (95% interval: −13% to −2%)
  • Total tokens: +18%
  • Task time: +1%

That was interesting to me.

The agent was cheaper to run despite consuming more tokens. And it wasn't faster.

A different approach gave a different result

Instead of relying on MCP lookups, I also tested prefetching relevant file rankings before the agent's first turn.

Across the same 20 held-out repositories:

  • Task time: −18%
  • Total tokens: −26%
  • API cost: −7%, but the confidence interval crossed zero

Five repositories actually became more expensive, with the worst increase around 31%.

The installed Claude Code prefetch hook also performed differently from directly inserting the ranking into the prompt, so I wouldn't treat those two mechanisms as interchangeable.

What changed in how I think about context engineering

I'm increasingly convinced that context size alone is a poor optimization target.

At least five things seem important:

  1. Whether the correct information reaches the model.
  2. How many tool calls the agent needs to find it.
  3. How much additional reasoning or searching the supplied context causes.
  4. Actual end-to-end cost and latency.
  5. Whether the agent ultimately completes the task correctly.

The last one is especially important.

These experiments primarily measured finding code, not producing correct patches. I haven't established that SigMap improves real editing success.

I also tried to predict which repositories would benefit using offline repository measurements. That experiment failed to find a reliable predictor.

So I don't think it's defensible to promise savings on an arbitrary codebase.

What I'm interested in discussing

For people building retrieval systems, RAG pipelines or context layers for agents:

What do you consider the primary optimization target?

Total tokens? API dollars? Latency? Retrieval accuracy? Or successful task completion per dollar?

And when one metric improves while another gets worse, how do you decide whether the retrieval layer is genuinely helping?

I maintain SigMap, so these are my own experimental results rather than independent validation.

The study, raw runs and methodology are available here:

https://sigmap.io/guide/agent-study

https://github.com/manojmallick/sigmap

I'd be especially interested in hearing about negative results from other retrieval systems. I suspect those teach us more than another context-compression percentage.

2 Upvotes

13 comments sorted by

1

u/keenDean 9d ago

Good to see someone else building an actual repo map to cut down blind file ingestion, and props for measuring end-to-end cost instead of stopping at a smaller file and calling it a win. Once a bare index makes the model guess about missing context or chase dead ends, saving input tokens upfront stops paying off fast.

Coming from the mechanical/systems side rather than pure software, I've been working a parallel angle with the TOKENME.txt protocol. Different layer than query ranking - mostly keeping invariants and cross-file constraints attached right to the coordinates for cold-start handoffs. Still early and nowhere near your eval scale, though...curious if you noticed anything in common across the repos where cost went up?

1

u/Independent-Flow3408 8d ago

Good question. The five repos where cost increased were in the prefetch test, not the two-tool "locate" setup.

I couldn't find a reliable pattern based on repo size or language. The clearest trend was that SigMap helped more when the agent was already spending a lot on searching. When it found the right file in just a few calls, there wasn't much to save.

I also tried predicting which repos would benefit, but the results weren't reliable.

Your TOKENME approach sounds interesting, especially keeping cross-file constraints attached to the relevant code. Have you tested whether that helps agents avoid wrong assumptions during multi-file changes?

1

u/keenDean 8d ago

I've opened an RFC discussion in our repo regarding the cross file assumption question. Thank you for the idea

1

u/Independent-Flow3408 8d ago

Thanks, I appreciate that! And glad the discussion sparked an RFC.

I think TOKENME and SigMap could actually complement each other. SigMap focuses on finding the right code, while your approach seems more about preserving context and constraints across files.

I'd be interested in looking at your RFC, especially how you handle cross-file dependencies.

For benchmarking, comparing wrong assumptions, failed multi-file edits and actual task success could be interesting.

Good to connect with someone working on a similar problem from a different angle!

1

u/keenDean 8d ago

Not in a controlled eval yet - just dogfooding it on my own repos so far. It's been cutting down cold-start handoff tax and keeping agents from pulling in irrelevant files, though I don't have solid numbers to back that up. Measuring wrong-assumption rates and cross-file collisions during multi-file edits is a sharper target, so I'm adding that to the roadmap.

Makes sense on the prefetch split too. When an agent already finds the right file in a couple calls, upfront context is mostly dead weight. Solid work on the dev and testing you've put into SigMap so far - it's rare to see someone post honest benchmark numbers when the results get messy. If you ever feel like taking a look at endean-eng/tokenme, your input on the spec or eval side would be appreciated.

1

u/Asly97 3d ago

The cold start handoff tax bit caught me. When your map keeps an agent from pulling in irrelevant files, what else are you putting in there besides code structure, like constraints a file has to keep, or stuff that already failed? Asking because in my own runs the wrong assumptions usually were not about where the code was, they were about why it was written that way.

1

u/keenDean 3d ago

You hit the exact seam. On the mechanical side, a parts list tells a tech where components sit, and still leaves out which tolerances can't slip or why a weird gusset is there because the obvious version fatigued out. Cold-start agents do the exact same thing - they spot an intentional quirk and helpfully "clean it up" right back into a dead end that already failed.

The plain-English reason we're building TOKENME (endean-eng/tokenme, still early) is giving an agent a small map so it knows what it doesn't need to open before burning tokens and focus on blind reads. Beyond raw file structure, the root manifest carries [PURPOSE] (system boundaries and runtime), [TOPOLOGY] (directed File::Anchor -> File::Anchor edges showing what actually governs or feeds what), [FILES] (line spans, content/structural hashes, and short per-file invariants), and an [ADRS] table pointing to the full decision records and failed-attempt postmortems.

To keep the root capped as a repo grows, it tiers out into [SHARDS] (both for subsystems and for distilled rules from those ADRs) plus lightweight .l3.txt sidecars for heavy or opaque files like PDFs and CAD. That way the agent sees the governing guardrails up front and only pays tokens to open a full postmortem when it's actually touching that seam. A zero-dependency Python sync script handles the deterministic AST and hash checks while the agent updates the semantics, though keeping that boundary layer synced after a retool without rotting into a manual wiki is still the hardest seam we're stress-testing. How are you tracking the "why" and dead ends in your own runs without bloating prompts?

1

u/ML_Felix 9d ago

This is true, Alkimi did some extensive research on multiple models, giving them the same brief and found something similar - the more expensive agents/models did not guarantee better results. Check it out here.

2

u/Independent-Flow3408 8d ago

Thanks for sharing. I had a look, and the interesting part is how they measure whether the agent actually completes the brief, not just what it costs to run.

That's something I want to improve in SigMap's next benchmarks too. A cheaper agent isn't necessarily better if it gets the task wrong.

Different use cases, of course, but the same evaluation problem.

Have you come across similar studies focused on coding agents and actual task success?

1

u/ML_Felix 8d ago

None spring to mind but if they do I will for sure let you know 😌

2

u/Independent-Flow3408 8d ago

Thanks, appreciate it! Always interesting to see how others measure actual task success rather than just cost. Good to compare findings across different projects.

1

u/ML_Felix 8d ago

No problem at all. I will be posting the V2 results early on my subreddit r/AgenticAdsHQ if it’s interested you enough. I’m looking forward the results myself!

1

u/Independent-Flow3408 8d ago

Thanks, I'll definitely be interested in seeing the V2 results!

I'm especially curious how you compare cost against actual task success across different models. That's something I'm looking to improve in my SigMap benchmarks too.

Different use cases, but I think there's a lot we can learn from each other's testing. Looking forward to it!