
Tokenscope
io.github.wartzar-bee由 GitHub 账号 wartzar-bee 发布v0.2.3更新于 Oct 25, 2026
Analyze Claude Code session cost & context attribution: output, cached re-reads, cache writes.
概览
本地 MCP 服务器,分析 Claude Code 会话日志,把 token 成本归因到输出、缓存读取、缓存写入和新增输入。
- 可以做什么
- 分析 Claude Code 会话的成本与上下文归因,包括每轮上下文增长、按模型和子代理的花费,可读取本地日志或内置演示会话。将当前会话与随附的离线真实会话参考集对比。生成仅含汇总数字的隐私安全分享摘要,输出为 markdown 或 SVG 成本报告卡片。
- 适用场景
- 当你想让助手解释某次 Claude Code 会话为何昂贵、上下文在哪些轮次被重复发送,或该会话与典型会话相比如何时适用,无需离开终端。
- 运行要求
- 通过 npx 从 npm 包以本地 stdio 进程运行;需要 Node.js,并能访问本地项目目录下的 Claude Code 会话日志。未声明账户、API 密钥、环境变量或网络访问。
安装
在 SourceWeft 中
- 打开 控制台中的 Tokenscope,将其添加到工作区。
- 为需要使用其工具的对话启用该服务。
Desktop only,通过 STDIO。 STDIO 服务会启动本地进程,因此需要 SourceWeft 桌面宿主。
其他 MCP 客户端
参照 仓库 中的启动说明。
README
tokenscope ⏣
See what your AI-coding session actually cost — and what's eating your context. A local, read-only CLI that parses your Claude Code session logs and shows where the money goes: model output vs. context being re-sent every turn (the hidden 60%+ of most bills).
(A real session, default Opus pricing. Your numbers will differ — prices are overridable.)
Why
Agentic coding (Claude Code, etc.) produces surprise bills, and the cause is mundane: as a session grows, the whole context is re-sent every turn, so cost balloons even when the model writes little. Existing dashboards show totals; tokenscope shows the attribution — output vs. cache-read vs. cache-write vs. fresh input, the per-turn context-growth curve, cost by model, subagent spend, and which tools fill your context — with concrete "trim this" insights.
Try it in 10 seconds (no Claude Code logs needed)
Runs on a bundled sample session so you see the full report before pointing it at your own logs — no setup, nothing to configure. (The sample is synthetic, for demonstration.)
Install / run
No install — runs via npx:
Reads ~/.claude/projects/**/*.jsonl. Read-only, local, no network, no telemetry — open the source; nothing leaves your machine.
Local cost gate (pre-push / pre-commit)
--max-total N / --max-delta N make scan exit 1 when the token footprint (or a diff's delta) blows a budget — the same check ci-guardrail runs in CI, but locally, before you push. Wire the absolute budget into a git hook so a runaway prompt/config never leaves your machine:
Under budget it prints the report and exits 0; over budget it prints a BLOCKED: line and exits 1. Without a --max-* flag scan just reports (exit 0), so it's opt-in. --max-delta gates the delta between two directories on disk (scan --diff <baseDir> --max-delta N) — point it at a checked-out base tree when you want a regression gate rather than an absolute cap.
Using the pre-commit framework? Add tokenscope to your .pre-commit-config.yaml — no git-hook scripting:
language: node, zero dependencies. With no args it prints the footprint (exit 0); add --max-total N (or --diff <baseDir> --max-delta N) to fail the commit over budget.
Share your bill (privacy-safe)
--share emits a compact summary built from aggregate numbers only — no file paths, no prompt/response content — so it's safe to paste in public:
- Markdown for Reddit / Discord / a GitHub issue (total, the output/cache-read/cache-write/fresh split with %, peak/avg context, and the headline "X% of spend was re-sent context").
- A self-contained SVG "cost report card" (
--share-svg) — no binary deps; renders inline on GitHub and is trivially shareable. - How you compare — both forms now answer "is my session unusual?" against a shipped, offline reference set of real sessions (e.g. "more cache-efficient than ~80% of measured sessions; median session re-sends 24%"). It's a reference yardstick, not a census — full honest distribution at tokenscope.pages.dev/benchmark.
Prefer not to touch a terminal flag? The same render runs entirely in your browser at the web surface in web/: paste your --json output and it draws the full report + the SVG card locally — nothing is uploaded.
Use it from an AI agent (MCP server)
There's an MCP server that exposes the same engine to AI agents / MCP clients (Claude Desktop, Claude Code, etc.) as tools: analyze_claude_cost, get_cost_benchmark, and tokenscope_share_summary. Add it to your MCP config:
Then ask your agent "use tokenscope to analyze my last Claude Code session." It's the same local, read-only engine — see mcp/README.md.
Pricing
Uses documented default prices (Anthropic cache multipliers: write 1.25×/2×, read 0.1× of input). Verify and override for your exact model/tier via ./.tokenscope.json:
Unknown models are flagged (never silently counted as $0). Token counts are read straight from the logs; cost = those counts × the prices shown.
Part of the wartzar-bee cost toolkit
tokenscope is the measurement engine behind a sibling tool, and one of three open-source cost projects:
- ci-guardrail — a GitHub Action that runs tokenscope in CI to predict a pull request's token-cost delta and comment on the responsible files (report-only, or fail the build past a threshold):
uses: wartzar-bee/ci-guardrail@v1. - enclave — the security-first, self-hosted runtime & sandbox the wartzar-bee agent fleet runs on (Apache-2.0).
If you find tokenscope useful, ci-guardrail is the zero-config way to run it on every PR.
Why this exists — further reading
tokenscope came out of running autonomous agents and watching the bill. The write-ups behind it:
- I put an agent on a timer — overnight it burned 136M tokens doing almost nothing — the runaway-cost postmortem that started this.
- Where your Claude Code bill actually goes — I measured 66 of my own sessions — the empirical breakdown tokenscope automates.
- The Claude Code cost formula: why the same session can cost 10× more tomorrow — the cost mechanics tokenscope surfaces.
- Cost-audit series — reproducible token-cost audits of popular agent frameworks (LangChain, AutoGen, CrewAI, …).
- Catch token-cost regressions in CI before they ship — tokenscope as a GitHub Action cost gate on your PRs.
Status / roadmap
- v0.1: Claude Code session cost + context attribution + insights. 20/20 unit tests on the cost math (
npm test). - Next (evidence-driven): per-tool/-file token attribution; daily/budget alerts; a
--watchlive meter; OpenAI/Codex log support.
MIT. Not affiliated with Anthropic.
来源:README.md,提交 4a931b2
工具
0版本历史
1- v0.2.3最新Sep 16, 2026
评分
已满足适用检查项的 50%
该评分衡量此收录条目的完整程度,依据注册中心条目、端点与公开来源计算得出。它不是对服务器本身的评价,也不表示服务器安全或当前可用:可连通性是带日期的检查结果,评分也不包含安装量或用户评价。
- 近 30 天内可连通25 分
请保持端点能够响应 MCP 握手;SourceWeft 每 30 天探测一次。
- 已声明认证方式10 分
请在注册中心条目中声明凭据请求头,或保持端点能够响应探测,以便判定其认证方式。
- 已列出工具15 分
请让服务器在无凭据时响应 tools/list,或发布可在沙箱中无凭据启动的软件包。
- 描述必需15 分
- 图标10 分
- 文档15 分
- 许可证10 分