All posts
AI-native method

A code graph: the agent asks for structure, not text

3 Aug 20263 min read2 views
A code graph: the agent asks for structure, not text

This note is about giving a coding agent a code graph. A coding agent spends most of its time finding, not writing. "Who calls this function?", "which handler does this route reach?", "what breaks if I change this signature?" Its default tool is grep. Fine in a small repository, but in one with thousands of files a function's name shows up in twenty places: the definition, calls, comments, tests, documents. The agent has to read them all to find the one that matters, and every line it reads costs context.

A code graph instead of text

In SimaCMS the whole repository is indexed in codebase-memory-mcp. It parses the code into a graph: functions, types, packages, calls, imports and routes are its nodes and edges. Instead of "every line containing this word", the agent asks:

  • search_graph: where is this symbol defined?
  • trace_path: who calls it, and whom does it call?
  • get_code_snippet: just this function's body.
  • detect_changes: what does this change reach?

The answers are short and exact. Instead of reading twenty files, the agent gets three nodes and one snippet.

A gate hook

Having a good tool isn't enough; the agent has to use it. Models default to grep and won't reach for a new tool without a push. So I wrote a small hook: until the agent has queried the graph once in a session, text search over source is blocked, and the error tells it to start from the graph.

After the first query grep is free again, because some things really are text: a literal, a translation key, an error message. The goal isn't banning grep; it's making the agent see the structure before the text.

Documents are in the graph too

Some SimaCMS specs are huge; the content module's spec alone is about seventy thousand tokens. Read whole, it eats half the context. But Markdown headings are stored in the graph as Section nodes with line numbers. The agent asks "where is the part about scheduled publishing?", gets the line, and reads only that section.

A simple rule sits beside it: read documents by the section. Table of contents, then the heading, then from that line.

A fresh index

A stale graph is worse than none, because it answers wrong with confidence. A script refreshes the index after changes, and cloud sessions refresh it at start. If something isn't in the graph, the agent should know the index may be behind and go back to the source.

What changed

Sessions got longer-lived, because context fills more slowly. Impact analysis got sharper, because "who calls this" now has a graph answer instead of a guess. And research subagents come back with a few graph queries rather than a wall of text.

If you work with agents on a large repository, I think this is the cheapest improvement you can make: a structural index, and a hook that nudges the agent towards it.

If you are just starting with agents, a code graph is step two. Step one is path-scoped rules and research subagents. The tool I use is codebase-memory-mcp.

Comments

Sign in to leave a comment.

Sign in

Loading…

Related posts

What goes in CLAUDE.md? A real before-and-afterAI-native method

What goes in CLAUDE.md? A real before-and-after

I gave Claude Code the same sentence twice. The first time it made seven business decisions on its own; the second time, with a twenty-line CLAUDE.md, it asked eight questions instead of guessing. This note covers what the file is, where it lives and what is worth writing in it.

24 Nov 20265 min