Claude Code 用久了,有件事一直很别扭:token 到底花在哪了?,需要我自己去查,或者通话 llm 去查。
想知道这次会话烧了多少 Token?手动敲 /context。想看工具调用次数?翻聊天记录数。想让这些数字一直挂在眼前?对不起,Claude Code 原生没这个能力——它的界面是封闭的,你改不了。
Mods 就是 Claude Code 最近放出来的插件能力,专门解决这类「想把信息显示出来」的需求。
一、Mods 到底能做什么
Anthropic Claude Code 团队核心成员 Thariq Shihipar 博客里这样回答了mods 能干嘛:
定制整个 Claude Code harness,执行逻辑和 UI 都能改。比如俄罗斯方块直接显示在终端里;比如每次对话结束自动 fork 一个 sub agent 考你“这个任务真的完成了吗”。因为 fork 会保留 prompt cache,成本非常低。它是“可变软件”的预览,让生成式软件可以被安全地定制。
“可变软件”的预览, 就是ui话定制化,功能监控,官方解释 Mods 是一个插件,一个由 JavaScript / TypeScript 事件处理函数组成的插件。这些函数直接跑在 Claude Code 自己的进程里,可以直接拿到:内部事件的完整上下文,以及界面本身的绘制权。
具体来说,它能做四件事:
- 画出新的界面元素:在转录流旁边的窗格、或输入框上方的条带,放上标签页、按钮、文本框。
- 重绘 Claude Code 自己的界面:替换它已经画好的部分,比如工具调用行、spinner(那个转圈的等待提示)、或者它反问你的对话框。
- 拦截工具调用和请求:比如在向用户提问时先挂起工具调用、回答问题但不让工具真的执行、把请求转发给另一个模型。
- 挂自己的命令和工具:注册一个 /command,立刻跑你的函数,不经过模型那一轮对话——哪怕 Claude 正在忙。
顺带提一句:Mods 官方还提到,同一个 Mod 里的多个 hook 共享它们所在文件里的变量。所以一个 hook 累加的计数,另一个 hook 可以直接拿来渲染。注意是「同一个文件」,跨文件就拿不到了。
二、mod 零 Token获取 数据 是怎么做到的
先看一个官方给的最小示例,它做的事情很简单:在 spinner 后面挂一个「工具调用次数」的计数器。
一个 Mod 的目录结构长这样:
first-mod/
├── .claude-plugin/
│ └── plugin.json
└── hooks/
├── hooks.json
└── register.js
核心逻辑都在 hooks/register.js:
// 两个 hook 共享的计数器
let calls = 0
// Mod 加载时 Claude Code 会调用一次
export function register(on) {
// 每次 Claude 即将调用工具时触发
on('tool.call', async ($, e, next) => {
calls += 1
// 让 Claude Code 重绘界面,新计数才会显示出来
$.ui.invalidate('ui.render')
// 放行,工具照常执行
return next(e)
})
// 每次 Claude Code 绘制 spinner 时触发
on('ui.render', { component: 'Spinner' }, async ($, e, next) => {
// 保留原有 spinner,只在后面追加计数文本
return next({
...e,
props: { ...e.props, suffix: ' · tool calls: ' + calls + '…' },
})
})
}
看完这段就能理解「零 Token」从哪来了:整个链路里没有任何一次模型调用。on('tool.call') 触发时,加 1 是个纯 JavaScript 运算;$.ui.invalidate('ui.render') 只是给界面发一个「重画」信号;next(e) 把事件原样放行。数据既没进模型上下文,也没等模型回话,自然就不产生 Token 消耗。
一个判断标准:只要你的 hook 里不让模型生成东西,它就不烧 Token。反过来说,如果你想在 Mod 里调一次模型去总结当前会话,那该花还是得花。
三、Mods Hook 还是 Settings Hook
最容易搞混的就是这两个概念。名字都带 Hook,但一个是进程内函数,一个是外部命令。
选择其实很直接:你想要「新的显示界面」或「自定义命令」,用 Mods;你想用已有脚本做拦截、阻断、审计,用 Settings Hook。
我的实际感受是这两者更像是互补而非替代。以前想给终端加个实时面板,唯一的办法就是写 hook 脚本往 stdout 打日志,然后接受它和主界面分离、还不好排版;现在 Mods 把「往界面上贴东西」这件事变得合法了。
四、用 Mods UI 监控 Token
回到开头那个让我一直别扭的问题:Token 到底花在哪了?
没有 Mods 的时候,我只有两个办法:
- 手动敲 /context 看一眼——每查一次,节奏全毁。
- 在 CLAUDE.md 里写一堆规则,让 AI 每隔几轮帮我汇报一次——能用,但这是拿 Token 换 Token,纯属左手倒右手。
有了 Mods 之后,Token 消耗可以显示在界面上。
我直接用了现成的 Mods——claude-mods.com 市场里的 context-bar。(也可以自己编写或者让让 Claude 编写)
版本要求:Claude Code 2.1.287 以上。
两条命令就能装上:
claude plugin marketplace add hamzafer/claude-code-mods
claude plugin install context-bar@claude-code-mods
装完之后,Token 分布就常驻在界面上了。
Mods :不是所有「让 AI 帮我做事」的需求都得塞进模型。显示、计数、布局这类事情,本质上是确定性的界面逻辑ui显示,让使用者更加直观的看到想要的数据和进度。mods 可能是后面cc 重要优化使用方向,就像Thariq 说的:
在极限情况下,我们设想的是 artifacts 会成为你进入整个 harness 的界面。你可以像评论一份实时文档一样评论你的工作计划,也许还能看到多个 agent 在做不同的事。为你的 harness 提供一个实时界面,大概就是事情发展的方向。
本文由 AI图鉴自动抓取并翻译,版权归原作者所有;如需查看原文请点下方链接。
Claude Code has spent a long time, and one thing has been very strange: where did Token spend it?
Want to know how much this session burned token? Manual knock/content. How many times do you want to see tool call? I'm sorry, Claude Code didn't have that ability -- it was closed, you couldn't change it.
Mods is the ability of Claude Code's recently released plugins to address such needs as "wanting to show information."
One, Mods, what can we do?
Anthropic Claude Code, a core member of the team, Tharik Shihipar, answers the question of what the Mods can do:
Customize the entire Claude Code Harness, the execution logic and the UI. For example, the Russian cubes are displayed directly in the terminal; for example, each conversation ends automatically fork a sub ant test you "is this job really done?" Because fork would keep prompt cache, very low cost. It is a preview of "variable software" so that the generated software can be safely customized.
The preview of variable software, i.e. customizing ui, functional monitoring, official interpretation of Mods, is a plugin consisting of the JavaScript/TypeScript event processing function. These functions run directly into Claude Code's own process, and can be obtained directly from the complete context of the internal event and the drawing rights of the interface itself.
Specifically, it can do four things:
- Draws new interface elements: in the pane next to the transfer stream, or enter a strip above the frame, with tabs, buttons, text boxes.
- Redraw Claude Code's own interface: replace the parts that it has drawn, such as tool call lines, spinner (the waiter in the circle) or it's a counter-question dialogue box.
- Interception of tool call and requests: for example, to hang up tool call when asking questions to users, to answer questions without allowing the tool to be implemented, and to forward requests to another model.
- Hang up your own commands and tools: register a /command, run your function immediately, without the model conversation -- even Claude is busy.
By the way: Mods official also mentioned that many of the books in the same Mod shared the variables in their files. So one hook cumulative, another hook can be rendered directly. Watch out for "the same document" and cross-document.
Two. Mod Zero Token, how did you get the data?
Let's look at an official example of the simple thing to do: hang a "tool call Number" counter behind a spinner.
The structure of a Mod directory is as follows:
first-mod/
├── .claude-plugin/
│ └── plugin.json
└── hooks/
├── hooks.json
└── register.js
The core logic is in books/register.js:
/ / Two Hook shared counters
let calls = 0
Claude Code calls once when loading
export function register(on) {
/ / Every time Claude is about to call the tool trigger
on('tool.call', async ($, e, next) => {
calls += 1
/ Let Claude Code redraw the interface so the new count will show
$.ui.invalidate('ui.render')
/ Released, tools implemented as usual
return next(e)
})
/ / Every time Claude Code draws spinner Fire!
on('ui.render', { component: 'Spinner' }, async ($, e, next) => {
/ / Retain original spinner, add counting text only after
return next({
...e,
props: { ...e.props, suffix: ' · tool calls: ' + calls + '…' },
})
})
}
After reading this, we can see where "Zero Token" came from: there's no model call in the entire chain. On (`tool.call') trigger, plus 1 is a pure JavaScript operation; $.ui.invalidate (`ui.render') only sends a "redraw" signal to the interface; next(e) lets the event go as it is. The data are neither in the model context nor waiting for the model to answer, which naturally does not produce token consumption.
A test: It doesn't burn token as long as it doesn't make the model. In turn, if you want to put a model in Mod to sum up the current session, then it's a flower.
Three, Mods Hook or Settings Hook?
The two concepts are most easily confused. All the names are with Hook, but one is a process function and one is an external command.
The choice is really straightforward: you want a "new display interface" or a "self-defined command" with Mods; you want to stop, block, audit with an existing script, use Settings Hook.
My actual feeling is that the two are more like complementary than alternative. The only way to add a real-time panel to the terminal is to write a log for the hook script to stdout, and then accept it separate from the main interface, which is not easy to print; now Mods has made it legal to post things on the interface.
Token
Back to the question that kept me going: where did Token spend it?
Without Mods, I have two ways:
- Manual knock/content take a look -- every check, all beats.
- Write a bunch of rules in the CLAUDE.md to have AI report me every few rounds -- it works, but it's token for Token, it's left-handed.
After having Mods, Token consumption can be displayed on the interface.
I'm using the readily available Modes-claude-mods.com market context-bar. (or by Claude)
Version requirement: Claude Code over 2.1.187.
Two orders can be filled:
claude plugin marketplace add hamzafer/claude-code-mods
claude plugin install context-bar@claude-code-mods
After loading, Token's distribution is permanently on the interface.
Mods: Not all of the demands for "let AI do something for me" have to be inserted into the model. Displays, counts, layouts, in essence, are definitive interface logic ui, which allows users to see the desired data and progress more intuitively. Mods may be the key optimized use direction in the back cc, as Tharaiq says:
In the extreme case, what we're thinking is that the artifices will become the interface for you to get into the whole Harness. You can comment on your work plan like a real-time document, and maybe you can see multiple angents doing different things. A real-time interface for your Harness, probably the way things go.
Auto-fetched and translated by AI Atlas. All rights belong to the original author.