token-dashboard-claude-analytics
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseToken Dashboard — Claude Code Analytics
Token Dashboard — Claude Code 分析工具
Skill by ara.so — Daily 2026 Skills collection.
Token Dashboard reads the JSONL transcripts Claude Code writes to and turns them into per-prompt cost analytics, tool/file heatmaps, cache analytics, project comparisons, and a rule-based tips engine. Everything runs locally — no data leaves your machine.
~/.claude/projects/由ara.so开发的Skill——属于Daily 2026 Skills合集。
Token Dashboard 读取Claude Code写入的JSONL会话记录,将其转化为按提示拆分的成本分析、工具/文件热力图、缓存分析、项目对比以及基于规则的提示引擎。所有操作均在本地运行——数据不会离开你的设备。
~/.claude/projects/Installation
安装
bash
git clone https://github.com/nateherkai/token-dashboard.git
cd token-dashboard
python3 cli.py dashboardNo . No Node.js. No build step. Requires Python 3.8+.
pip installWindows:
bash
git clone https://github.com/nateherkai/token-dashboard.git
cd token-dashboard
py -3 cli.py dashboardbash
git clone https://github.com/nateherkai/token-dashboard.git
cd token-dashboard
python3 cli.py dashboard无需,无需Node.js,无需构建步骤。要求Python 3.8+。
pip installWindows系统:
bash
git clone https://github.com/nateherkai/token-dashboard.git
cd token-dashboard
py -3 cli.py dashboardKey CLI Commands
核心CLI命令
bash
undefinedbash
undefinedStart the full dashboard UI at http://127.0.0.1:8080
python3 cli.py dashboard
python3 cli.py dashboard
Populate/refresh the SQLite cache, then exit
填充/刷新SQLite缓存,然后退出
python3 cli.py scan
python3 cli.py scan
Print today's totals in the terminal
在终端打印今日使用总计
python3 cli.py today
python3 cli.py today
Print all-time totals in the terminal
在终端打印全时段使用总计
python3 cli.py stats
python3 cli.py stats
Show active optimization tips in terminal
在终端显示当前优化提示
python3 cli.py tips
python3 cli.py tips
Dashboard with options
带选项启动仪表盘
python3 cli.py dashboard --no-open # don't auto-open browser
python3 cli.py dashboard --no-scan # skip initial scan, use cached DB only
python3 cli.py dashboard --projects-dir /path/to/projects --db /path/to/cache.db
undefinedpython3 cli.py dashboard --no-open # 不自动打开浏览器
python3 cli.py dashboard --no-scan # 跳过初始扫描,仅使用缓存数据库
python3 cli.py dashboard --projects-dir /path/to/projects --db /path/to/cache.db
undefinedConfiguration
配置
Environment Variables
环境变量
bash
undefinedbash
undefinedChange port (default: 8080)
修改端口(默认:8080)
PORT=9000 python3 cli.py dashboard
PORT=9000 python3 cli.py dashboard
Change bind address (WARNING: keep 127.0.0.1 — 0.0.0.0 exposes data on network)
修改绑定地址(警告:保持为127.0.0.1——0.0.0.0会在网络上暴露数据)
HOST=127.0.0.1 python3 cli.py dashboard
HOST=127.0.0.1 python3 cli.py dashboard
Custom projects directory
自定义项目目录
CLAUDE_PROJECTS_DIR=/custom/path python3 cli.py dashboard
CLAUDE_PROJECTS_DIR=/custom/path python3 cli.py dashboard
Custom SQLite cache location
自定义SQLite缓存位置
TOKEN_DASHBOARD_DB=/custom/path/cache.db python3 cli.py dashboard
undefinedTOKEN_DASHBOARD_DB=/custom/path/cache.db python3 cli.py dashboard
undefinedPricing Configuration
定价配置
Edit directly to update model prices or add plans:
pricing.jsonjson
{
"models": {
"claude-opus-4-5": {
"input": 15.00,
"output": 75.00,
"cache_write": 18.75,
"cache_read": 1.50
}
},
"plans": {
"api": { "label": "API", "multiplier": 1.0 },
"pro": { "label": "Pro ($20/mo)", "multiplier": 0.0 },
"max": { "label": "Max ($100/mo)", "multiplier": 0.0 }
}
}直接编辑来更新模型价格或添加套餐:
pricing.jsonjson
{
"models": {
"claude-opus-4-5": {
"input": 15.00,
"output": 75.00,
"cache_write": 18.75,
"cache_read": 1.50
}
},
"plans": {
"api": { "label": "API", "multiplier": 1.0 },
"pro": { "label": "Pro ($20/mo)", "multiplier": 0.0 },
"max": { "label": "Max ($100/mo)", "multiplier": 0.0 }
}
}Data Sources
数据源
Claude Code writes session JSONL files here:
| OS | Path |
|---|---|
| macOS / Linux | |
| Windows | |
The dashboard only reads these files — never modifies them. It caches results in SQLite at .
~/.claude/token-dashboard.dbClaude Code将会话JSONL文件写入以下位置:
| 操作系统 | 路径 |
|---|---|
| macOS / Linux | |
| Windows | |
仪表盘仅读取这些文件——绝不会修改它们。它会将结果缓存到的SQLite数据库中。
~/.claude/token-dashboard.dbDashboard Tabs
仪表盘标签页
| Tab | What it shows |
|---|---|
| Overview | All-time totals, daily charts, cost by plan, top tools, recent sessions |
| Prompts | Most expensive user prompts ranked by tokens; click to see tool calls and result sizes |
| Sessions | Turn-by-turn view with per-turn tokens and tool calls |
| Projects | Per-project comparison: tokens, sessions, files touched |
| Skills | Most-invoked skills and their token costs |
| Tips | Rule-based suggestions (repeated file reads, oversized tool results, low cache-hit rate) |
| Settings | Switch between API / Pro / Max pricing plans |
| 标签页 | 展示内容 |
|---|---|
| 概览 | 全时段总计、每日图表、按套餐划分的成本、高频使用工具、近期会话 |
| 提示 | 按令牌数排序的最昂贵用户提示;点击可查看工具调用和结果大小 |
| 会话 | 逐轮次视图,包含每轮的令牌数和工具调用 |
| 项目 | 项目对比:令牌数、会话数、涉及文件 |
| Skills | 调用最频繁的Skills及其令牌成本 |
| 提示建议 | 基于规则的优化建议(重复文件读取、过大工具结果、低缓存命中率) |
| 设置 | 在API / Pro / Max定价套餐间切换 |
API Endpoints
API端点
The dashboard exposes JSON endpoints at :
http://127.0.0.1:8080/api/bash
undefined仪表盘在暴露JSON端点:
http://127.0.0.1:8080/api/bash
undefinedOverview stats
概览统计
Most expensive prompts
最昂贵的提示
Session list
会话列表
Single session detail
单个会话详情
curl http://127.0.0.1:8080/api/sessions/<session-id>
curl http://127.0.0.1:8080/api/sessions/<session-id>
Project comparison
项目对比
Optimization tips
优化提示建议
undefinedundefinedReal Code Examples
代码示例
Scripting Against the SQLite Cache
基于SQLite缓存编写脚本
After running , query the cache directly:
python3 cli.py scanpython
import sqlite3
import os
db_path = os.path.expanduser("~/.claude/token-dashboard.db")
conn = sqlite3.connect(db_path)运行后,可直接查询缓存:
python3 cli.py scanpython
import sqlite3
import os
db_path = os.path.expanduser("~/.claude/token-dashboard.db")
conn = sqlite3.connect(db_path)Get top 10 most expensive prompts
获取前10个最昂贵的提示
cursor = conn.execute("""
SELECT
project_slug,
session_id,
input_tokens,
output_tokens,
cache_read_tokens,
cost_usd,
substr(user_text, 1, 80) as prompt_preview
FROM turns
ORDER BY cost_usd DESC
LIMIT 10
""")
for row in cursor.fetchall():
print(f"${row[5]:.4f} | {row[0]} | {row[6]}")
conn.close()
undefinedcursor = conn.execute("""
SELECT
project_slug,
session_id,
input_tokens,
output_tokens,
cache_read_tokens,
cost_usd,
substr(user_text, 1, 80) as prompt_preview
FROM turns
ORDER BY cost_usd DESC
LIMIT 10
""")
for row in cursor.fetchall():
print(f"${row[5]:.4f} | {row[0]} | {row[6]}")
conn.close()
undefinedGet Daily Token Totals
获取每日令牌总计
python
import sqlite3
import os
db_path = os.path.expanduser("~/.claude/token-dashboard.db")
conn = sqlite3.connect(db_path)
cursor = conn.execute("""
SELECT
date(created_at) as day,
SUM(input_tokens) as total_input,
SUM(output_tokens) as total_output,
SUM(cache_read_tokens) as total_cache_read,
SUM(cost_usd) as total_cost
FROM turns
GROUP BY date(created_at)
ORDER BY day DESC
LIMIT 30
""")
for row in cursor.fetchall():
print(f"{row[0]}: ${row[4]:.4f} ({row[1]} in, {row[2]} out, {row[3]} cached)")
conn.close()python
import sqlite3
import os
db_path = os.path.expanduser("~/.claude/token-dashboard.db")
conn = sqlite3.connect(db_path)
cursor = conn.execute("""
SELECT
date(created_at) as day,
SUM(input_tokens) as total_input,
SUM(output_tokens) as total_output,
SUM(cache_read_tokens) as total_cache_read,
SUM(cost_usd) as total_cost
FROM turns
GROUP BY date(created_at)
ORDER BY day DESC
LIMIT 30
""")
for row in cursor.fetchall():
print(f"{row[0]}: ${row[4]:.4f} ({row[1]} in, {row[2]} out, {row[3]} cached)")
conn.close()Programmatic Scan via Python
通过Python程序化扫描
python
import sys
import ospython
import sys
import osAdd the project root to path
将项目根目录添加到路径
sys.path.insert(0, '/path/to/token-dashboard')
from token_dashboard.scanner import Scanner
projects_dir = os.path.expanduser("/.claude/projects")
db_path = os.path.expanduser("/.claude/token-dashboard.db")
scanner = Scanner(projects_dir=projects_dir, db_path=db_path)
scanner.scan()
print("Scan complete")
undefinedsys.path.insert(0, '/path/to/token-dashboard')
from token_dashboard.scanner import Scanner
projects_dir = os.path.expanduser("/.claude/projects")
db_path = os.path.expanduser("/.claude/token-dashboard.db")
scanner = Scanner(projects_dir=projects_dir, db_path=db_path)
scanner.scan()
print("扫描完成")
undefinedFetch Overview Stats Programmatically
程序化获取概览统计
python
import urllib.request
import jsonpython
import urllib.request
import jsonRequires dashboard to be running: python3 cli.py dashboard --no-open
需要仪表盘处于运行状态:python3 cli.py dashboard --no-open
with urllib.request.urlopen("http://127.0.0.1:8080/api/overview") as resp:
data = json.loads(resp.read())
print(f"Total sessions: {data['total_sessions']}")
print(f"Total cost (API): ${data['total_cost_usd']:.2f}")
print(f"Cache hit rate: {data['cache_hit_rate']:.1%}")
undefinedwith urllib.request.urlopen("http://127.0.0.1:8080/api/overview") as resp:
data = json.loads(resp.read())
print(f"总会话数: {data['total_sessions']}")
print(f"总成本(API): ${data['total_cost_usd']:.2f}")
print(f"缓存命中率: {data['cache_hit_rate']:.1%}")
undefinedCommon Patterns
常见用法
Reset and Rebuild the Cache
重置并重建缓存
bash
rm ~/.claude/token-dashboard.db
python3 cli.py scanbash
rm ~/.claude/token-dashboard.db
python3 cli.py scanRun on a Different Port to Avoid Conflicts
使用不同端口避免冲突
bash
PORT=9090 python3 cli.py dashboardbash
PORT=9090 python3 cli.py dashboardExport Tips to File
将提示建议导出到文件
bash
python3 cli.py tips > optimization-tips.txtbash
python3 cli.py tips > optimization-tips.txtAutomate Daily Stats Logging
自动记录每日统计
bash
undefinedbash
undefinedAdd to crontab: 0 9 * * * /path/to/daily-stats.sh
添加到crontab: 0 9 * * * /path/to/daily-stats.sh
cd /path/to/token-dashboard && python3 cli.py today >> ~/claude-usage-log.txt
undefinedcd /path/to/token-dashboard && python3 cli.py today >> ~/claude-usage-log.txt
undefinedPoint at a Different Projects Directory
指定不同的项目目录
bash
undefinedbash
undefinedIf Claude Code projects are in a non-standard location
如果Claude Code项目位于非标准位置
python3 cli.py dashboard --projects-dir ~/work/.claude/projects
undefinedpython3 cli.py dashboard --projects-dir ~/work/.claude/projects
undefinedTroubleshooting
故障排除
| Problem | Solution |
|---|---|
| "No data" / empty charts | Run |
| Port 8080 in use | |
| Numbers stuck/wrong | Delete |
| Two instances running | Stop all instances first — they fight over the SQLite DB |
| Use |
| No sessions found | Ensure Claude Code has been used and files exist in |
| 问题 | 解决方案 |
|---|---|
| "无数据"/图表为空 | 运行 |
| 端口8080被占用 | 使用 |
| 数据停滞/错误 | 删除 |
| 两个实例同时运行 | 先停止所有实例——它们会争夺SQLite数据库 |
Windows系统找不到 | 使用 |
| 未找到会话 | 确保已使用Claude Code,且 |
Architecture Overview
架构概览
cli.py
└─► token_dashboard/scanner.py # reads JSONL, dedupes by message.id, writes SQLite
└─► token_dashboard/server.py # serves /api/* JSON routes + web/ static files
└─► web/ # vanilla JS + vendored ECharts, no build step
pricing.json # editable model/plan pricing
~/.claude/token-dashboard.db # SQLite cache (auto-created)Deduplication note: Claude Code writes each assistant response 2–3 times during streaming. The scanner dedupes by so tallies match actual API billing — expect lower numbers than tools that sum every raw JSONL row.
message.idcli.py
└─► token_dashboard/scanner.py # 读取JSONL,按message.id去重,写入SQLite
└─► token_dashboard/server.py # 提供/api/* JSON路由 + web/静态文件
└─► web/ # 原生JS + 引入的ECharts,无需构建步骤
pricing.json # 可编辑的模型/套餐定价
~/.claude/token-dashboard.db # SQLite缓存(自动创建)去重说明: Claude Code在流式传输过程中会将每个助手响应写入2–3次。扫描器会按去重,因此统计结果与实际API账单一致——相比那些直接累加所有原始JSONL行的工具,这里的数值会更低。
message.id