中文 | English
一个用于统计 Codex 桌面端本地会话 Token 用量的小工具。
这个工具会扫描本地 Codex 会话目录:
~/.codex/sessions~/.codex/archived_sessions
然后读取每个会话最后一次 token_count.total_token_usage 事件,汇总出:
input_tokenscached_input_tokensoutput_tokensreasoning_output_tokenstotal_tokens
同时还会生成一个自包含的 HTML 可视化报告,包含:
- 指标卡
- 按天趋势图
- 高消耗会话排行
- 不需要 API Key
- 直接读取本地 Codex 会话记录
- 可作为 CLI 工具安装
- 支持终端汇总输出
- 支持按天统计
- 支持 JSON 导出
- 支持自包含 HTML 可视化
- 已配置 GitHub Actions 基础校验
- Python 3.9+
不依赖第三方 Python 包。
直接运行:
python3 codex_token_usage.py或者安装成命令行工具:
pip install .
codex-token-usage默认运行:
python3 codex_token_usage.py这会在终端输出统计结果,并生成:
./codex_token_usage_report.html常用示例:
python3 codex_token_usage.py --by-day
python3 codex_token_usage.py --top-sessions 20
python3 codex_token_usage.py --json
python3 codex_token_usage.py --html report.html
python3 codex_token_usage.py --codex-home ~/.codex安装后可直接使用:
codex-token-usage
codex-token-usage --by-day --top-sessions 20
codex-token-usage --json后续更新并推送:
./publish.sh "Update report layout"GitHub Actions 当前会执行:
- Python 语法编译检查
- 基于空 Codex 目录的 JSON 输出检查
- 基于空 Codex 目录的 HTML 报告生成检查
cached_input_tokens属于输入 Token 的缓存命中部分,不应再额外叠加到input_tokensreasoning_output_tokens通常是输出 Token 的子集,不应重复计算- 这是基于本地会话日志的估算值,不是官方账单值
codex_token_usage.pypyproject.toml.github/workflows/ci.ymlassets/report-preview.svgsample_report.html
MIT
This is a small utility for estimating Codex desktop token usage from local session files.
It scans:
~/.codex/sessions~/.codex/archived_sessions
Then, for each session, it reads the last token_count.total_token_usage event and aggregates:
input_tokenscached_input_tokensoutput_tokensreasoning_output_tokenstotal_tokens
It also generates a self-contained HTML report with:
- summary cards
- daily usage trend chart
- top session rankings
- No API key required
- Reads local Codex session history directly
- Installable as a CLI tool
- Terminal summary output
- Daily breakdown support
- JSON export
- Self-contained HTML visualization
- GitHub Actions CI for basic validation
- Python 3.9+
No third-party Python dependencies are required.
Run directly:
python3 codex_token_usage.pyOr install it as a CLI:
pip install .
codex-token-usageRun with defaults:
python3 codex_token_usage.pyThis prints a terminal summary and writes:
./codex_token_usage_report.htmlCommon examples:
python3 codex_token_usage.py --by-day
python3 codex_token_usage.py --top-sessions 20
python3 codex_token_usage.py --json
python3 codex_token_usage.py --html report.html
python3 codex_token_usage.py --codex-home ~/.codexInstalled CLI examples:
codex-token-usage
codex-token-usage --by-day --top-sessions 20
codex-token-usage --jsonPush later updates:
./publish.sh "Update report layout"GitHub Actions currently runs:
- Python syntax compilation
- JSON output validation against an empty fixture Codex directory
- HTML report generation validation against an empty fixture Codex directory
cached_input_tokensis part of input usage and should not be added on top ofinput_tokensreasoning_output_tokensis typically a subset of output usage and should not be double-counted- This is a local estimate from session logs, not an official billing number
codex_token_usage.pypyproject.toml.github/workflows/ci.ymlassets/report-preview.svgsample_report.html
MIT