Skip to content

Latest commit

ย 

History

History
139 lines (102 loc) ยท 5.28 KB

File metadata and controls

139 lines (102 loc) ยท 5.28 KB

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