LaoYueHanNi/dsh-token-usage ↗★ 0
dsh-token-usage
dsh local plugin: persist per-request model token usage (live hook + manual history sync command)
安装
$
npx -p @deepseek-ai/dsh dsh plugin --profile web add github:LaoYueHanNi/dsh-token-usage说明文档
阅读完整 README ↗dsh-token-usage
dsh 本地插件:按请求粒度持久化模型 token 用量,写入按天分片的 JSONL 文件。
仓库:
功能
- 实时 hook:每次模型请求成功后(
assistant/message会话事件)追加一行,包含请求 id、模型名、四项 token 用量、时间、会话 id。 - 首次启动自动同步一次:补齐插件安装前已发生的历史请求(
state.json标记判定,之后永不自动重复;崩溃重试由请求 id 去重保证幂等)。 - 手动再同步命令:
/token-usage-sync(Web 命令面板)随时手动补写,以请求 id 去重,与实时数据不重复。
安装与移除
从 GitHub 安装(推荐)
dsh plugin --profile web add github:LaoYueHanNi/dsh-token-usage
# 或指定仓库 URL:
# dsh plugin --profile web add git+https://github.com/LaoYueHanNi/dsh-token-usage.git
# dsh plugin --profile web add git+ssh://git@github.com/LaoYueHanNi/dsh-token-usage.git
从本地目录安装(开发调试用)
dsh plugin --profile web add link:
# 示例:dsh plugin --profile web add link:D:/plugins/dsh-token-usage
包声明了 dsh.bundle,add 会自动把插件加入该 profile 的层栈,无需手动编辑任何配置文件。首次启动会自动执行一次历史补齐(终端打印 [token-usage] first-run sync: N added, M skipped),之后纯实时记录。
更新
dsh plugin --profile web update dsh-token-usage
移除
dsh plugin --profile web remove dsh-token-usage
自动从层栈移除并停止加载。数据文件($DSH_HOME/token-usage/)会保留,需要时手动删除。
临时挂载(覆盖层,不动 profile)
dsh web --patch /cordis.yml
# 示例:dsh web --patch D:/plugins/dsh-token-usage/cordis.yml
仅当次启动生效。cordis.yml 指向 built 产物 lib/index.js。
Windows 上运行 built 安装版 dsh 时,临时覆盖层的插件条目必须用
file:///URL 形式(仓库内cordis.yml已写好);源码运行(tsx)两种形式都接受。bundle 安装方式无此问题(按包名解析)。
配置
插件条目可带 config:(bundle 安装时写在 profile 的 cordis.patch.yml 对应条目下,临时挂载时写在 cordis.yml):
- insert:
- id: token-usage
name: 'dsh-token-usage'
config:
path: 'C:/data/token-usage' # 数据目录;缺省 $DSH_HOME/token-usage($DSH_HOME 未设时为 ~/.dsh/token-usage)
未知配置键在加载时直接报错。
数据
目录下每个本地日期一个文件:usage-YYYY-MM-DD.jsonl,每行一条成功请求:
{
"requestId": "",
"time": 1730000000000,
"sessionId": "…",
"model": "deepseek-chat",
"usage": {
"inputTokens": 100,
"outputTokens": 50,
"cacheReadTokens": 900,
"cacheWriteTokens": 10
}
}