Skip to main content

CLI tool to track and display OpenCode token usage statistics

Project description

opencode-usage

CLI tool to track and display OpenCode token usage statistics. Reads directly from OpenCode's local SQLite database — no API keys or external services needed for basic usage.

Features

  • Daily breakdown — token usage and cost per day
  • Group by dimension — model, agent, provider, or session
  • Agent × Model view — see which model each agent uses
  • Time filtering — last N days, relative durations (7d, 2w), or ISO dates
  • Period comparison — compare current vs previous period with --compare
  • JSON output — pipe to jq or other tools
  • LLM-powered insights — analyze session transcripts and generate a self-contained HTML report
  • Cross-platform — macOS, Linux, Windows

Installation

# From PyPI
pip install opencode-usage

# Or with uv
uv tool install opencode-usage

From source

git clone https://github.com/rchardx/opencode-usage.git
cd opencode-usage
uv sync
uv tool install -e .

After installation, opencode-usage is available globally.

Usage

The CLI has two subcommands: run (default) and insights.

run — Token usage statistics

# Default: last 7 days, daily breakdown
opencode-usage

# Time filtering
opencode-usage run --days 30
opencode-usage run --since 7d
opencode-usage run --since 2025-01-01

# Group by dimension
opencode-usage run --by model
opencode-usage run --by agent          # shows model per agent
opencode-usage run --by provider
opencode-usage run --by session --limit 10

# JSON output
opencode-usage run --json
opencode-usage run --by model --json | jq '.rows[].label'

# Compare with previous period
opencode-usage run --since 7d --compare

insights — LLM-powered analysis

Analyze your OpenCode sessions and generate an HTML report with workflow insights, friction patterns, agent performance, and actionable suggestions.

# Interactive model picker
opencode-usage insights

# Specific model
opencode-usage insights --model gpt-4o-mini

# Customize analysis
opencode-usage insights --days 30 --concurrency 4 --output report.html

# Force re-analysis (ignore cache)
opencode-usage insights --force

Requires an API key for the LLM provider — set via environment variable (e.g. OPENAI_API_KEY) or OpenCode's auth.json.

Example output

╭──────────────── OpenCode Usage — Last 7 days ────────────────╮
│   Calls: 1,280  │  Tokens: 52.3M  │  Cost: $0.00             │
╰──────────────────────────────────────────────────────────────╯

               Usage by Agent (Last 7 days)
┏━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
┃ Agent                  ┃ Model        ┃ Calls ┃   Total ┃    Cost ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
│ build                  │ deepseek-r1  │   420 │   18.2M │       - │
│                        │ qwen-3-coder │   105 │    4.7M │       - │
├────────────────────────┼──────────────┼───────┼─────────┼─────────┤
│ explore                │ gemma-3      │   310 │   12.5M │       - │
│                        │ minimax-m2.5 │   198 │    8.1M │       - │
├────────────────────────┼──────────────┼───────┼─────────┼─────────┤
│ librarian              │ llama-4      │   156 │    5.8M │       - │
├────────────────────────┼──────────────┼───────┼─────────┼─────────┤
│ oracle                 │ qwen-3-coder │    91 │    3.0M │       - │
└────────────────────────┴──────────────┴───────┴─────────┴─────────┘

Configuration

Environment Variable Description
OPENCODE_DB Override database path (default: auto-detected per platform)
NO_COLOR Disable colored output when set (see no-color.org)
{PROVIDER}_API_KEY API key for insights LLM provider (e.g. OPENAI_API_KEY)
{PROVIDER}_BASE_URL Base URL override for insights LLM provider

Default database locations:

  • All platforms (macOS, Linux, Windows): ~/.local/share/opencode/opencode.db

Development

git clone https://github.com/rchardx/opencode-usage.git
cd opencode-usage
uv sync

# Lint & format
uvx ruff check .
uvx ruff format .

# Install pre-commit hooks
uvx pre-commit install

License

MIT

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

opencode_usage-0.2.4.tar.gz (32.9 kB view details)

Uploaded Source

Built Distribution

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

opencode_usage-0.2.4-py3-none-any.whl (40.0 kB view details)

Uploaded Python 3

File details

Details for the file opencode_usage-0.2.4.tar.gz.

File metadata

  • Download URL: opencode_usage-0.2.4.tar.gz
  • Upload date:
  • Size: 32.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for opencode_usage-0.2.4.tar.gz
Algorithm Hash digest
SHA256 e639d245c13669ca0a1e0d72b89628527f4c00239ee1958dbd837d735f1c3b55
MD5 139ee014eabedf5350236c842c341fee
BLAKE2b-256 6ccfe530326096877000847256cbb4d0fd066121994a2ddcb847fa18588a7273

See more details on using hashes here.

File details

Details for the file opencode_usage-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: opencode_usage-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 40.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for opencode_usage-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d5ff9be7fc896138f292ca441c1e4c8ad215ac56166b107c88695dcf2be94f05
MD5 c728f4730aff23548cfb5d48cbce07f2
BLAKE2b-256 43966aafc13a831d6281e95cb986cf59d9e77b56eda3e0534a72966fa9b2caa7

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