codex-stats is a local analytics CLI for Codex usage.
It reads your local Codex state from ~/.codex and surfaces:
- session summaries
- rolling usage totals across today, week, month, or the last
Ndays - model and project breakdowns
- project-specific drilldowns
- recent session history
- estimated token-based cost
- anomaly-aware usage insights and recommendations
- export and import for cross-device snapshots
- merged export snapshots for multi-device rollups
- OTLP metrics export for Grafana and OpenTelemetry collectors
- shareable weekly and monthly reports
pipx install codex-statsOr with pip:
python3 -m pip install codex-statscodex-statsShow the default usage summary for today.codex-stats todayShow today's usage summary explicitly.codex-stats weekShow usage totals for the last 7 days.codex-stats monthShow usage totals for the last 30 days.codex-stats --days 14Show a rolling summary for the lastNdays.codex-stats sessionShow the most recent session in detail.codex-stats session --id <session_id>Show one specific session by ID.codex-stats modelsBreak usage down by model.codex-stats projectBreak usage down by project.codex-stats project backend-apiShow a single project's summary across all available local data.codex-stats project backend-api --days 30Show a single project's summary for a rolling time window.codex-stats dailyShow per-day usage with an ASCII trend graph.codex-stats compareCompare the last 7 days against the previous 7 days.codex-stats compare today yesterdayCompare named time windows directly.codex-stats historyShow recent session history.codex-stats topShow the largest sessions by token usage.codex-stats top --project backend-apiShow the largest sessions for one project.codex-stats costsShow estimated cost totals and monthly projection.codex-stats insightsShow anomaly-aware insights and recommended next steps.codex-stats doctorValidate local Codex data sources and config.codex-stats doctor --strictReturn a non-zero exit code if any doctor check fails.codex-stats initCreate a default config file under~/.config/codex-stats/.codex-stats config showShow the effective config, including pricing and display defaults.codex-stats report weeklyGenerate a weekly shareable report.codex-stats report weekly --format markdownGenerate a weekly report in Markdown.codex-stats report weekly --format htmlGenerate a standalone HTML report for sharing, including inline charts.codex-stats report weekly --format svgGenerate a bundle of four smaller standalone SVG assets in the current working directory: summary, cost, focus, and project snapshots.codex-stats report weekly --project backend-apiGenerate a weekly report for one project.codex-stats report weekly --format markdown --output weekly-report.mdWrite a formatted report to a file.codex-stats report weekly --format html --output weekly-report.htmlWrite a polished standalone HTML report to a file.codex-stats report weekly --format svg --output weekly-report-assetsWrite the SVG asset bundle into the output directory you provide.codex-stats report weekly --format html --renderGenerate an HTML report, write it to a temp file, and open it in the default browser.codex-stats report weekly --format svg --renderGenerate an SVG report, write it to a temp file, and open it in the default browser.codex-stats export codex-stats-export.jsonExport normalized local stats to JSON.codex-stats export codex-stats-export.json --since 30dExport only a rolling window of recent sessions.codex-stats import laptop.json desktop.jsonRead one or more exported snapshots and summarize them.codex-stats merge merged.json laptop.json desktop.jsonMerge multiple exported snapshots into one deduplicated file.codex-stats merge merged.json laptop.json desktop.json --jsonMerge snapshots and return a machine-readable merge summary.codex-stats otel --output otlp-metrics.jsonWrite OTLP JSON metrics derived from local Codex session data.codex-stats otel --endpoint http://localhost:4318/v1/metricsPush OTLP JSON metrics directly to an OTLP/HTTP collector endpoint.codex-stats otel --since 30d --daily-days 14 --resource-attr deployment.environment=devExport a rolling window plus daily history with additional resource metadata.codex-stats watchRun a live terminal dashboard that refreshes usage summaries continuously.codex-stats watch --project backend-api --days 14 --interval 2Watch one project with a shorter refresh interval and a custom rolling window.codex-stats watch --alert-cost-usd 20 --alert-tokens 500000 --alert-delta-pct 50Raise live alerts when the rolling window crosses your chosen thresholds.codex-stats watch --reset-stateIgnore the saved watch baseline for this scope and rebuild it from the current snapshot.codex-stats completions zshPrint shell completion setup for your shell.codex-stats --color alwaysForce ANSI color output.codex-stats --jsonReturn machine-readable JSON output for supported commands.
codex-stats does not proxy or intercept Codex API traffic.
It reads local Codex artifacts, including:
state_5.sqlitefor session metadata- rollout JSONL files for request and token snapshots
- Costs are estimates, not billing values.
- Output depends on local Codex file formats remaining compatible.
exportandimportlet you move normalized snapshots between machines.mergelets you deduplicate and combine exported snapshots into one file.export --since Ndlimits snapshots to a rolling window before sharing.otelemits OTLP/HTTP JSON metrics, including aggregate token counters and daily historical gauges.watchis intended for interactive terminals and exits cleanly onCtrl-C.watchpersists alert/session baseline state under the config directory soNEWmarkers survive restarts unless you pass--reset-state.doctor --strictis useful in scripts and CI because it returns a non-zero exit code on failed checks.--color auto|always|nevercontrols ANSI styling.
The otel command converts local Codex session data into OTLP JSON metrics so you can feed them into Grafana, Grafana Alloy, or any OTLP/HTTP collector.
Write the payload to disk:
codex-stats otel --output otlp-metrics.jsonPush directly to an OTLP/HTTP endpoint:
codex-stats otel --endpoint http://localhost:4318/v1/metricsAdd resource metadata or restrict the export window:
codex-stats otel \
--since 30d \
--daily-days 14 \
--resource-attr deployment.environment=dev \
--resource-attr service.namespace=developer-toolsExported metrics include aggregate sums such as codex_stats_tokens, codex_stats_requests, and codex_stats_estimated_cost_usd, plus daily gauges such as codex_stats_daily_tokens.
This repo now includes starter assets under examples/grafana/codex-stats-dashboard.json and examples/grafana/grafana-alloy.alloy.
Quick local loop:
alloy run examples/grafana/grafana-alloy.alloy
codex-stats otel --endpoint http://localhost:4318/v1/metrics --since 30dThen import the dashboard JSON into Grafana and point it at your Prometheus-compatible datasource. The dashboard includes:
- topline stats for tokens, requests, sessions, and estimated cost
- daily token, request, and cost charts
- table views for token usage by project and estimated cost by model
The current priority list lives in docs/roadmap.md.
Share-ready assets now live under docs/assets/codex-stats-share-card.svg, and report --format svg can generate a full bundle of report assets directly from local usage data.
watch can surface live warnings and critical alerts directly in the terminal dashboard.
Example:
codex-stats watch \
--days 7 \
--alert-cost-usd 20 \
--alert-tokens 500000 \
--alert-requests 20 \
--alert-delta-pct 50Alerts currently cover:
- explicit cost, token, request, and delta thresholds
- large usage spikes versus the previous window
- anomaly conditions already detected by the insights engine
- stateful marking of newly triggered conditions and newly observed sessions across watch restarts for the same scope
Optional pricing config lives at ~/.config/codex-stats/config.toml.
[pricing]
default_usd_per_1k_tokens = 0.01
[pricing.model_usd_per_1k_tokens]
gpt-5.4 = 0.02
gpt-5-mini = 0.005To create the config file automatically:
codex-stats initTo inspect the effective config:
codex-stats config show
codex-stats config show --jsonMachine-readable output is intended to stay stable across patch releases.
summaryJSON: time-window totals such as sessions, requests, tokens, cost estimate, and top modelreportJSON: period, optional project scope, summary, comparison, projects, top sessions, costs, and insightsexportJSON:schema_version,exported_at, and normalizedsessionsdoctorJSON: a list of checks withname,ok,detail, andseverityconfigJSON: config path, whether the file exists, pricing defaults, overrides, and display defaultsimportandmergeJSON: import summary plus deduped session counts
Full field documentation lives in docs/json-schema.md.
For local development from the repo:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -U pip setuptools
python -m pip install -e .Run without installing:
PYTHONPATH=src python3 -m codex_stats