Claude Code 代码模式将工具调用从 16 个压缩到 3 个

原标题:Claude Code Mode Internals
62AI 研判分

作者通过抓取本机 API 请求发现,Claude Code 新增的 Code Mode 把默认工具列表从 16 项压缩为 REPL、Poll、Advisor 三项,由智能体编写程序而非逐条调用工具,从而降低上下文与调用开销。文章拆解了该模式的运行机制,并讨论如何将同一思路移植到自建 CLI 与智能体技能中。

报道正文

本文目录9 个章节

Image 1: Muthukumaran Navaneethakrishnan

6 min read

1 day ago

What exactly happens when Code Mode runs? And Can we adapt the same concept for your CLIs and agent skills?

The tool list keeps growing

If you look at the new Code Mode in Claude Code, you see that by default the tool list will always grow.

The default is a handful of tools. You try to execute things, and then you add your skills, and then your MCP servers, and it keeps on growing. So they introduced Code Mode.

What exactly happens when Code Mode runs? Let’s see.

What I looked at

I looked at the API requests that were made, and the tool list sent in each one. I could see them on my machine.

In normal mode, in my session, I get 16 entries. In Code Mode it is just three: REPL, Poll and advisor. The advisor is there in both (you know advisor usually a bigger model like fable will be there..)

Press enter or click to view image in full size

Image 2

How Code Mode works

The REPL tool carries the descriptions of the tools. Your core built-in tools are there as type signatures, as part of the REPL description.

But the description does not have the exact signature of every tool. For the rest there is a method called searchTools. It pulls the tool you need, and then the model writes the code for it.

Press enter or click to view image in full size

Image 3

All of this happens in a REPL. If you are into Python or Clojure, or you know what a REPL is, you know it keeps running. Here it keeps running inside that Claude Code session, and the state stays there until the session restarts.

With a normal tool call, you need to maintain the state separately and keep track of where you are. The REPL keeps the state between calls , so you do not have to.

So I took it as inspiration

I have many CLIs and many agent skills. My CLIs have 20 to 30 different ways you can act on. So I thought: adapt the same concept.

Make CLI not just expose what it does. Make it composable.

What if your CLI could do that? Take aos, a CLI that can do a lot on a system. Move a window, move the mouse, add a bubble, switch something off, open a tab, automate something. There are more than 60 operations.

Now say this to an agent, in normal mode or in REPL mode. It has to call the CLI every single time.

First aos window move. Then a call to change the brightness. Then another window move. Then aos to open a tab. Each one is a process invocation: you start the process, you get the answer, and then you do the next one.

So invoking a CLI is a cost.

Press enter or click to view image in full size

Image 4

CLI CodeMode

So I thought: if we could have the same concept that Code Mode has, it might be better. I built something called CLI CodeMode.

It is a library. You register all your verbs with it, with their descriptions, so the agent can read what each one does. It also gives you JSON output.

Get Muthukumaran Navaneethakrishnan’s stories in your inbox

Join Medium for free to get updates from this writer.

Remember me for faster sign in

It also adds one more thing to your CLI, called code. Under code you have describe, and you have skill.

Press enter or click to view image in full size

Image 5

It also has a small JavaScript runtime. It is not Bun or Node.js. It runs a JavaScript subset with a small AST. You can import a file and use a regular set of JavaScript and the conditions you need, and nothing more. (I thought of using Clojure, but no one seemed interested, so I changed it to JavaScript. Sorry, TypeScript.)

Then you update the skill for it.

Think about CLIs that have a lot of commands. We don’t want the agent to keep starting a process for every one of them. Rather, the agent composes one JavaScript program, and the CLI validates it before it runs, with code check, and does the rest.

The two CLIs I tried it on

ctx-optimize builds a knowledge graph of a codebase(its kind of one of the fastest, despite not many uses), so a coding agent can answer from it instead of grepping files. You ask it to find a symbol, show a card for it, or plan a change. One question usually takes several calls. https://github.com/muthuishere/ctx-optimize

aos is CLI over the machine: windows, mouse and keyboard, screenshots, files, shell, packages,bubbles etc. Agents drive the machine with it the same way you do. https://github.com/muthuishere/aos

Both are mine, both are released, and both now have a code command.

What I measured

Take one question to ctx-optimize: search for a symbol, open a card for each of the top five hits, then plan the change. That is seven calls. Here is that work done three ways.

Normal: one tool call per command. 3 to 7 trips to the model, 7 processes, 28,273 bytes back.

Press enter or click to view image in full size

Image 6

Claude Code’s Code Mode: one script. 1 trip, still 7 processes. The raw output stays in the script.

Press enter or click to view image in full size

Image 7

With CLI CodeMode: one code run. 1 trip, 1 process, 169 bytes back, and less CPU (137 ms against 164 to 167 ms).

Press enter or click to view image in full size

Image 8

On aos, take a machine check: list the windows, the top processes, the disks, the battery, the running apps and the displays. Six read-only commands, four times each, so 24 calls. Here is that work done three ways:

Press enter or click to view image in full size

Image 9

  • Claude Code Code Mode: 24 processes, 217 to 1,644 ms, 1,228 to 1,493 ms CPU.
  • With CLI CodeMode: 1 process, 217 ms, 1,012 ms CPU.

Try it

I tried to keep the library small. It is available in Go, Rust, Python and TypeScript. Go is the main one. The others are experimental and run the same engine compiled to WebAssembly.

Code Mode removes the trips to the model. It does not remove the process, the parsing and the raw output of every command. That cost sits in the CLI, so the CLI is where to fix it.

If your CLI has more than a handful of commands, an agent pays that cost on every call. Add code, describe your operations once, and let the agent write one program.

Try it on your own CLI, and if it is not worth it, tell me where it breaks.