codex-token-summary
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCodex Token 消耗统计
Codex Token Consumption Statistics
快速使用(推荐使用 Python 脚本)
Quick Start (Recommended to Use Python Script)
本技能提供参数化的 Python 脚本,无需手工编写 SQL/JSON 解析代码。脚本位于本目录 ,支持灵活的时间范围、输出格式控制。
codex_token_summary.pyThis skill provides a parameterized Python script, eliminating the need to manually write SQL/JSON parsing code. The script is located in this directory as , supporting flexible time ranges and output format control.
codex_token_summary.py常用命令
Common Commands
bash
undefinedbash
undefined最近 7 天简单表格(默认)
Simple table for the last 7 days (default)
python codex_token_summary.py
python codex_token_summary.py
最近 30 天
Last 30 days
python codex_token_summary.py --days 30
python codex_token_summary.py --days 30
自定义日期范围
Custom date range
python codex_token_summary.py --start-date 2026-04-01 --end-date 2026-05-18
python codex_token_summary.py --start-date 2026-04-01 --end-date 2026-05-18
详细格式(含 Token 分类)
Detailed format (including Token categories)
python codex_token_summary.py --days 7 --format detailed
python codex_token_summary.py --days 7 --format detailed
输出为 JSON
Output as JSON
python codex_token_summary.py --days 14 --output json
python codex_token_summary.py --days 14 --output json
包含周末
Include weekends
python codex_token_summary.py --days 30 --no-exclude-weekends
undefinedpython codex_token_summary.py --days 30 --no-exclude-weekends
undefinedWindows 环境初始化
Windows Environment Initialization
若 Windows 上未安装 Python,使用同目录的 一键安装并配置:
setup_python_env.ps1powershell
undefinedIf Python is not installed on Windows, use the in the same directory to install and configure it with one click:
setup_python_env.ps1powershell
undefined以管理员身份运行 PowerShell,然后执行
Run PowerShell as administrator, then execute
.\setup_python_env.ps1
该脚本会自动检测 Python、从 python.org 下载官方安装包、加入 PATH,并安装脚本所需的依赖(仅 Python < 3.9 时需 `backports.zoneinfo`)。.\setup_python_env.ps1
This script will automatically detect Python, download the official installation package from python.org, add it to PATH, and install dependencies required by the script (only `backports.zoneinfo` is needed when Python < 3.9).脚本参数速查
Quick Reference for Script Parameters
| 参数 | 说明 | 示例 |
|---|---|---|
| 最近 N 天(默认 7) | |
| 自定义开始日期 | |
| 自定义结束日期 | |
| 输出详细程度(默认 simple) | |
| 输出类型(默认 table) | |
| 包含周末 | |
| 时区(默认 Asia/Shanghai) | |
详见本目录 。
README_SCRIPT.md| Parameter | Description | Example |
|---|---|---|
| Last N days (default: 7) | |
| Custom start date | |
| Custom end date | |
| Output detail level (default: simple) | |
| Output type (default: table) | |
| Include weekends | |
| Time zone (default: Asia/Shanghai) | |
See in this directory for details.
README_SCRIPT.md适用场景
Applicable Scenarios
当用户要求统计本机 Codex / Codex Desktop 在一段时间内的 Token 消耗,尤其是需要按任务目的、会话、模型、Token 类型占比输出表格时,使用本技能。
默认使用中文输出。除非用户另有说明,统计“最近 7 天”时按自然日回看 7 天。
This skill is used when users need to count the Token consumption of the local Codex / Codex Desktop over a period of time, especially when requiring tables output by task purpose, session, model, and Token type proportion.
Chinese output is used by default. Unless specified otherwise by the user, "last 7 days" refers to looking back 7 natural days.
数据源
Data Sources
主要读取两个本地数据源:
-
~/.codex/state_*.sqlite- 表:
threads - 用途:获取线程 ID、任务目的、项目路径、模型、rollout 文件路径、创建/更新时间、总 Token 计数。
- 应在 下发现
~/.codex/,再按 schema 判断哪个是状态库。state_*.sqlite - 关键字段:
idrollout_pathcwdtitlepreviewfirst_user_messagecreated_atupdated_attokens_usedmodelreasoning_effortagent_rolethread_source
兼容来源:- 旧的 里 session 名称字段通常叫
~/.codex/session_index.jsonl,只在状态库缺失或需要交叉校验时使用。thread_name
- 表:
-
和
~/.codex/sessions/**/*.jsonl~/.codex/archived_sessions/*.jsonl- 路径通常来自 ,不要只扫描
threads.rollout_path,因为较早或归档会话可能在sessions/。archived_sessions/ - 用途:读取 事件,按事件时间做增量统计。
token_count - 关键事件:
- JSONL 行的
type == "event_msg" payload.type == "token_count"payload.info.total_token_usage
- JSONL 行的
- 路径通常来自
Mainly reads two local data sources:
-
~/.codex/state_*.sqlite- Table:
threads - Purpose: Obtain thread ID, task purpose, project path, model, rollout file path, creation/update time, total Token count.
- Should find under
state_*.sqlite, then determine which one is the state database based on the schema.~/.codex/ - Key fields:
idrollout_pathcwdtitlepreviewfirst_user_messagecreated_atupdated_attokens_usedmodelreasoning_effortagent_rolethread_source
Compatible source:- The session name field in the old is usually called
~/.codex/session_index.jsonl, which is only used when the state database is missing or cross-verification is needed.thread_name
- Table:
-
and
~/.codex/sessions/**/*.jsonl~/.codex/archived_sessions/*.jsonl- The path usually comes from ; do not only scan
threads.rollout_path, as older or archived sessions may be insessions/.archived_sessions/ - Purpose: Read events and perform incremental statistics by event time.
token_count - Key events:
- in JSONL lines
type == "event_msg" payload.type == "token_count"payload.info.total_token_usage
- The path usually comes from
访问方式
Access Method
优先发现可用的 Codex 状态 SQLite,再查询线程元数据,最后按 逐个读取 JSONL。
rollout_path状态库发现规则:
- 在 下查找
~/.codex/。state_*.sqlite - 对候选文件执行 或查询
.tables。sqlite_master - 选择包含 表的文件。
threads - 校验 表至少包含这些字段:
threads、id、rollout_path、cwd、created_at。tokens_used - 如果多个文件都匹配,优先选择最近修改时间最新的文件;必要时输出候选列表让用户确认。
- 不要把 当主统计源。日志库可用于调试,但项目维度和 rollout 路径来自状态库的
logs_*.sqlite表。threads
示例查询:
bash
STATE_DB="$(find ~/.codex -maxdepth 1 -name 'state_*.sqlite' -type f -print | sort | tail -1)"
sqlite3 -json "$STATE_DB" \
"select id, rollout_path, cwd, title, preview, first_user_message, tokens_used, created_at, updated_at, model, reasoning_effort, agent_role, thread_source from threads"注意windows下使用符合windows powershell的脚本命令查询
推荐用脚本聚合,而不是只使用 。原因是 是线程级累计值,无法按天切分,也无法拆出缓存输入、输出、思考等类型。
threads.tokens_usedthreads.tokens_usedPrioritize discovering available Codex state SQLite, then query thread metadata, and finally read JSONL files one by one according to .
rollout_pathState database discovery rules:
- Search for under
state_*.sqlite.~/.codex/ - Execute or query
.tableson candidate files.sqlite_master - Select the file that contains the table.
threads - Verify that the table contains at least these fields:
threads,id,rollout_path,cwd,created_at.tokens_used - If multiple files match, prioritize the one with the latest modification time; if necessary, output a candidate list for user confirmation.
- Do not use as the main statistics source. The log database can be used for debugging, but project dimensions and rollout paths come from the
logs_*.sqlitetable of the state database.threads
Example query:
bash
STATE_DB="$(find ~/.codex -maxdepth 1 -name 'state_*.sqlite' -type f -print | sort | tail -1)"
sqlite3 -json "$STATE_DB" \
"select id, rollout_path, cwd, title, preview, first_user_message, tokens_used, created_at, updated_at, model, reasoning_effort, agent_role, thread_source from threads"Note: Use script commands compatible with Windows PowerShell for queries on Windows
It is recommended to aggregate using the script instead of only using . The reason is that is a thread-level cumulative value, which cannot be split by day or broken down into types like cached input, output, reasoning, etc.
threads.tokens_usedthreads.tokens_used时间范围
Time Range
默认口径:
- 时区:
Asia/Shanghai - 当前日期以运行环境日期为准。
- “最近 7 天”表示包含今天在内的 7 个自然日。
- 排除 的日期,即周六和周日。
weekday >= 5 - 用 JSONL 事件的
token_count判断是否落入统计范围。timestamp
示例:如果当前日期是 ,统计范围为 到 ,实际计入工作日为 、、、、。
2026-05-182026-05-122026-05-182026-05-122026-05-132026-05-142026-05-152026-05-18Default specifications:
- Time zone:
Asia/Shanghai - Current date is based on the runtime environment date.
- "Last 7 days" means 7 natural days including today.
- Exclude dates where , i.e., Saturdays and Sundays.
weekday >= 5 - Use the of JSONL
timestampevents to determine if they fall within the statistical range.token_count
Example: If the current date is , the statistical range is from to , and the actual working days included are , , , , .
2026-05-182026-05-122026-05-182026-05-122026-05-132026-05-142026-05-152026-05-18聚合算法
Aggregation Algorithm
对每个 rollout JSONL 文件:
- 读取对应 行,生成“任务目的”作为主维度。
threads - 顺序读取 JSONL 中的 事件。
token_count - 每个事件里的 是线程累计值,不是单次增量。
total_token_usage - 对同一文件内相邻 做差,得到本次事件增量。
token_count - 只累加事件时间落入统计工作日范围内的增量。
- 按“任务目的”聚合总量。
- 同时保留项目路径、模型、会话 ID 等明细,便于解释异常大头
任务目的生成规则:
- 优先使用 。
threads.title - 如果 为空或明显是系统审查/子 agent 长指令,可回退到
title。threads.preview - 如果 也不可用,可回退到
preview的首行或前 80 个字符。threads.first_user_message - 保留 和
thread_source,因为子 agent 的任务目的通常来自 spawn 指令,不一定等同用户原始需求。agent_role - 对过长任务目的做展示截断,但聚合键应使用完整值,避免不同任务被合并。
如果某个线程创建于统计范围内但找不到任何 事件,需要在结果里说明缺失,不要静默忽略。
token_countFor each rollout JSONL file:
- Read the corresponding row and generate "task purpose" as the main dimension.
threads - Sequentially read events in JSONL.
token_count - in each event is a cumulative value for the thread, not a single increment.
total_token_usage - Calculate the difference between adjacent events in the same file to get the increment of this event.
token_count - Only accumulate increments whose event time falls within the statistical working days.
- Aggregate the total amount by "task purpose".
- At the same time, retain details such as project path, model, session ID to facilitate explaining abnormal large amounts.
Task purpose generation rules:
- Prioritize using .
threads.title - If is empty or obviously a system review/sub-agent long instruction, fall back to
title.threads.preview - If is also unavailable, fall back to the first line or first 80 characters of
preview.threads.first_user_message - Retain and
thread_source, because the task purpose of sub-agents usually comes from spawn instructions, which may not be equivalent to the user's original requirement.agent_role - Truncate overly long task purposes for display, but use the complete value as the aggregation key to avoid merging different tasks.
If a thread was created within the statistical range but no events are found, it needs to be explained in the results instead of being silently ignored.
token_countToken 分类
Token Categories
Codex 本地记录能直接拆出的字段:
input_tokenscached_input_tokensoutput_tokensreasoning_output_tokenstotal_tokens
推荐输出分类:
- :
新输入/检索上下文input_tokens - cached_input_tokens - :
缓存输入cached_input_tokens - :
输出/写代码output_tokens - reasoning_output_tokens - :
思考reasoning_output_tokens
Fields that can be directly extracted from local Codex records:
input_tokenscached_input_tokensoutput_tokensreasoning_output_tokenstotal_tokens
Recommended output categories:
- :
New Input/Retrieval Contextinput_tokens - cached_input_tokens - :
Cached Inputcached_input_tokens - :
Output/Code Writingoutput_tokens - reasoning_output_tokens - :
Reasoningreasoning_output_tokens
统计维度
Statistical Dimensions
主维度:
- 任务目的:优先来自 ,必要时回退到
threads.title或threads.previewthreads.first_user_message
默认指标:
项目模型Token 总量占比新输入/检索上下文缓存输入输出/写代码思考
可选辅助维度:
- 日期
- 会话 ID:
id - 项目路径:
cwd - 模型:
model - 推理强度:
reasoning_effort - 子 agent 类型:
agent_role - 线程来源:
thread_source
Main dimension:
- Task purpose: Prioritized from , fall back to
threads.titleorthreads.previewif necessary.threads.first_user_message
Default metrics:
ProjectModelTotal TokensProportionNew Input/Retrieval ContextCached InputOutput/Code WritingReasoning
Optional auxiliary dimensions:
- Date
- Session ID:
id - Project path:
cwd - Model:
model - Reasoning effort:
reasoning_effort - Sub-agent type:
agent_role - Thread source:
thread_source
模型
Model
模型字段:
- 优先使用 。
threads.model - 如果 为空,可从 rollout JSONL 的
threads.model中寻找模型字段。turn_context.payload.model - 不要把 当成模型名;它通常只是供应商,例如
session_meta.payload.model_provider。openai - 推理强度优先使用 ;如果直接读 JSONL,可用
threads.reasoning_effort。turn_context.payload.effort - 同一个任务目的下如果出现多个模型,表格中用逗号列出,或显示为 。
mixed: model-a / model-b
Model field:
- Prioritize using .
threads.model - If is empty, look for the model field from
threads.modelin the rollout JSONL.turn_context.payload.model - Do not treat as the model name; it is usually just the provider, e.g.,
session_meta.payload.model_provider.openai - Prioritize using for reasoning intensity; if reading JSONL directly, use
threads.reasoning_effort.turn_context.payload.effort - If multiple models appear under the same task purpose, list them with commas in the table, or display as .
mixed: model-a / model-b
输出格式
Output Format
先用一段话说明统计口径,再输出表格。默认输出“简单”方案;只有用户明确要求详细分类、Token 类型占比、输入/缓存/输出/思考拆分时,才输出“详细”方案。
First use a paragraph to explain the statistical specifications, then output the table. The "simple" scheme is output by default; the "detailed" scheme is only output when the user explicitly requests detailed classification, Token type proportion, or split of input/cache/output/reasoning.
简单方案(默认)
Simple Scheme (Default)
先输出 摘要行,再输出明细表格。 展示总 Token 量和 100% 占比。
合计合计markdown
**合计 Token**:XXX(100%)表格字段固定为:
| 任务目的 | 模型 | Token 总量 | 占比 |
|---|
示例:
markdown
| `统计近7天项目Token消耗` | `gpt-5.5` | 255,187,782 | 99.55% |明细表格中不再包含 行。
合计First output the summary row, then the detailed table. The shows the total Token amount and 100% proportion.
TotalTotalmarkdown
**Total Tokens**: XXX (100%)Fixed table fields:
| Task Purpose | Model | Total Tokens | Proportion |
|---|
Example:
markdown
| `Count project Token consumption in the last 7 days` | `gpt-5.5` | 255,187,782 | 99.55% |The row is no longer included in the detailed table.
Total详细方案
Detailed Scheme
用户要求详细输出时,先输出 摘要行,再输出明细表格。 展示各分类汇总值及占总量的百分比。
合计合计markdown
**合计 Token**:XXX(100%)| 新输入/检索上下文:XXX (XX%) | 缓存输入:XXX (XX%) | 输出/写代码:XXX (XX%) | 思考:XXX (XX%)明细表格使用当前完整字段:
| 任务目的 | 项目 | 模型 | Token 总量 | 占比 | 新输入/检索上下文 | 缓存输入 | 输出/写代码 | 思考 |
|---|
每个分类字段建议显示为:
text
数量 (占该任务总量百分比)例如:
markdown
| `统计近7天项目Token消耗` | `/path/to/project` | `gpt-5.5` | 255,187,782 | 99.55% | 25,302,445 (9.91%) | 229,055,744 (89.76%) | 619,303 (0.24%) | 210,290 (0.08%) |明细表格中不再包含 行。
合计When the user requests detailed output, first output the summary row, then the detailed table. The shows the aggregated value of each category and its percentage of the total amount.
TotalTotalmarkdown
**Total Tokens**: XXX (100%) | New Input/Retrieval Context: XXX (XX%) | Cached Input: XXX (XX%) | Output/Code Writing: XXX (XX%) | Reasoning: XXX (XX%)The detailed table uses the complete current fields:
| Task Purpose | Project | Model | Total Tokens | Proportion | New Input/Retrieval Context | Cached Input | Output/Code Writing | Reasoning |
|---|
Each category field is recommended to be displayed as:
text
Amount (percentage of the total amount of this task)Example:
markdown
| `Count project Token consumption in the last 7 days` | `/path/to/project` | `gpt-5.5` | 255,187,782 | 99.55% | 25,302,445 (9.91%) | 229,055,744 (89.76%) | 619,303 (0.24%) | 210,290 (0.08%) |The row is no longer included in the detailed table.
Total结果说明
Result Explanation
表格后应补充一段限制说明:
- 数据来自本机 中符合 schema 的状态库,以及 rollout JSONL。
~/.codex/state_*.sqlite - 分类中的 、
缓存输入、输出是 Codex 记录的结构化 Token 字段。思考 - “搜索、写代码、读文件”等语义类型没有独立精确字段,只能通过 Token 字段近似解释。
- 如果存在缺失 rollout 文件或缺失 事件的线程,需要列出受影响线程数量和路径。
token_count
A paragraph of limitation explanation should be added after the table:
- Data comes from the state database that conforms to the schema in the local , and rollout JSONL.
~/.codex/state_*.sqlite - ,
Cached Input,Outputin the categories are structured Token fields recorded by Codex.Reasoning - Semantic types such as "search, code writing, file reading" do not have independent precise fields and can only be approximately explained through Token fields.
- If there are threads with missing rollout files or missing events, list the number of affected threads and their paths.
token_count
推荐校验
Recommended Verification
统计完成后做两项校验:
- 检查是否有统计范围内创建的线程没有 事件。
token_count - 对比项目聚合总量与会话增量总量之和是否一致。
Perform two verifications after statistics are completed:
- Check if there are threads created within the statistical range without events.
token_count - Compare whether the total aggregated amount of projects is consistent with the sum of session incremental amounts.