Give Your AI Agent Codebase Context Without Stuffing the Prompt

repowise teamUpdated 8 min read
On this page

To give an AI agent codebase context without stuffing the prompt, index the repository once and let the agent query that index through the Model Context Protocol (MCP), pulling only the files, symbols and history the task needs. The agent asks for an overview, then for the two or three files that matter, instead of starting every session with a dump of the source tree. In a measured agent loop, that approach cut output tokens 31.6% against a bare agent.

Part of the hub on codebase context for AI agents.

Why prompt stuffing fails

Prompt stuffing fails because a larger window does not make a model attend to all of it equally, and because you pay for every token on every turn. Context windows now reach hundreds of thousands of tokens, and some models offer a million or more. A repository still rarely fits, and fitting is not the same as working.

Three problems show up in practice.

  • Position matters. Liu et al. found that models answer best when the relevant passage sits at the start or end of a long input and worse when it sits in the middle (Lost in the Middle, TACL 2024). The interface definition in the 23rd pasted file is exactly the kind of fact that gets underweighted.
  • Raw source is mostly noise for the question. Imports, boilerplate, fixtures and unrelated functions all compete for attention with the one function the agent has to change.
  • The cost recurs. A stuffed prompt is resent on each turn. Prompt caching lowers the price of a repeated prefix, but a cache entry is invalidated when the prefix changes, and an edited codebase changes the prefix.

The fix is to change what the agent is given. Source code is the raw material. What an agent usually needs first is derived: what the module is for, what depends on it, who changes it, whether it is risky to touch.

Three ways to give an agent context

There are three common approaches: retrieval over code chunks, a hand-written context file, and tools the agent calls on demand. They are not exclusive, and most teams end up combining them.

Vector retrieval (RAG)Context file (CLAUDE.md, AGENTS.md)Structured MCP tools
What the agent getsCode chunks similar to the queryA short handbook written by a personAnswers computed from a graph, git history and docs
Good atFinding a function by meaningConventions, commands, house rulesArchitecture, impact, ownership, risk, history
Stays currentWhen re-embeddedOnly when someone edits itWhen the index updates
Cost per sessionRetrieved chunks each queryThe whole file, every sessionOnly what each call returns
Weak at"How does data flow from the API to the database?"Anything that changes weeklySubjective team norms

Scroll the table sideways to see every column.

A context file is the right home for rules a human has to state, such as how to run tests and what not to touch. Retrieval is good for "where is the code that does X". Questions about structure and consequence (what breaks if I change this, why is it built this way) are graph and history questions, and a graph answers them directly.

How the MCP approach works

With MCP, the agent investigates in a short sequence of narrow calls and reads raw source last. repowise exposes ten tools by default, described in the MCP tools reference: get_answer, get_context, get_symbol, search_codebase, get_risk, get_change_risk, get_why, get_overview, get_health and get_dead_code.

A typical investigation for "add rate limiting to the login endpoint" looks like this.

  1. get_overview() returns the architecture map, entry points and module layout. The agent now knows where authentication lives without listing a directory.
  2. get_answer("how does login authentication work?") returns one cited answer with the supporting evidence, so the agent often does not need a second search.
  3. get_context(targets=["src/auth/login.py"]) returns a triage card for the file: documentation, signatures, ownership, hotspot status and fix history, with no source bytes. Pass include=["skeleton"] for the verified outline, or include=["callers"] for who depends on it.
  4. get_risk(targets=["src/auth/login.py"]) shows churn, bug-fix history and structural reach before the agent edits a file many others import. After the edit, get_change_risk() scores the diff itself with no LLM call.
  5. get_why("why is login rate limited at the gateway?") surfaces decision records and git archaeology, so the agent does not undo a deliberate choice.
  6. Read the file. Only now does the agent open source, and only the files it has already narrowed to.
repowise chat answering how a file code health score is calculated, with the agent's activity steps collapsed above the cited answer
repowise chat answering how a file code health score is calculated, with the agent's activity steps collapsed above the cited answer

Every response carries _meta fields that say how complete and how fresh it is. If the index is behind the working tree, the response flags the files it served so the agent knows to read those directly rather than trust a stale summary.

What it saves, measured

In a real agent loop against a bare-agent control (Codex, django/django, n=43), the structured-tools approach cut output tokens 31.6% and reached an answer in 3.8 tool calls on average, fewer than the bare agent needed. The run is described in full on the benchmarks page.

Latest result: agents open up to 99% fewer files with repowise and write up to 51.2% less (Sonnet 4.6), every model tested opened fewer files, and the newest flagship, Opus 5.5, got 48 of 48 answers right (benchmarks).

A second number is easier and answers a different question. Loading one commit's context from repowise takes 35.6x fewer tokens than reading every changed file (pallets/flask, 30 most recent non-merge commits). That measures one payload, not a finished session, so treat 31.6% as the figure for what an agent actually saves.

Both numbers come from one repository and one harness. They show the direction and the scale on a real task, and the method is published so you can run the same comparison on your own repository.

Set it up

Three commands index a repository and serve it to any MCP client. The structural index needs no API key and no LLM spend.

bash
pip install repowise
repowise init        # parses code, mines git history, builds the graph and wiki
repowise mcp         # serves the tools over stdio

repowise init registers the server with Claude Code and installs its hooks. For Codex, run repowise init --codex. For Claude Desktop, Cursor or Cline, add the server to the client's MCP config:

json
{
  "mcpServers": {
    "repowise": {
      "command": "repowise",
      "args": ["mcp"]
    }
  }
}

repowise parses 28 languages to a full AST. Run repowise update after changes to refresh the index. The step-by-step client setup is in setting up the MCP server for Claude Code, Cursor and Cline, and the full tool list is in 8 MCP tools for AI agents.

Pitfalls

  • Treating the index as a replacement for reading. The tools decide which files to read. Before editing a file, the agent still reads it.
  • Leaving the tools unused. Agents sometimes ignore MCP tools they were never told about. Why an MCP server is not being called covers the fixes.
  • Stale answers after big edits. Run repowise update --working-tree after a batch of uncommitted changes, and read the _meta freshness fields.
  • Context file bloat. Keep CLAUDE.md to rules a person must state. Let the index carry facts that change.

FAQ

What is prompt stuffing?
Prompt stuffing is pasting large amounts of source code or documentation into a model's prompt so it can "see everything". It raises cost and latency on every turn, and models use information in the middle of a long input less reliably than information at the ends.
How do I give Claude Code context about a large codebase without pasting files?
Index the repository and connect it as an MCP server, so Claude Code calls tools for architecture, file context, risk and history on demand. With repowise, repowise init indexes the repo and registers the server with Claude Code in one step.
Does a larger context window make this unnecessary?
No. A larger window raises the ceiling on what fits, not the quality of what the model does with it, and cost still scales with the tokens you send. Selecting the right context stays worthwhile at any window size.
Does repowise send my code to a third party?
The structural index (graph, git analysis, deterministic wiki) is built locally with no LLM calls and no API key. Prose generation is optional and uses the provider you configure, including a local model.
How is this different from the indexing built into an editor?
Editor indexes are usually retrieval over code chunks inside one tool. repowise adds the dependency graph, git history, ownership, decisions, health and dead-code analysis, and serves them through MCP so Claude Code, Codex, Cursor and Cline read the same index.