Using code relationship graphs to make AI coding agents smarter and cheaper

Instead of dumping entire files into an AI coding agent, this experiment maps out how functions and modules connect to each other as a graph, then feeds only the relevant pieces. The agent found the right code spots more accurately, and fewer tokens were used — meaning lower cost. It's a practical idea for anyone running AI agents on real-sized codebases.

One of the biggest headaches with AI coding agents on large projects is deciding what code to show them. Feed too much and you waste tokens and confuse the model; feed too little and it misses context. This experiment introduces 'code context graphs': before the agent starts working, you build a map of which functions call which, and which files depend on what. When the agent needs to make a change, it queries this graph and receives only the connected, relevant code snippets instead of whole files.

The reported results show the agent located the correct edit points faster and the amount of code passed in shrank noticeably, cutting token costs. The work is still at the early-experiment stage and the community is discussing how to validate it at scale, but for developers building or fine-tuning AI coding agents, this graph-based context selection is a concrete technique worth trying.

Key points

  • Map code relationships as a graph first, then give the AI only the relevant connected pieces
  • Smaller context per task means lower token usage and reduced API cost
  • Agent accuracy in finding the right edit location improved in tests
  • Especially useful for large codebases where choosing context files is already a challenge
  • Still an early experiment — methodology is being discussed and validated by the community

Quick term guide

AI coding agent
An AI tool that can write, edit, and run code from your instructions.
coding agent
An AI tool that writes or edits code from a person’s instructions.
AI coding agents
AI tools that can help write, edit, or organize software code.
code context graph
A map showing how functions, files, and modules in a codebase are connected to each other.
token costs
Token costs are the fees paid for the text an AI model reads and writes.
token cost
The money or usage spent when sending text to an AI model and getting text back.
Context Files
Files or folders you give to the assistant so it can read and use them in a chat.
context file
A file that gives the AI background information or rules to follow.
Read original