Skip to content

feat: add token usage metrics and dialog#28456

Closed
mael-klingler wants to merge 3 commits into
anomalyco:devfrom
mael-klingler:token-metrics-usage
Closed

feat: add token usage metrics and dialog#28456
mael-klingler wants to merge 3 commits into
anomalyco:devfrom
mael-klingler:token-metrics-usage

Conversation

@mael-klingler
Copy link
Copy Markdown

@mael-klingler mael-klingler commented May 20, 2026

Issue for this PR

Closes #

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds token usage metrics across three surfaces:

 1. TUI dialog (/usage slash command) — shows total/daily/per-session token breakdown and cost in the terminal UI.                                                                                                                                                                                  
 2. CLI command (opencode usage) — prints token usage summary from either the local DB or the remote console API, with optional --json output.                                                                                                                                                      
 3. Console backend — new /api/usage.json route and queryUsageMetrics server function that aggregate tokens by session/daily/weekly/monthly windows from UsageTable. The trackUsage function in the zen handler now also emits aggregate metric events (                                            
    session total, daily/weekly/monthly sums, estimated tokens-per-second).                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                    
 Also fixes a minor issue in src/index.ts where ResolveMessage was used as a type guard without being imported at runtime — replaced with a duck-typing check. 

How did you verify your code works?

Ran the TUI and opened the /usage dialog; ran opencode usage locally and confirmed token counts match session data.

Screenshots / recordings

If this is a UI change, please include a screenshot or recording.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

If you do not follow this template your PR will be automatically rejected.

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label May 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found two potentially related PRs that may be addressing similar functionality:

  1. PR feat(opencode): first-class opencode insights usage report command #26590 - feat(opencode): first-class opencode insights usage report command

  2. PR feat(usage): unified usage tracking with auth refresh (#9281) #9545 - feat(usage): unified usage tracking with auth refresh

These PRs may be addressing similar or overlapping concerns around token/usage tracking and reporting, though PR #28456 appears to be more comprehensive (adding TUI dialog, CLI command, and console backend routes).

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label May 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@mael-klingler
Copy link
Copy Markdown
Author

Thank you

@mael-klingler
Copy link
Copy Markdown
Author

wie geht es nun weiter

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.

1 participant