Cost Tracker — Token 使用量與花費追蹤
SkillAI & modelsTracks and calculates token usage and cost (USD) for the current session. Produces before-and-after comparison reports to help users understand the actual benefits of workspace optimization.
Available today. Use it from your connected AI after setup.
No other account needed.
Connect ahel once, and every AI you use reads what you have installed.
Then ask your AI: use the Cost Tracker — Token 使用量與花費追蹤 skill
What this skill tells your AI
The instructions your AI receives, as published by zeuikli/claude-code-workspace in .claude/skills/cost-tracker/SKILL.md and read by ahel’s review.
定價表(2026-05,官方 Opus 4.8 發布後)
| 模型 | Input ($/1M tokens) | Output ($/1M tokens) | Cache Read ($/1M) | Cache Write ($/1M) |
|---|---|---|---|---|
| Opus 4.8 | $5.00 | $25.00 | $0.50 | $6.25 |
| Sonnet 4.6 | $3.00 | $15.00 | $0.30 | $3.75 |
| Haiku 4.5 | $1.00 | $5.00 | $0.10 | $1.25 |
來源:Models overview、Pricing。 Cache Read = 0.1× input、Cache Write(5m) = 1.25× input。Opus 4.8 fast mode 為 premium 定價,不在此表。
使用方式
在 session 中執行 /cost-tracker,系統將:
-
統計當前 session 的 Token 使用量
- 分別計算 input / output / cache tokens
- 按模型分層統計(Haiku / Sonnet / Opus)
-
計算花費(USD)
- 套用上方定價表
- 區分主對話 vs Sub Agent 花費
-
產出前後對比
- 優化前(假設全部使用 Opus)的預估花費
- 優化後(Advisor 模式分層)的實際花費
- 差異百分比
計算公式
花費 = (input_tokens × input_price / 1M)
+ (output_tokens × output_price / 1M)
+ (cache_read_tokens × cache_read_price / 1M)
+ (cache_write_tokens × cache_write_price / 1M)
分層估算模型
依據 CLAUDE.md 的 Advisor 模式,各角色的 Token 分配比例:
| 角色 | 模型 | 預估 Token 佔比 |
|---|---|---|
| 搜尋探索 | Haiku | ~30% |
| 程式碼實作 + 測試 | Sonnet | ~55% |
| 架構審查 + 顧問 | Opus | ~15% |
輸出格式
### Session Cost Report
| 指標 | 數值 |
|------|------|
| Total Input Tokens | xxx |
| Total Output Tokens | xxx |
| Cache Read Tokens | xxx |
| Cache Write Tokens | xxx |
| **Total Cost (USD)** | $x.xx |
#### 模型分層花費
| 模型 | Tokens | Cost (USD) | 佔比 |
|------|--------|-----------|------|
| Haiku | xxx | $x.xx | xx% |
| Sonnet | xxx | $x.xx | xx% |
| Opus | xxx | $x.xx | xx% |
#### 優化效益
| 指標 | 全 Opus | Advisor 分層 | 節省 |
|------|---------|-------------|------|
| 預估花費 | $x.xx | $x.xx | -xx% |
Agent SDK 整合範例(TypeScript)
import { query } from "@anthropic-ai/claude-agent-sdk";
let sessionCost = 0;
const modelBreakdown: Record<string, { tokens: number; cost: number }> = {};
for await (const message of query({ prompt: "your task" })) {
if (message.type === "result") {
sessionCost += message.total_cost_usd;
for (const [model, usage] of Object.entries(message.modelUsage)) {
if (!modelBreakdown[model]) modelBreakdown[model] = { tokens: 0, cost: 0 };
modelBreakdown[model].tokens += usage.inputTokens + usage.outputTokens;
modelBreakdown[model].cost += usage.costUSD;
}
}
}
console.log(`Session total: $${sessionCost.toFixed(4)}`);
for (const [model, data] of Object.entries(modelBreakdown)) {
console.log(` ${model}: ${data.tokens} tokens, $${data.cost.toFixed(4)}`);
}
Gotcha
- Claude Code 的
/usage是累積值:它顯示從 session 開始到當下的總量,不是「本次操作」的增量。 - Cache write tokens 比 input 貴:cache write = 1.25× input price,計算時不要誤用 input 單價。
- 「優化前」是假設值:以全 Opus 計算的對比數字是估算,實際上你可能原本也不是全 Opus。
- Sub Agent 費用需個別計算:子 agent 的 token 不一定計入主對話的
/usage,需確認 SDK 版本行為。 - 定價表可能已更新:本 skill 的定價以 2026-04 為準,若超過 3 個月請重新確認官方定價。
Signals
- GitHub stars
- 156
- Forks
- 38
- Last commit
- Sep 2026
Advanced
- Catalog kind
- skill
- Gateway key
cost-tracker-zeuikli- Source
- github.com/zeuikli/claude-code-workspace