Agent 最大成本是它没写出的代码:用代码图谱替代文本搜索
An agent's biggest cost is the code it never writes.
做编码 Agent 的同学必看,这篇把文本搜索的三大坑和代码图谱方案讲透了,还附了跨语言实测成本降幅,赶紧拿去优化你的 Agent 定位逻辑。
An agent's biggest cost is the code it never writes.
代理最大的成本是它从未编写的代码。
Microsoft canceled Claude Code for 5,000 engineers. Uber burned its entire 2026 AI budget in 4 months too.
微软取消了为5000名工程师的Claude Code。Uber也在4个月内烧光了其2026年的全部AI预算。
While both these stories were framed around the bill, none of them asked what the agent was actually doing with the tokens.
虽然这两个故事都围绕账单展开,但没有人问代理实际上在用令牌做什么。
Developers intuitively think it's writing code, and more output just means a higher bill.
开发者直觉上认为它是在写代码,更多的输出只是意味着更高的账单。
But that assumption breaks once you look at what a coding agent actually does in a session.
但一旦你观察编码代理在会话中的实际行为,这个假设就站不住脚了。
An agent starts a task with no map of your codebase. Before it writes a single line, it has to find where the change belongs.
代理开始任务时,对你的代码库没有地图。在写一行代码之前,它必须找到更改应该放在哪里。
Text search is the only tool most agents have for that, and it faces three distinct problems:
文本搜索是大多数代理唯一可用的工具,它面临三个不同的问题:
1) A name can show up at hundreds of locations that don't matter, and the agent has to open and read every one to tell the difference.
1) 一个名字可能出现在数百个无关的位置,代理必须打开并阅读每一个才能区分。
2) Two things can share a name and mean something different, a method with different arguments, a variable shadowing a field.
2) 两个东西可能共享一个名字但含义不同,比如参数不同的方法,或遮蔽字段的变量。
3) Sometimes the code shares no words with what you searched for at all, a class implementing an interface without ever naming it nearby.
3) 有时代码与搜索的词毫无关联,比如一个类实现了接口,但附近从未提及该接口。
The first two cost tokens. The third costs more, i.e., a missed connection that passes every test and ships as a bug somewhere else, later, with no clear link back to what caused it.
前两个问题消耗令牌。第三个问题代价更高,即一个被遗漏的连接通过了所有测试,却作为bug在其他地方出现,且没有明确线索指向其根源。
Instead of solving this smarter search, treat the codebase as a graph instead of text.
与其解决这种更智能的搜索,不如将代码库视为图而不是文本。
Classes, methods, and interfaces become the nodes. The real relationships between them, calls, implements, extends, become the edges.
类、方法和接口成为节点。它们之间的真实关系,如调用、实现、继承,成为边。
Every node and edge carries the exact file and line it lives at.
每个节点和边都携带其所在的确切文件和行号。
An agent working against that graph asks a direct structural question, like which classes implement this interface, and gets back exact locations, not a list of matches to sort through.
基于该图工作的代理可以提出直接的结构性问题,比如哪些类实现了这个接口,并得到确切位置,而不是一堆需要筛选的匹配项。
This is close to what an IDE already does when it offers go to definition or find all references.
这接近IDE在提供“转到定义”或“查找所有引用”时所做的。
The difference is the agent can ask that question itself, mid-task, without a human looking it up first.
区别在于代理可以在任务中途自行提出该问题,无需人类先查找。
The graph doesn't need a compiler to build it either. That matters, because code mid-edit rarely compiles cleanly.
构建图也不需要编译器。这一点很重要,因为编辑中的代码很少能干净地编译。
Sonar Vortex runs this graph inside the agent's own loop.
Sonar Vortex在代理自身的循环中运行这个图。
Sonar tested it on six tasks across four languages, using real merged commits from open-source projects. Each ran ten times per side, and every run had to pass the actual build and tests before it counted.
Sonar在四种语言的六个任务上进行了测试,使用了开源项目中真实的合并提交。每侧运行十次,每次运行必须通过实际构建和测试才算数。
Cost fell in every one of the six tasks, from 5% on a straightforward change up to 36% on the Java interface change.
六个任务中的每一个成本都有所下降,从简单更改的5%到Java接口更改的36%。
The biggest drops came whenever one change had to happen in many places at once. That's exactly when text search is most likely to miss one.
最大的下降发生在需要同时在多个地方进行一项更改时。这正是文本搜索最可能漏掉一个的时候。
You can read the original study this post is based on here: https://fandf.co/4cJCjHl
您可以在此阅读本帖子所依据的原始研究:https://fandf.co/4cJCjHl
The visual below explains regular text search with semantic code navigation, and I worked with the Sonar team to put this together.
下面的可视化解释了带有语义代码导航的常规文本搜索,我与Sonar团队合作制作了此内容。
I also wrote a full breakdown of this. It covers all three ways text search fails, how the graph itself gets built, and the complete results across all six tasks.
我还写了对此的完整分析。它涵盖了文本搜索失败的三种方式、图本身是如何构建的,以及所有六项任务的完整结果。
Read it below.
请在下方阅读。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力