Add token-stats-hook Go module and update README

This commit is contained in:
Knowit
2026-04-18 10:05:00 +08:00
parent e04f79b1d1
commit e29e3ec91c
4 changed files with 143 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
# token-stats-hook
Claude Code Stop hook每次会话结束时自动将 token 用量快照追加到 `~/.claude/token-stats-backup.jsonl`
用 Go 编写,编译为二进制,启动时间 <5ms。
## 安装
```bash
cd hooks/token-stats-hook
go build -o ~/.claude/token-stats-hook-bin .
```
然后在 `~/.claude/settings.json` 中加入:
```json
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "~/.claude/token-stats-hook-bin 2>/dev/null || true"
}
]
}
]
}
}
```
## 备份格式
每行一条 JSON 记录:
```json
{"timestamp":"2026-04-18T02:03:11Z","date":"2026-04-18","user":"charles","input":12861,"output":176499,"cache_write":1352380,"cache_read":15525643,"actual":189360}
```