opencode-usage-v2 0.2.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opencode_usage_v2-0.2.5/.gitignore +30 -0
- opencode_usage_v2-0.2.5/LICENSE +22 -0
- opencode_usage_v2-0.2.5/PKG-INFO +217 -0
- opencode_usage_v2-0.2.5/README.md +190 -0
- opencode_usage_v2-0.2.5/pyproject.toml +100 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/__init__.py +18 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/__main__.py +5 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/_opencode_cli.py +158 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/auth.py +82 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/cli.py +271 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/db.py +712 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/insights/__init__.py +19 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/insights/analyze.py +384 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/insights/cache.py +60 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/insights/extract.py +600 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/insights/orchestrator.py +159 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/insights/prompts.py +306 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/insights/report.py +574 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/insights/types.py +75 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/llm.py +93 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/models.py +145 -0
- opencode_usage_v2-0.2.5/src/opencode_usage/render.py +240 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.egg-info/
|
|
6
|
+
*.egg
|
|
7
|
+
dist/
|
|
8
|
+
build/
|
|
9
|
+
|
|
10
|
+
# Virtual environments
|
|
11
|
+
.venv/
|
|
12
|
+
venv/
|
|
13
|
+
env/
|
|
14
|
+
|
|
15
|
+
# IDE
|
|
16
|
+
.idea/
|
|
17
|
+
.vscode/
|
|
18
|
+
*.swp
|
|
19
|
+
*.swo
|
|
20
|
+
*~
|
|
21
|
+
|
|
22
|
+
# OS
|
|
23
|
+
.DS_Store
|
|
24
|
+
Thumbs.db
|
|
25
|
+
|
|
26
|
+
# Sisyphus working files
|
|
27
|
+
.sisyphus/
|
|
28
|
+
|
|
29
|
+
# Generated reports
|
|
30
|
+
opencode-insights.html
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 rchardx
|
|
4
|
+
Copyright (c) 2026 fcomovaz
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: opencode-usage-v2
|
|
3
|
+
Version: 0.2.5
|
|
4
|
+
Summary: CLI tool to track and display OpenCode token usage statistics (v1 and v2)
|
|
5
|
+
Project-URL: Changelog, https://github.com/fcomovaz/opencode-usage-v2-supp/blob/main/CHANGELOG.md
|
|
6
|
+
Project-URL: Homepage, https://github.com/fcomovaz/opencode-usage-v2-supp
|
|
7
|
+
Project-URL: Issues, https://github.com/fcomovaz/opencode-usage-v2-supp/issues
|
|
8
|
+
Project-URL: Repository, https://github.com/fcomovaz/opencode-usage-v2-supp
|
|
9
|
+
Author: rchardx
|
|
10
|
+
Maintainer: fcomovaz
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: analytics,cli,opencode,sqlite,tokens,usage
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Environment :: Console
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Utilities
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Requires-Dist: rich>=13.0
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# opencode-usage-v2
|
|
29
|
+
|
|
30
|
+
[](https://github.com/fcomovaz/opencode-usage-v2-supp/actions/workflows/ci.yml)
|
|
31
|
+
[](https://pypi.org/project/opencode-usage-v2/)
|
|
32
|
+
[](https://pypi.org/project/opencode-usage-v2/)
|
|
33
|
+
[](LICENSE)
|
|
34
|
+
|
|
35
|
+
> **Maintained fork:** `opencode-usage-v2` is a maintained fork of the deleted upstream `opencode-usage` repository (`rchardx/opencode-usage`), updated with full support for OpenCode v1, OpenCode v2, and migrated mixed SQLite schemas.
|
|
36
|
+
|
|
37
|
+
CLI tool to track and display [OpenCode](https://github.com/opencodeco/opencode) token usage statistics. Reads directly from OpenCode's local SQLite database — no API keys or external services needed for basic usage.
|
|
38
|
+
|
|
39
|
+
## Features
|
|
40
|
+
|
|
41
|
+
- **OpenCode v1 & v2 support** — reads legacy `message`/`session`, current `session_message`/`session_v2`, and migrated mixed databases without double-counting
|
|
42
|
+
- **Daily breakdown** — token usage and cost per day
|
|
43
|
+
- **Group by dimension** — model, agent, provider, or session
|
|
44
|
+
- **Agent × Model view** — see which model each agent uses
|
|
45
|
+
- **Time filtering** — last N days, relative durations (`7d`, `2w`), or ISO dates
|
|
46
|
+
- **Period comparison** — compare current vs previous period with `--compare`
|
|
47
|
+
- **JSON output** — pipe to `jq` or other tools
|
|
48
|
+
- **LLM-powered insights** — analyze session transcripts and generate a self-contained HTML report
|
|
49
|
+
- **Cross-platform** — macOS, Linux, Windows
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
### From PyPI (Recommended)
|
|
54
|
+
|
|
55
|
+
Run directly without installing using `uvx`:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
uvx opencode-usage-v2
|
|
59
|
+
uvx opencode-usage-v2 run --by model
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Or install globally / into a virtual environment:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# With uv
|
|
66
|
+
uv tool install opencode-usage-v2
|
|
67
|
+
|
|
68
|
+
# With pip
|
|
69
|
+
pip install opencode-usage-v2
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Installing `opencode-usage-v2` exposes both `opencode-usage-v2` and `opencode-usage` CLI commands (drop-in compatible).
|
|
73
|
+
|
|
74
|
+
> **Warning:** Do **not** install `opencode-usage-v2` into the same virtual environment as the legacy `opencode-usage` package. Both distributions provide the `opencode_usage` Python namespace and `opencode-usage` script, so co-installing them in one environment causes file conflicts. Uninstall `opencode-usage` first if upgrading in an existing environment.
|
|
75
|
+
|
|
76
|
+
### From source (local development)
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
# Clone repository
|
|
80
|
+
git clone https://github.com/fcomovaz/opencode-usage-v2-supp.git
|
|
81
|
+
cd opencode-usage-v2-supp
|
|
82
|
+
|
|
83
|
+
# Option 1: with standard Python venv & pip
|
|
84
|
+
python -m venv .venv
|
|
85
|
+
# macOS / Linux:
|
|
86
|
+
source .venv/bin/activate
|
|
87
|
+
# Windows (PowerShell):
|
|
88
|
+
.venv\Scripts\Activate.ps1
|
|
89
|
+
# Windows (Command Prompt):
|
|
90
|
+
.venv\Scripts\activate.bat
|
|
91
|
+
pip install -e .
|
|
92
|
+
|
|
93
|
+
# Option 2: with uv
|
|
94
|
+
uv sync
|
|
95
|
+
uv tool install -e .
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Usage
|
|
99
|
+
|
|
100
|
+
Both `opencode-usage-v2` and `opencode-usage` invoke the same CLI, which has two subcommands: `run` (default) and `insights`. You can also invoke it as `python -m opencode_usage`.
|
|
101
|
+
|
|
102
|
+
### `run` — Token usage statistics
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
# Default: last 7 days, daily breakdown
|
|
106
|
+
opencode-usage-v2
|
|
107
|
+
|
|
108
|
+
# Time filtering
|
|
109
|
+
opencode-usage-v2 run --days 30
|
|
110
|
+
opencode-usage-v2 run --since 7d
|
|
111
|
+
opencode-usage-v2 run --since 2025-01-01
|
|
112
|
+
|
|
113
|
+
# Group by dimension
|
|
114
|
+
opencode-usage-v2 run --by model
|
|
115
|
+
opencode-usage-v2 run --by agent # shows model per agent
|
|
116
|
+
opencode-usage-v2 run --by provider
|
|
117
|
+
opencode-usage-v2 run --by session --limit 10
|
|
118
|
+
|
|
119
|
+
# JSON output
|
|
120
|
+
opencode-usage-v2 run --json
|
|
121
|
+
opencode-usage-v2 run --by model --json | jq '.rows[].label'
|
|
122
|
+
|
|
123
|
+
# Compare with previous period
|
|
124
|
+
opencode-usage-v2 run --since 7d --compare
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### `insights` — LLM-powered analysis
|
|
128
|
+
|
|
129
|
+
Analyze your OpenCode sessions and generate an HTML report with workflow insights, friction patterns, agent performance, and actionable suggestions.
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
# Interactive model picker
|
|
133
|
+
opencode-usage-v2 insights
|
|
134
|
+
|
|
135
|
+
# Specific model
|
|
136
|
+
opencode-usage-v2 insights --model gpt-4o-mini
|
|
137
|
+
|
|
138
|
+
# Customize analysis
|
|
139
|
+
opencode-usage-v2 insights --days 30 --concurrency 4 --output report.html
|
|
140
|
+
|
|
141
|
+
# Force re-analysis (ignore cache)
|
|
142
|
+
opencode-usage-v2 insights --force
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Requires an API key for the LLM provider — set via environment variable (e.g. `OPENAI_API_KEY`) or OpenCode's `auth.json`.
|
|
146
|
+
|
|
147
|
+
> **Note:** The `insights` extraction queries currently target the legacy v1 schema tables (`session`, `message`, `part`). Token usage reporting (`run`) supports both v1 and v2 schemas.
|
|
148
|
+
|
|
149
|
+
### Example output
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
╭──────────────── OpenCode Usage — Last 7 days ────────────────╮
|
|
153
|
+
│ Calls: 1,280 │ Tokens: 52.3M │ Cost: $0.00 │
|
|
154
|
+
╰──────────────────────────────────────────────────────────────╯
|
|
155
|
+
|
|
156
|
+
Usage by Agent (Last 7 days)
|
|
157
|
+
┏━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
|
|
158
|
+
┃ Agent ┃ Model ┃ Calls ┃ Total ┃ Cost ┃
|
|
159
|
+
┡━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
|
|
160
|
+
│ build │ deepseek-r1 │ 420 │ 18.2M │ - │
|
|
161
|
+
│ │ qwen-3-coder │ 105 │ 4.7M │ - │
|
|
162
|
+
├────────────────────────┼──────────────┼───────┼─────────┼─────────┤
|
|
163
|
+
│ explore │ gemma-3 │ 310 │ 12.5M │ - │
|
|
164
|
+
│ │ minimax-m2.5 │ 198 │ 8.1M │ - │
|
|
165
|
+
├────────────────────────┼──────────────┼───────┼─────────┼─────────┤
|
|
166
|
+
│ librarian │ llama-4 │ 156 │ 5.8M │ - │
|
|
167
|
+
├────────────────────────┼──────────────┼───────┼─────────┤
|
|
168
|
+
│ oracle │ qwen-3-coder │ 91 │ 3.0M │ - │
|
|
169
|
+
└────────────────────────┴──────────────┴───────┴─────────┴─────────┘
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Schema Support
|
|
173
|
+
|
|
174
|
+
`opencode-usage-v2` supports both OpenCode v1 and v2 database schemas, as well as migrated (mixed) databases:
|
|
175
|
+
|
|
176
|
+
| OpenCode Version | Session Table | Message Table | Status |
|
|
177
|
+
|---|---|---|---|
|
|
178
|
+
| **v1** (legacy) | `session` | `message` (`role = 'assistant'`) | Supported |
|
|
179
|
+
| **v2** (current) | `session_v2` | `session_message` (`type = 'assistant'`) | Supported |
|
|
180
|
+
| **Mixed / Migrated** | Both tables present | Both tables present | Supported (deduplicated by ID) |
|
|
181
|
+
|
|
182
|
+
In mixed databases migrated from v1 to v2, messages present in both tables are automatically deduplicated by message ID to prevent double-counting.
|
|
183
|
+
|
|
184
|
+
## Configuration
|
|
185
|
+
|
|
186
|
+
| Environment Variable | Description |
|
|
187
|
+
|---|---|
|
|
188
|
+
| `OPENCODE_DB` | Override database path (default: auto-detected per platform) |
|
|
189
|
+
| `NO_COLOR` | Disable colored output when set (see [no-color.org](https://no-color.org)) |
|
|
190
|
+
| `{PROVIDER}_API_KEY` | API key for insights LLM provider (e.g. `OPENAI_API_KEY`) |
|
|
191
|
+
| `{PROVIDER}_BASE_URL` | Base URL override for insights LLM provider |
|
|
192
|
+
|
|
193
|
+
Default database locations:
|
|
194
|
+
|
|
195
|
+
- **All platforms** (macOS, Linux, Windows): `~/.local/share/opencode/opencode.db`
|
|
196
|
+
|
|
197
|
+
## Development
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
git clone https://github.com/fcomovaz/opencode-usage-v2-supp.git
|
|
201
|
+
cd opencode-usage-v2-supp
|
|
202
|
+
uv sync
|
|
203
|
+
|
|
204
|
+
# Run tests
|
|
205
|
+
uv run pytest
|
|
206
|
+
|
|
207
|
+
# Lint & format
|
|
208
|
+
uvx ruff check .
|
|
209
|
+
uvx ruff format --check .
|
|
210
|
+
|
|
211
|
+
# Install pre-commit hooks
|
|
212
|
+
uvx pre-commit install
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## License
|
|
216
|
+
|
|
217
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# opencode-usage-v2
|
|
2
|
+
|
|
3
|
+
[](https://github.com/fcomovaz/opencode-usage-v2-supp/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/opencode-usage-v2/)
|
|
5
|
+
[](https://pypi.org/project/opencode-usage-v2/)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
> **Maintained fork:** `opencode-usage-v2` is a maintained fork of the deleted upstream `opencode-usage` repository (`rchardx/opencode-usage`), updated with full support for OpenCode v1, OpenCode v2, and migrated mixed SQLite schemas.
|
|
9
|
+
|
|
10
|
+
CLI tool to track and display [OpenCode](https://github.com/opencodeco/opencode) token usage statistics. Reads directly from OpenCode's local SQLite database — no API keys or external services needed for basic usage.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **OpenCode v1 & v2 support** — reads legacy `message`/`session`, current `session_message`/`session_v2`, and migrated mixed databases without double-counting
|
|
15
|
+
- **Daily breakdown** — token usage and cost per day
|
|
16
|
+
- **Group by dimension** — model, agent, provider, or session
|
|
17
|
+
- **Agent × Model view** — see which model each agent uses
|
|
18
|
+
- **Time filtering** — last N days, relative durations (`7d`, `2w`), or ISO dates
|
|
19
|
+
- **Period comparison** — compare current vs previous period with `--compare`
|
|
20
|
+
- **JSON output** — pipe to `jq` or other tools
|
|
21
|
+
- **LLM-powered insights** — analyze session transcripts and generate a self-contained HTML report
|
|
22
|
+
- **Cross-platform** — macOS, Linux, Windows
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
### From PyPI (Recommended)
|
|
27
|
+
|
|
28
|
+
Run directly without installing using `uvx`:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
uvx opencode-usage-v2
|
|
32
|
+
uvx opencode-usage-v2 run --by model
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Or install globally / into a virtual environment:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# With uv
|
|
39
|
+
uv tool install opencode-usage-v2
|
|
40
|
+
|
|
41
|
+
# With pip
|
|
42
|
+
pip install opencode-usage-v2
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Installing `opencode-usage-v2` exposes both `opencode-usage-v2` and `opencode-usage` CLI commands (drop-in compatible).
|
|
46
|
+
|
|
47
|
+
> **Warning:** Do **not** install `opencode-usage-v2` into the same virtual environment as the legacy `opencode-usage` package. Both distributions provide the `opencode_usage` Python namespace and `opencode-usage` script, so co-installing them in one environment causes file conflicts. Uninstall `opencode-usage` first if upgrading in an existing environment.
|
|
48
|
+
|
|
49
|
+
### From source (local development)
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Clone repository
|
|
53
|
+
git clone https://github.com/fcomovaz/opencode-usage-v2-supp.git
|
|
54
|
+
cd opencode-usage-v2-supp
|
|
55
|
+
|
|
56
|
+
# Option 1: with standard Python venv & pip
|
|
57
|
+
python -m venv .venv
|
|
58
|
+
# macOS / Linux:
|
|
59
|
+
source .venv/bin/activate
|
|
60
|
+
# Windows (PowerShell):
|
|
61
|
+
.venv\Scripts\Activate.ps1
|
|
62
|
+
# Windows (Command Prompt):
|
|
63
|
+
.venv\Scripts\activate.bat
|
|
64
|
+
pip install -e .
|
|
65
|
+
|
|
66
|
+
# Option 2: with uv
|
|
67
|
+
uv sync
|
|
68
|
+
uv tool install -e .
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Usage
|
|
72
|
+
|
|
73
|
+
Both `opencode-usage-v2` and `opencode-usage` invoke the same CLI, which has two subcommands: `run` (default) and `insights`. You can also invoke it as `python -m opencode_usage`.
|
|
74
|
+
|
|
75
|
+
### `run` — Token usage statistics
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# Default: last 7 days, daily breakdown
|
|
79
|
+
opencode-usage-v2
|
|
80
|
+
|
|
81
|
+
# Time filtering
|
|
82
|
+
opencode-usage-v2 run --days 30
|
|
83
|
+
opencode-usage-v2 run --since 7d
|
|
84
|
+
opencode-usage-v2 run --since 2025-01-01
|
|
85
|
+
|
|
86
|
+
# Group by dimension
|
|
87
|
+
opencode-usage-v2 run --by model
|
|
88
|
+
opencode-usage-v2 run --by agent # shows model per agent
|
|
89
|
+
opencode-usage-v2 run --by provider
|
|
90
|
+
opencode-usage-v2 run --by session --limit 10
|
|
91
|
+
|
|
92
|
+
# JSON output
|
|
93
|
+
opencode-usage-v2 run --json
|
|
94
|
+
opencode-usage-v2 run --by model --json | jq '.rows[].label'
|
|
95
|
+
|
|
96
|
+
# Compare with previous period
|
|
97
|
+
opencode-usage-v2 run --since 7d --compare
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### `insights` — LLM-powered analysis
|
|
101
|
+
|
|
102
|
+
Analyze your OpenCode sessions and generate an HTML report with workflow insights, friction patterns, agent performance, and actionable suggestions.
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
# Interactive model picker
|
|
106
|
+
opencode-usage-v2 insights
|
|
107
|
+
|
|
108
|
+
# Specific model
|
|
109
|
+
opencode-usage-v2 insights --model gpt-4o-mini
|
|
110
|
+
|
|
111
|
+
# Customize analysis
|
|
112
|
+
opencode-usage-v2 insights --days 30 --concurrency 4 --output report.html
|
|
113
|
+
|
|
114
|
+
# Force re-analysis (ignore cache)
|
|
115
|
+
opencode-usage-v2 insights --force
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Requires an API key for the LLM provider — set via environment variable (e.g. `OPENAI_API_KEY`) or OpenCode's `auth.json`.
|
|
119
|
+
|
|
120
|
+
> **Note:** The `insights` extraction queries currently target the legacy v1 schema tables (`session`, `message`, `part`). Token usage reporting (`run`) supports both v1 and v2 schemas.
|
|
121
|
+
|
|
122
|
+
### Example output
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
╭──────────────── OpenCode Usage — Last 7 days ────────────────╮
|
|
126
|
+
│ Calls: 1,280 │ Tokens: 52.3M │ Cost: $0.00 │
|
|
127
|
+
╰──────────────────────────────────────────────────────────────╯
|
|
128
|
+
|
|
129
|
+
Usage by Agent (Last 7 days)
|
|
130
|
+
┏━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
|
|
131
|
+
┃ Agent ┃ Model ┃ Calls ┃ Total ┃ Cost ┃
|
|
132
|
+
┡━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
|
|
133
|
+
│ build │ deepseek-r1 │ 420 │ 18.2M │ - │
|
|
134
|
+
│ │ qwen-3-coder │ 105 │ 4.7M │ - │
|
|
135
|
+
├────────────────────────┼──────────────┼───────┼─────────┼─────────┤
|
|
136
|
+
│ explore │ gemma-3 │ 310 │ 12.5M │ - │
|
|
137
|
+
│ │ minimax-m2.5 │ 198 │ 8.1M │ - │
|
|
138
|
+
├────────────────────────┼──────────────┼───────┼─────────┼─────────┤
|
|
139
|
+
│ librarian │ llama-4 │ 156 │ 5.8M │ - │
|
|
140
|
+
├────────────────────────┼──────────────┼───────┼─────────┤
|
|
141
|
+
│ oracle │ qwen-3-coder │ 91 │ 3.0M │ - │
|
|
142
|
+
└────────────────────────┴──────────────┴───────┴─────────┴─────────┘
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Schema Support
|
|
146
|
+
|
|
147
|
+
`opencode-usage-v2` supports both OpenCode v1 and v2 database schemas, as well as migrated (mixed) databases:
|
|
148
|
+
|
|
149
|
+
| OpenCode Version | Session Table | Message Table | Status |
|
|
150
|
+
|---|---|---|---|
|
|
151
|
+
| **v1** (legacy) | `session` | `message` (`role = 'assistant'`) | Supported |
|
|
152
|
+
| **v2** (current) | `session_v2` | `session_message` (`type = 'assistant'`) | Supported |
|
|
153
|
+
| **Mixed / Migrated** | Both tables present | Both tables present | Supported (deduplicated by ID) |
|
|
154
|
+
|
|
155
|
+
In mixed databases migrated from v1 to v2, messages present in both tables are automatically deduplicated by message ID to prevent double-counting.
|
|
156
|
+
|
|
157
|
+
## Configuration
|
|
158
|
+
|
|
159
|
+
| Environment Variable | Description |
|
|
160
|
+
|---|---|
|
|
161
|
+
| `OPENCODE_DB` | Override database path (default: auto-detected per platform) |
|
|
162
|
+
| `NO_COLOR` | Disable colored output when set (see [no-color.org](https://no-color.org)) |
|
|
163
|
+
| `{PROVIDER}_API_KEY` | API key for insights LLM provider (e.g. `OPENAI_API_KEY`) |
|
|
164
|
+
| `{PROVIDER}_BASE_URL` | Base URL override for insights LLM provider |
|
|
165
|
+
|
|
166
|
+
Default database locations:
|
|
167
|
+
|
|
168
|
+
- **All platforms** (macOS, Linux, Windows): `~/.local/share/opencode/opencode.db`
|
|
169
|
+
|
|
170
|
+
## Development
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
git clone https://github.com/fcomovaz/opencode-usage-v2-supp.git
|
|
174
|
+
cd opencode-usage-v2-supp
|
|
175
|
+
uv sync
|
|
176
|
+
|
|
177
|
+
# Run tests
|
|
178
|
+
uv run pytest
|
|
179
|
+
|
|
180
|
+
# Lint & format
|
|
181
|
+
uvx ruff check .
|
|
182
|
+
uvx ruff format --check .
|
|
183
|
+
|
|
184
|
+
# Install pre-commit hooks
|
|
185
|
+
uvx pre-commit install
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## License
|
|
189
|
+
|
|
190
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = "hatchling.build"
|
|
3
|
+
requires = ["hatchling>=1.27"]
|
|
4
|
+
|
|
5
|
+
[dependency-groups]
|
|
6
|
+
dev = ["pytest>=7.0"]
|
|
7
|
+
|
|
8
|
+
[project]
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "rchardx" },
|
|
11
|
+
]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 4 - Beta",
|
|
14
|
+
"Environment :: Console",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.10",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Topic :: Utilities",
|
|
23
|
+
]
|
|
24
|
+
dependencies = [
|
|
25
|
+
"rich>=13.0",
|
|
26
|
+
]
|
|
27
|
+
description = "CLI tool to track and display OpenCode token usage statistics (v1 and v2)"
|
|
28
|
+
keywords = ["opencode", "cli", "tokens", "usage", "analytics", "sqlite"]
|
|
29
|
+
license = "MIT"
|
|
30
|
+
license-files = ["LICENSE"]
|
|
31
|
+
maintainers = [
|
|
32
|
+
{ name = "fcomovaz" },
|
|
33
|
+
]
|
|
34
|
+
name = "opencode-usage-v2"
|
|
35
|
+
readme = "README.md"
|
|
36
|
+
requires-python = ">=3.10"
|
|
37
|
+
version = "0.2.5"
|
|
38
|
+
|
|
39
|
+
[project.scripts]
|
|
40
|
+
opencode-usage = "opencode_usage.cli:main"
|
|
41
|
+
opencode-usage-v2 = "opencode_usage.cli:main"
|
|
42
|
+
|
|
43
|
+
[project.urls]
|
|
44
|
+
Changelog = "https://github.com/fcomovaz/opencode-usage-v2-supp/blob/main/CHANGELOG.md"
|
|
45
|
+
Homepage = "https://github.com/fcomovaz/opencode-usage-v2-supp"
|
|
46
|
+
Issues = "https://github.com/fcomovaz/opencode-usage-v2-supp/issues"
|
|
47
|
+
Repository = "https://github.com/fcomovaz/opencode-usage-v2-supp"
|
|
48
|
+
|
|
49
|
+
[tool.bandit]
|
|
50
|
+
exclude_dirs = ["tests"]
|
|
51
|
+
skips = ["B310", "B404", "B603", "B607", "B608"]
|
|
52
|
+
|
|
53
|
+
[tool.commitizen]
|
|
54
|
+
major_version_zero = true
|
|
55
|
+
name = "cz_conventional_commits"
|
|
56
|
+
tag_format = "$version"
|
|
57
|
+
update_changelog_on_bump = true
|
|
58
|
+
version_provider = "pep621"
|
|
59
|
+
version_scheme = "pep440"
|
|
60
|
+
|
|
61
|
+
[tool.hatch.build.targets.sdist]
|
|
62
|
+
exclude = [
|
|
63
|
+
".github/**",
|
|
64
|
+
"AGENTS.md",
|
|
65
|
+
"CHANGELOG.md",
|
|
66
|
+
"PR_DESCRIPTION.md",
|
|
67
|
+
"opencode-insights.html",
|
|
68
|
+
"tests/**",
|
|
69
|
+
]
|
|
70
|
+
include = [
|
|
71
|
+
"src/opencode_usage/**",
|
|
72
|
+
"pyproject.toml",
|
|
73
|
+
"README.md",
|
|
74
|
+
"LICENSE",
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
[tool.hatch.build.targets.wheel]
|
|
78
|
+
packages = ["src/opencode_usage"]
|
|
79
|
+
|
|
80
|
+
[tool.pytest.ini_options]
|
|
81
|
+
testpaths = ["tests"]
|
|
82
|
+
|
|
83
|
+
[tool.ruff]
|
|
84
|
+
line-length = 100
|
|
85
|
+
target-version = "py310"
|
|
86
|
+
|
|
87
|
+
[tool.ruff.lint]
|
|
88
|
+
select = [
|
|
89
|
+
"E", # pycodestyle errors
|
|
90
|
+
"W", # pycodestyle warnings
|
|
91
|
+
"F", # pyflakes
|
|
92
|
+
"I", # isort
|
|
93
|
+
"UP", # pyupgrade
|
|
94
|
+
"B", # flake8-bugbear
|
|
95
|
+
"SIM", # flake8-simplify
|
|
96
|
+
"RUF", # ruff-specific
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
[tool.ruff.lint.isort]
|
|
100
|
+
known-first-party = ["opencode_usage"]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""OpenCode usage statistics CLI."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _resolve_version() -> str:
|
|
9
|
+
"""Return installed distribution version, with fallback for legacy or editable setups."""
|
|
10
|
+
for dist_name in ("opencode-usage-v2", "opencode-usage"):
|
|
11
|
+
try:
|
|
12
|
+
return version(dist_name)
|
|
13
|
+
except PackageNotFoundError:
|
|
14
|
+
continue
|
|
15
|
+
return "0.0.0+unknown"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
__version__ = _resolve_version()
|