Skip to content

[bin] Explore creating a CLI helper for parsing OpenAI codex token usage and computing costs #41

@0xdevalias

Description

@0xdevalias

Explore creating a CLI helper that can parse the OpenAI Codex token-usage output format and compute detailed cost breakdowns. The primary use case is taking a typical Codex usage string such as:

Token usage: total=112,632 input=67,873 (+ 1,148,032 cached) output=44,759 (reasoning 37,312)

Extracting the relevant token counts (input, cached, output, reasoning, etc.), looking up model pricing, and calculating & displaying the cost for each section and the total.

The tool should also support being invoked with individual token parameters (e.g. --input=… --output=… --reasoning=…) for situations where the usage string isn’t available or when doing custom cost estimation.

Model & Pricing Sources / Constraints

We need solid sources to get model/pricing info. Some options & caveats:

Proposal: Implementation Notes

  • Maintain a pricing config (JSON/YAML) mapping relevant models (Codex, codex-mini, etc.) → cost per token category (input, output, cached, reasoning). Include metadata like “last verified” date.
  • Support two input modes:
    1. Full usage string in Codex format (as above)
    2. Individual token flags (e.g. --input, --cached, --output, --reasoning) for custom or partial data.
  • Handle unknown/new models gracefully (warn user, allow manual override of pricing, fallback to a default model or default rates).
  • Optionally a command or flag like --update-pricing which attempts to fetch/prune pricing from official sources (scraping or via OpenAI API if/when one exists). Robust handling for errors or stale data.
  • If relevant, support currency options or conversion (e.g. default USD, maybe allow AUD etc.) to make it usable locally.

Example CLI Output Mockup

$ bin/codex-token-cost codex-mini "Token usage: total=112,632 input=67,873 (+ 1,148,032 cached) output=44,759 (reasoning 37,312)"

Model: codex-mini
Pricing: input $1.50 / 1M tokens, output $6.00 / 1M tokens, cached $0.75 / 1M tokens, reasoning $X / 1M tokens

  • Input tokens:         67,873        → $0.102
  • Cached tokens:      1,148,032        → $0.861
  • Output tokens:         44,759        → $0.269
  • Reasoning tokens:      37,312        → $0.XXX
  ─────────────────────────────────────────────

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions