你的智能体为什么不爱用你的 CLI | Ayush Bhardwaj
原题Your Agent Hates Your CLI - Ayush Bhardwaj, Allos AI
看前速览
智能体反复使用的管道命令,能暴露 CLI 应该直接提供哪些功能。
把重复出现的绕路当作产品反馈
查看智能体是否在同一子命令之后反复用相同的筛选或追加调用。重复的模式比一次管道更能说明问题。
直接提供筛选能力和失败信息
支持范围、精简结果和批量查询,保留有用的报错,并在更新参数的同时维护帮助和技能说明。
回放真实调用,衡量最终结果
把后续动作视为需求线索。用留出的样本测试改动,既观察输出大小,也统计是否拿到了有用结果。
完整摘要
Ayush Bhardwaj 审查了约 2 万次命令调用,观察编码智能体如何反复截取、重排输出,或换参数再试。他据此给自己的会话历史 CLI 加入按范围读取、精简搜索结果和批量查询。演讲还解释了管道为何可能掩盖命令失败,过时的帮助和技能文档为何会让智能体继续使用废弃参数,以及怎样把真实调用回放成回归测试。衡量改进时,要看智能体是否用更少轮次得出更好的答案。
章节
文字稿
简要描述Brief Description
Ayush Bhardwaj 使用真实的代理命令执行记录,展示反复使用管道、重试和解析输出的过程,如何揭示 CLI 应该直接提供哪些功能。Ayush Bhardwaj uses real agent command transcripts to show how repeated pipes, retries and output parsing reveal what a CLI should expose directly.
目录Table of Contents
- 代理调用中隐藏的变通方案The workarounds hiding in agent calls
- 审计 20,000 次调用与误导性的退出代码Auditing 20,000 calls and misleading exit codes
- 聊天历史 CLI 案例研究The chat-history CLI case study
- 更小的切片、更紧凑的结果和批量请求Smaller slices, compact results and batch requests
- 技能和帮助文本就是界面Skills and help text are the interface
- 将对话转录内容转换为回放测试Turn transcripts into replay tests
- 代理模式与衡量有用成果Agent mode and measuring useful outcomes
- 设计接口,然后使用真实跟踪数据进行测试Design the interface, then test against real traces
智能体调用中隐藏的变通方案The workarounds hiding in agent calls
嗨,我是 Ayush。我开发了一个 CLI,让你的智能体能够跨工具复用过去会话中的上下文。在改进它的过程中,我意识到我的智能体为什么暗地里讨厌我的 CLI,以及你可以如何改进自己的工具。 Claude Code 想从文档页面获取一个设置项,于是它下载了整个页面,用 grep 搜索,然后把结果限制在 40 行。Codex 想知道有多少个依赖存在漏洞,于是它请求了整份 JSON 报告,并保留最后 40 行——这份报告把总数放在那里。Cursor 运行了一套测试,发现一个失败项,于是保留了最后 80 行。 这三种截取方式都保留了此处展示的信息。它们都把一个假设写进了代码:答案会出现在输出的哪个位置。这个假设可能掩盖那些构建这些工具的人所遇到的摩擦。单独看一次管道操作,你很难发现什么;但同一条管道一次又一次地出现,可能就说明你的工具缺少了某些东西。Hi, I am Ayush. I built a CLI that lets your agent reuse context from your past session across tools. While improving it, I realized why my agent secretly hated my CLI and what you can do about yours. Claude Code wanted one setting from a docs page. So, it downloaded the whole page, grepped it, and kept the result at 40 lines. Codex wanted to know how many dependencies were vulnerable. It asked the whole JSON report and kept the last 40 lines where this report puts the totals. Cursor ran a test suite, found one failure and kept the last 80 lines. All three cuts kept the information shown here. Each one coded in an assumption about where the answer sits in the output that can hide friction from the people who build these tools. One pipe tells you very little. The same pipe written again and again can point to something your tool is missing.
这些都是我自己对话记录中的真实调用,而 Claude Code 那次是负责审阅这场演讲的代理,它还是没忍住。代理可以绕过阻力,而不提交 bug 报告。它们会围绕你的程序写个小程序。6 月,内容平台 Sanity 发布了他们的 MCP 服务器的总结。上线以来,已有超过 20,000 个代理进行了约 300 万次工具调用,却没有收到一份 bug 报告。于是他们推出了一个反馈工具。代理有了吐槽的地方之后,就开始表达问题所在了。但你不必等到有了反馈工具才开始。反馈早已藏在你的对话记录里。These are real calls from my own transcripts and the Claude Code one was the agent reviewing this talk and it still couldn't help itself. Agents can work around friction without filing a bug report. They write a little program around your program. In June, Sanity, the content platform, wrote up their MCP server. More than 20,000 agents, about 3 million tool calls since launch and not a single bug report. So they shipped a give feedback tool. Once agent had somewhere to complain, they started expressing what's wrong. But you don't need a feedback tool to start. Feedback is already sitting somewhere in your transcripts.
审计 20,000 次调用与具有误导性的退出代码Auditing 20,000 calls and misleading exit codes
我从自己的转录记录中提取了大约 20,000 次代理调用。这个样本中出现了 14 种 CLI 工具:Git、GitHub CLI、cargo、curl、npm、docker 等。四分之一的调用会把输出传给 head、grep、tail、Python、sort 或 wc 等过滤工具。对于以读取为主的命令,这一比例超过一半。git show 和 cargo test 的比例都达到了 62%。 有些执行框架使用管道的频率要低得多。比如 Codex 允许代理为每次调用设置输出上限。而且,在没有启用 pipefail 的 Bash 中,管道不仅会减少噪声:管道会返回最后一条命令的退出码。也就是说,如果你把 cargo test 的输出传给 head,即使测试失败,它也可能报告成功。屏幕上就是一个例子:编译错误,却显示了绿色的通过标记。状态报告有误,但代理仍然能读到错误信息。更糟的是,管道还可能把错误本身截掉。 在 Claude Code 的一个 GitHub 议题中,一位用户报告说,某个脚本抛出了引用错误,并以错误码退出。I extracted about 20,000 agent calls from my same transcripts. 14 CLI appear in that sample. Git, GitHub CLI, cargo, curl, npm, docker, and others. A quarter fed output into filters such as head, grep, tail, Python, sort or wc. For read heavy commands, it's more than half. git show and cargo test were each at 62%. Some harnesses pipe far less. Codex for example lets the agent set an output limit on each call. And pipes don't only cut noise in Bash without pipefail. A pipe then returns the exit code of its last command. So let's say you pipe cargo test into head. It can report success even when the test failed. Here's one example on the screen. A compile error and a green dot. The status was wrong but the agent could still read the error. A pipe can also cut the error itself which is worse. In a Claude Code issue on GitHub, a user reported it a script threw a reference error and exited with an error code.
tail 显示了三行无害内容,Claude 报告称操作成功。错误从未传到模型那里。因此,我的建议是:工具失败时要明确报错。最后几行应简短总结,并提供完整日志的路径。tail showed three harmless lines and Claude reported success. The error never reached the model. So my advice would be to fail loudly when a tool fails. Make the very last lines a short summary and a path to the full log.
聊天历史记录 CLI 案例研究The chat-history CLI case study
我是在折腾自己编写的一个 CLI 时了解到这些的。它是个小工具,可以让编程智能体搜索自己在 Claude Code、Codex、cursor 中的过往会话。它附带一个 skill,让智能体能够自行决定何时以及如何调用它。我研究的对象就是这些智能体。我的十月审计记录了大约一千次调用,这就是我的小型案例研究。这种方法适用于智能体调用的任何工具。只要看看它们调用之后紧接着做了什么就行。 80%。五次里有四次,智能体用我的工具打开旧会话后,输出都会直接传给 grep、sed、head、tail 或 Python。我把这称为“管道税”。管道能把无用内容挡在上下文之外,这很好。所谓的税,就是智能体每次都得手动编写同一条精简输出的命令。每次看起来只是小事,而且大多时候也能奏效,但每多写一次,就又多了一次对答案所在位置的猜测。I learned all this while tinkering with a CLI I built. It's a small tool that lets coding agents search their own past sessions across Claude Code, Codex, cursor. It ships with a skill so the agent can decide when and how to call it. Agents are the users that I'm studying. My October audit recorded about thousand calls and that's my small case study. The method works on anything your agents calls. Just look at what they do right after the call. 80%. Four out of five times an agent opened an old session with my tool. The output went straight into grep, sed, head, tail or Python. I call that the pipe tax. A pipe keeps junk out of context and that's fine. The tax is that the agent writing that same cut down command by hand every single time. It's small each time and it mostly works but each one is another guess about where the answer is.
这个合理的质疑来自我读到的一条 Hacker News 讨论串,主题是如何为代理设计 CLI。评论建议让代理自行决定要读取多少输出。代理完全可以通过 grep 或 head 来处理管道输出。确实可以,而且对于一次性任务,它们也应该这么做。我并不反对这一点。但在这些日常使用的 CLI 中,我发现 83% 的管道用法都符合至少出现过 10 次的模式:相同的 CLI 子命令和管道工具。传入的参数可能不同,但代理反复索要的都是同样的内容。The fair objection comes from a Hacker News thread about designing CLIs for agents that I was reading. The comment says to leave it to the agents to choose how much output to reads. agent can pipe through grep or head perfectly well. Well, they can and for oneoff they should. I'm not against that. But across those everyday CLIs, I found 83% of pipes fell into the patterns seen at least 10 times. The same CLI subcomand and pipe tools. The arguments passed could differ, but the agent keep asking for the same things.
更小的数据片段、紧凑的结果和批量请求Smaller slices, compact results and batch requests
我理解他们截断的输出可能很大。我用我的工具以两种方式打开了一个很长的会话:整个内容有 120 万个字符,而通过管道传给智能体的内容,只包含搜索匹配项附近的部分,大约 3,000 个字符。工具框架确实会限制输出,但大小限制并不能识别哪些消息是相关的。另一方面,选择标记可以让智能体告诉你的工具它想要哪一部分。根据我的经验以及我对自己工具所做的改进,我给你举三个例子。把左栏看作错误报告。先看第一个例子:用 ripgrep 和 sed 进行筛选,就是让我挑选想要的部分。来看我的命令。现在,我的读取命令视图可以只返回匹配项附近的部分、仅返回匹配的消息,或者返回最近的几条消息,并且设有大小限制。第二个例子是用 Python 处理 JSON。I understand the output they're cutting is probably big. I opened one long session with my tool two ways the whole thing 1.2 million characters and that's why the agent pipe just the part around the search match was about 3,000 characters. Harnesses do cap output but a size cap does not identify the relevant messages. Selection flags on the other hand let the agent tell your tool which part it wants. So based on my experience and improvements I made to my own tool, I'll give you three examples. Read the left column as the bug reports. So starting with the first one filtering with ripgrep and sed means let me pick the part I want. So read my command. So my read command view can now return just the part around a match only the matching messages or the last few with a size cap. Second example would be Python over JSON.
所以,对 JSON 使用 Python 意味着要给我更少的字段。于是,搜索功能增加了紧凑模式,每条命中结果占一行,在结果相同的情况下,体积比 JSON 小了约六倍。聊天历史中的 Inspect 命令可以总结一个会话。大约三分之一的 Inspect 调用,是智能体手动遍历多个会话时发起的。正如你在 for 循环中看到的,这意味着我可以一次询问多个会话中的多个问题,现在只需调用一次。正如你所见,我的路线图就在命令周围的指令中,我也得从中吸取经验。云成本工具 Infracost 在五月也记录了同样的发现。他们的智能体会基于 JSON 转储编写 jq 和 Python 处理流程。于是,他们把筛选功能移到了 CLI 中,并添加了紧凑格式和一项用于指导智能体的技能。在他们的测试中,未做任何这些改进的普通 Claude 六道难题一道也没答对;使用这些改进后,六道全对,而且整个基准测试的成本也下降了 41%。So Python over JSON means give me fewer fields. So search got a compact mode one line per hit about six times smaller than JSON with the same results. Inspect command in chat history summarizes a session. About the third of inspect calls came from agent looping over several sessions by hand. As you can see with the for loop that means let me ask several about several at once and now it's one call. As you can see my road map was in the commands around my command and I had to learn from that. Infracost the cloud cost tool wrote up the same discovery in May. Their agents were writing jq and Python pipelines over JSON dumps. So they moved the filtering into the CLI and added a compact format and a skill to teach it. Plain Claude with none of that got zero of six hard questions right in their testing and with it six of six and cost across the benchmark also fell 41%.
在 Hacker News 上,写那篇帖子的作者说,读那些智能体日志就像是在观看可用性测试,我对此深有同感。On Hacker News, the person who wrote that post said that reading those agent logs felt like watching usability tests and I couldn't agree more.
技能和帮助文本就是接口Skills and help text are the interface
这部分听起来可能很简单、显而易见。一旦把它加入 CLI,就通过 skill 文件或帮助标志告诉你的 agent 它的存在,但 agent 会照字面意思执行。我做了一些改动,让我的 deep 标志变成了空操作。我仍然在 skill 里告诉 agent 要使用它,想看看它们会怎么做。它们会不会发现?95 次搜索中,95 次都还是传入了它。之后,有两个会话加载了改写后的 skill,其中不再要求它们使用这个标志。没有一次搜索传入 D 标志,而且两个会话在第一次读取时都尝试了新标志。所以,你的 skill.md、帮助文本和错误消息就是你的界面。这也意味着过时的文档就是 bug,因为你的 agent 会一直照着它们做。新标志提供给 agent 后,几乎每次搜索都使用了精简输出,而且几乎每次都使用了新标志。This part might sound trivial and obvious. Once it's in the CLI, teach your agent it's there with the skill file or the help flag, but the agents take the teaching literally. I made some changes that turned my deep flag into a no op. I left the skill telling the agents to use it to see what they do. Would they find out or not? 95 searches out of 95 still passed it. After that, two sessions loaded a rewritten skill and that no longer told them to use it. None of the searches passed D flag and both tried new flags on their first read. So your skill.md, your help text, your error messages are the interface. That also means stale docs are bugs because your agent will keep following them. Once the new flags reached agents, nearly every search used compact output and nearly every reduced the new flags.
管道输送率从 82% 降至约四分之一。The piping rate fell from 82% to about a quarter.
将转录内容转换为回放测试Turn transcripts into replay tests
你可以轻松地为你的工具获取真实的评测数据和测试用例。如果你每天都在用它,这些数据已经在你的对话记录里了。智能体实际运行过的命令就是测试用例,而它紧接着采取的行动则可以作为粗略的答案参考。用旧版本和新版本分别重放这些命令。如果你调整了排序,就检查智能体随后使用的结果是否仍然排在最前面。如果你更改了输出格式,就检查有没有内容丢失。如果你更改了默认设置,就看看哪些命令的行为因此发生了变化。而当智能体用不同的参数重新运行某个调用时,你很可能就找到了一个失败案例。答案参考只是对智能体意图的粗略猜测。留出几个案例,用它们来评估你的修复效果。所以,在分析了 20,000 次调用之后,如果让我从头再来一次,我会先构建这个。You can get real evals and test cases for your tool easily. If you use it every day, they are already in your transcripts. The commands agent really ran are the test cases and what agent did immediately next is a rough answer key. replay those commands through your old build and your new one. If you change a ranking, check that result the agent went on to use still comes out on the top or not. If you change an output format, check that nothing went missing. If you change a default, see which commands now behave differently. And when an agent reruns a call with different arguments, you have probably found a failing case. The answer key is only a rough guess at what the agent wanted. keep a few cases back and use those to score your fix. So after analyzing 20,000 invocation, if I were to start all over again, this is what I would build first.
在测量的流水线中,55% 下游只用了 head、tail 或 sed,这表明大家需要更小的数据片段。因此,优先提供范围和数量限制等功能,别先忙着做花哨的东西。只有当智能体会主动使用某个标志时,它才有用。GitHub CLI 的 list 命令有一个 limit 标志,智能体有 81% 的时间都用了它。 反例是 cargo:cargo test 的安静模式会为每个测试打印一个字符,而不是一整行,同时仍会报告失败。智能体大约每运行 10 次 cargo 命令才会传入这个标志一次,反而选择了管道处理。所以,要在帮助文本和技能中教会智能体使用这个标志;并且让大量输出默认少打印一些,同时说明如何获取更多内容。例如,显示“300 条中显示 20 条”,并添加 offset 标志;要查看下一页,就传入 20。55% of the measured pipelines used only head, tail or sed downstream that points to demand for smaller slices. So ship ranges and limits before anything fancy. A flag only helps if agents reach for it. GitHub CLI's list command have a limit flag. An agent used it 81% of the time. The counter example would be cargo for cargo test quiet mode prints a character per test instead of a line and still reports failures. Agent passed it on about one cargo run in 10 and piped instead. So teach the flag. So teach the flag in your help text and your skill and make big outputs print less by default and say how to get more. for example, and with something like showing 20 of 300, add offset flag 20 for the next page.
代理模式与衡量有效成果Agent mode and measuring useful outcomes
一次性推出其中几项功能的方法之一,是提供代理模式。你的 CLI 会察觉到有代理在调用它,并采用不同的输出方式。Claude Code 这类代理会在其 shell 中设置一个环境变量,这样你的工具就能识别它们。DuckDB 在终端中推出了这种模式。用户看到的是排版精美的表格,而代理看到的是纯文本表格,其中会说明省略了多少行以及如何获取这些行。对于特别大的查询,它会提前停止,并明确说明这一点。错误则会以 JSON 格式返回,因为代理更容易读取。它和我们刚才讨论的是同一份清单,而且让模型读取的输出减少了 59%。不过,节省的 token 只占模型读取的全部 token 的约 0.5%。所以成本和运行时间都没有改善。如果你还记得,Infracost 的代理开始能给出正确答案了。而 DuckDB 本来就能给出正确答案。所以,要衡量交互轮数和正确答案,而不只是 token 数。One way to ship several of these at once is an agent mode. Your CLI notices an agent is calling it and prints differently. Agents like Claude Code set an environment variable in their shell so your tool can spot them. DuckDB shipped one at a terminal. You get a nicely drawn table, but an agent gets a plain one that says how many rows were left out and how to get them. A huge query stops early and says so. And errors come back as JSON because agents read them better. Is the same list as we have been just through and it cut the output model read by 59%. However, the tokens saved were about 0.5% of all the tokens the model read. So cost and runtime didn't improve. If you remember, Infracost agent started getting answers right. DuckDB’s answers were already there. So measure turns and right answers, not just tokens.
设计界面,然后使用真实跟踪数据进行测试Design the interface, then test against real traces
你可能会想:“我让我的智能体把这些都做了。”但它们现在还没那么擅长,至少还做不到一次性搞定。问题出在设计上。让智能体为智能体构建工具,它很可能会先设想一个人,然后再把智能体硬加进去。它会照搬自己见过的最常见模式,而其中大多数都很糟糕。一项对官方 MCP 服务器的研究发现,大多数服务器只是简单封装了现有 API。另一项对真实 MCP 服务器工具描述的检查则发现,97% 的描述都存在问题。文档也有同样的缺陷。苏黎世联邦理工学院的研究人员测试了由智能体编写的上下文文件,发现这些文件不但无助于任务成功,运行成本还更高,表现也不如人编写的文件。智能体已经很擅长这场演讲另一半所讲的事情了。给它一份记录稿和一个可以运行的测试,它就能找出问题所在,并改进工具。You might be thinking, I'll ask my agent to do all of this. They are not that good at this yet. At least not one-shotting it. The design is where it goes wrong. Ask an agent to build a tool for agent. It probably pictures a person and then bolts the agent on. It copies most common patterns it has seen and most of those are poor. A study of official MCP servers found most just rep an existing API. Another check tool descriptions from real MCP servers and flagged issues in 97% of them. Docs have the same gap. Researchers at ETH Zurich tested context file written by agents. They didn't help tasks succeed cost more to run and did worse than files written by people. Agents are already good at other half of this talk. Give one transcript and a test it can run and it can find what's wrong and improve the tool.
有时会超出人们手动编写的内容,Anthropic 也在其内部工具中报告过这种情况。所以,你来决定工具应该是什么样,再让智能体对照实际发生的情况进行检查。你已经看过这些例子的证据了,所以我就不逐一读了。花点时间消化一下,或者截个图。想找到你自己的清单,先备份好对话记录,因为 Claude Code 可能会在 30 天后删除不活跃的记录。然后,搜索工具名称后面跟着竖线,以及其他类似的模式。如果你发布了 MCP 服务器,留意智能体是否紧接着又调用了你的工具,而且传入了不同的参数。如果你只从这场演讲中记住一件事,那就是:如果你在为智能体构建工具,智能体的对话记录就是你可能从未查看过的产品分析数据。变通方案就是错误报告。Sometimes past what people wrote by hand and Anthropic reported that on its own internal tools as well. So you decide what the tool should be and let the agent check it against what really happened. You have seen the evidence for each of these. So, I won't read them. Take a moment to absorb these or screenshot. To find your own list, back up your transcripts first because Claude Code can delete inactive ones after 30 days. Then, search for them for a tool's name followed by a pipe and other similar patterns. If you ship an MCP server, look for agent calling your tool again straight away with different arguments. If you remember one thing from this talk, if you are building a tool for your agent, your agent transcript are the product analytics you probably aren't reading. The workaround is the bug report.
所以,去读读你的转录稿吧。如果需要帮助,可以查看 Cargo 或 GitHub 上的聊天记录,它们也能帮你完成这件事。谢谢。So go read your transcripts and if you need any help doing that, check out chat history on Cargo or GitHub. It can do this for you as well. Thank you.