Skip to main content

Console OpenAI API cost and token usage analysis by key and model

Project description

openai-usage 📊

Python PyPI Docker License

A command-line tool to inspect token usage and estimated cost for your OpenAI projects.

flowchart TB
    A[OPENAI_ADMIN_API_KEY] --> B[Fetch Projects & Usage]
    C[litellm Pricing DB] --> D[Local Cache]
    B --> E[Calculate Costs]
    D --> E
    E --> F[Color-coded Terminal Table]

🚀 Features

Feature Description
📋 List Projects See all projects accessible with your admin API key
📊 Detailed Usage Usage data broken down by day, model, and API key
💰 Auto-Updated Pricing Pricing fetched from litellm and cached locally
🔀 Flexible Grouping Group and sort by day, project, key, or model
📅 Custom Date Ranges Focus on a specific time period
🎨 Colorful Output Clear, color-coded table for easy reading

📦 Installation

From PyPI (recommended)

pip install openai-usage-report

Or with uv:

uv pip install openai-usage-report

From GitHub

# With uv
uv pip install git+https://github.com/obeone/scripts#subdirectory=openai-usage

# With pipx
git clone https://github.com/obeone/scripts
cd scripts
pipx install ./openai-usage

Environment Variable

This tool requires an admin-level OpenAI API key:

export OPENAI_ADMIN_API_KEY="your_admin_api_key_here"

🔧 Usage

List Available Projects

openai-usage --list-projects

View Usage for All Projects

openai-usage

View Usage for Specific Projects and a Date Range

openai-usage --projects proj_xxxxxxxxxxxx --start-date 2024-01-01 --end-date 2024-01-31

Group and Sort Results

openai-usage --group-by project day

Manage Pricing Data

Pricing is automatically fetched from litellm on first run and cached locally (~/.cache/openai-usage/pricing.json). To update manually:

openai-usage --update-pricing

To check cache status:

openai-usage --pricing-info

A warning is displayed if the cache is older than 30 days.


🐳 Docker Usage

Command Description
docker build -t openai-usage . Build the image
docker run --rm -e OPENAI_ADMIN_API_KEY="..." openai-usage View all projects usage
docker run --rm -e OPENAI_ADMIN_API_KEY="..." openai-usage -l List projects
docker run --rm -e OPENAI_ADMIN_API_KEY="..." openai-usage -p proj_xxx Specific project

⚙️ Options

Argument Short Description Default
--help -h Show the help message and exit
--list-projects -l List available projects and exit
--projects [ID ...] -p One or more project IDs to analyze All projects
--start-date YYYY-MM-DD -sd Start date for the usage report Start of current month
--end-date YYYY-MM-DD -ed End date for the usage report End of current month
--group-by [CRITERIA ...] -gb Criteria to group and sort results (order matters) day
--update-pricing Fetch latest pricing from litellm and update local cache
--pricing-info Show pricing cache path, last update date, and model count

Available Grouping Criteria: day, project, key, model


📄 License

MIT — Grégoire Compagnon (@obeone)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openai_usage_report-0.3.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openai_usage_report-0.3.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file openai_usage_report-0.3.0.tar.gz.

File metadata

  • Download URL: openai_usage_report-0.3.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for openai_usage_report-0.3.0.tar.gz
Algorithm Hash digest
SHA256 0495f6d4ed72b287538337f04aeb3ec16f569d0242b27d2a4bfed8aae601368f
MD5 18581ab5e0e762f21586e8059a4cada1
BLAKE2b-256 27d61a6b026bf10e22bea799fbb43e3eac0c93eacd4207013d767217cdc194ee

See more details on using hashes here.

File details

Details for the file openai_usage_report-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_usage_report-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1653034066b6cf906085facd8f8d26c4c50b26f7ccd425c962db2a0724bc1059
MD5 595ce0d1706eac79f06f4ec8ba06a7fa
BLAKE2b-256 322acd444708dbccbceff4bfb35a08b95012cc157890c591f701c0ba962e938d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page