Skip to content
153.7k

BlogConcepts

What is codebase context for AI coding agents?

What is codebase context for AI coding agents?

Codebase context is the information an AI coding agent has about your repository when it writes or reviews code. It covers the structure of the code, the conventions your team follows and the reasons behind past decisions. Better context means fewer guesses about where code lives and what a change will break.

The term sits inside a bigger idea that Anthropic calls context engineering: choosing which tokens reach the model on each turn. Codebase context is the part of that problem that is about your code.

What does codebase context include?

Codebase context has three layers: structure, conventions and history. An agent needs all three to make a change that fits the code around it.

LayerWhat it answersWhere it usually lives
StructureWhat calls this function? What imports this module? What breaks if this type changes?The code itself, read through a parser or a graph
ConventionsHow does this team name things, handle errors, write tests?Instruction files, linters, existing code
HistoryWhy is this check here? Which decision led to this design?Commit messages, pull requests, design docs, code comments

Structure is the layer agents get wrong most often, because it is spread across many files. Conventions are easier, because a team can write them down once.

Is codebase context the same as the context window?

No. The context window is the fixed amount of text a model can read in one request. Codebase context is the subset of your repository that ends up there, plus anything the agent can fetch on demand through tools. Good context engineering picks the smallest set that still answers the task.

Anthropic's engineering team describes context as "a finite resource with diminishing marginal returns" in the same post on context engineering. Loading more files is not the same as giving the agent more context. Past a point it is noise.

How do AI coding agents get codebase context?

Agents get codebase context from four sources: file reads, text search, instruction files and tools. Each one answers a different kind of question.

  1. File reads. The agent opens a file and reads it. This is precise for one file and expensive across many.
  2. Text search. The agent runs grep or a similar search for a name or a string. This is fast for exact matches and blind to relationships. We covered the cost of this pattern in why AI agents grep.
  3. Instruction files. Claude Code loads CLAUDE.md at the start of every session and can read AGENTS.md in its place, according to the Claude Code memory docs. Other agents read AGENTS.md. These files carry conventions well and go stale when they try to describe structure, which is the argument in CLAUDE.md best practices.
  4. Tools over MCP. The agent calls a server through the Model Context Protocol and gets back an answer instead of a pile of files. See the MCP server glossary entry for the short version.

Sourcegraph's engineers describe the same mix from the retrieval side in lessons from building AI coding assistants: keyword, embedding and graph retrievers each surface code the others miss.

Why does an agent miss callers in other files?

Text search finds names, not relationships. A call through an alias, an interface or a re-export uses a different string, so grep never returns it. Unless the agent reads the right file by chance, the caller stays outside its context and the change ships without it.

This is the structure layer failing. The agent's reasoning can be correct for every line it read and still be wrong about the system, because the line it needed was never in its context. Monorepos make it worse, as we covered in monorepo context for AI coding agents.

What does good codebase context look like?

Good codebase context is small, current and structural. The agent asks a question and gets the exact callers, imports or paths that answer it, with file and line, and nothing else.

A code graph is one way to get there. Functions, classes and files become nodes. Calls, imports and inheritance become edges. Instead of reading ten files to find who calls charge_card, the agent asks for the callers and gets a list. Our explainer on what a code knowledge graph is goes deeper on how the map is built.

Three properties decide whether the context helps:

  • Current. A map built from last month's code points the agent at functions that moved. The context has to rebuild when the code changes.
  • Sourced. Every answer should carry the file and line it came from, so a person can check it.
  • Shared. If Claude Code, Cursor and a reviewer each build their own picture of the code, they disagree. One map that every agent and every teammate queries keeps answers consistent.

Where does Graphify fit?

Graphify gives coding agents and people shared codebase context, built on a code graph of your repository. The Graphify CLI is open source. It parses your code locally with tree-sitter and serves the graph to agents over MCP or the command line.

bash
uv tool install --python 3.12 graphifyy
graphify update .

graphify update builds the code graph without calling a model and writes it to graphify-out/graph.json. From there, an agent can ask structural questions instead of reading files. The Claude Code integration page shows how to connect it, and the install guide covers every option.

Use Graphify in your agent

Add Graphify to ChatGPT, Claude, Cursor and more in one click.

Try Graphify now.

Try for free