Skip to content

feat: implement stats command#3832

Merged
rekram1-node merged 2 commits intoanomalyco:devfrom
kaanmertkoc:feat/display-usage-stats
Nov 4, 2025
Merged

feat: implement stats command#3832
rekram1-node merged 2 commits intoanomalyco:devfrom
kaanmertkoc:feat/display-usage-stats

Conversation

@kaanmertkoc
Copy link
Copy Markdown
Contributor

Description

Closes #1634

Implements the /stats command to aggregate token usage and costs across all sessions.

Usage

opencode stats --days 7

What it shows

  • Overview: Session count, total messages, time range
  • Costs & Tokens: Total cost, cost per day, input/output tokens, cache reads/writes
  • Tool Usage: Breakdown of which tools were used most frequently

Implementation

  • Fetches all sessions and projects
  • Processes in batches of 20 to avoid memory overhead
  • Filters by date range with --days flag (default: 7 days)
  • Works with existing storage, no schema changes needed
  • Shows warning for >100 sessions

Performance

Tested on M4 Pro, 24GB RAM:

  • 14 sessions, ~1.5k messages = ~1.3s

Unsure how it scales with 100+ or 1000+ sessions.

Feedback welcome if anyone's willing to test at scale.

Screenshots

Screenshot 2025-11-03 at 20 40 23 Screenshot 2025-11-03 at 20 40 37

Comment thread packages/opencode/src/cli/cmd/stats.ts Outdated
Comment thread packages/opencode/src/cli/cmd/stats.ts Outdated
Comment on lines +34 to +38
return yargs.option("days", {
describe: "show stats for the last N days (default: 7)",
type: "number",
default: 7,
})
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we default to all time, also can we have some breakdown per project? Or have a global or some other flag

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Removed the default days to all time.

Quick question about the project breakdown. I've implemented an --all flag that toggles between current project and all projects. But I'm wondering if you wanted a more detailed breakdown showing stats for each project separately?

Current implementation:

  • opencode stats → current project only
  • opencode stats --all → all projects combined

Let me know if you'd prefer a different approach!

@rekram1-node
Copy link
Copy Markdown
Collaborator

Screenshot 2025-11-03 at 2 19 32 PM

May need some tool name aligning

@rekram1-node
Copy link
Copy Markdown
Collaborator

there have definitely been context7 calls in the past year I think I had some yesterday are we hiding some number of tools?

@kaanmertkoc kaanmertkoc force-pushed the feat/display-usage-stats branch from c69a308 to 9edc1ed Compare November 3, 2025 21:59
@kaanmertkoc
Copy link
Copy Markdown
Contributor Author

there have definitely been context7 calls in the past year I think I had some yesterday are we hiding some number of tools?

Yes! The original code was limiting this to top 10 tools with .slice(0, 10), which was cutting off context7. I bumped it to 20 temporarily, but that's not really a solution.

How should we approach to this?

@kaanmertkoc
Copy link
Copy Markdown
Contributor Author

Screenshot 2025-11-03 at 2 19 32 PM May need some tool name aligning

Thanks for catching that. I fixed the alignment by truncating long tool names with ".." so the table lines up properly now.

Please let me know what do you think about this approach.

Screenshot 2025-11-04 at 00 45 29

@rekram1-node
Copy link
Copy Markdown
Collaborator

we could add some flag for # of tools and default to all

Comment thread packages/opencode/src/cli/cmd/stats.ts Outdated
Comment on lines +40 to +44
.option("all", {
describe: "show stats across all projects (default: current project only)",
type: "boolean",
default: false,
})
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make this a project flag, that is a string,

if not added:

  • all

if added but empty:

  • current project

if added and has a value:

  • use that project id

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, changed it to --project flag with the behavior you described:

  • Not provided → all projects
  • Empty string → current project (opencode stats --project "")
  • Specific value → that project ID (opencode stats --project <id>)

Let me know if this works!

@kaanmertkoc kaanmertkoc force-pushed the feat/display-usage-stats branch from 9edc1ed to fb3b75a Compare November 3, 2025 22:48
@kaanmertkoc
Copy link
Copy Markdown
Contributor Author

we could add some flag for # of tools and default to all

Done! Added --tools flag that accepts a number. Defaults to showing all tools if not specified.

Usage:

  • opencode stats → shows all tools
  • opencode stats --tools 10 → shows top 10 tools

@rekram1-node
Copy link
Copy Markdown
Collaborator

looking good! should be good to merge when i get back from grocery store

@rekram1-node rekram1-node merged commit 63862b1 into anomalyco:dev Nov 4, 2025
3 checks passed
xywsxp pushed a commit to xywsxp/opencode that referenced this pull request Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Track Overall Token Usage (Usage Stats)

2 participants