opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat(tui): add /usage command and sidebar usage section

Open improdead opened this issue 1 week ago • 9 comments

Summary

This PR adds the ability for users to view their AI provider quota usage directly within OpenCode. Users can now easily monitor how much of their rate limits they've consumed across different providers like OpenAI, Anthropic, GitHub Copilot, and Google/Antigravity.

With more and more people using plugins like GitHub Copilot and Antigravity (Google), having visibility into rate limits has become increasingly important. This feature helps users avoid hitting quota limits unexpectedly during their coding sessions.

What's New

/usage Command A new dialog that shows detailed usage information for your current AI provider:

  • Visual progress bars showing quota consumption
  • Reset time countdowns (e.g., "Resets in 2h 30m")
  • Toggle between "used" and "remaining" views with the Tab key
  • Special handling for GitHub Copilot with built-in device flow authentication
  • Shows "Unlimited" for pay-per-use providers like OpenCode Zen

Sidebar Usage Section A compact usage display in the sidebar that:

  • Shows the current provider's quota at a glance
  • Automatically refreshes after each assistant turn completes
  • Refreshes when you switch to a different model/provider
  • Can be hidden by clicking the ✕ button
  • Can be restored by clicking "Show usage" in the Context section
  • Only displays when usage data is available (no errors shown if provider doesn't support it)

Supported Providers

Provider API Endpoint Notes
OpenAI chatgpt.com/backend-api/wham/usage ChatGPT Plus/Pro quotas
Anthropic api.anthropic.com/api/oauth/usage Claude rate limits
GitHub Copilot api.github.com/copilot_internal/user Uses separate device flow auth
Google/Antigravity Google cloudcode API Vertex AI quotas

GitHub Copilot

GitHub Copilot requires a separate authentication flow since usage data isn't accessible through the regular Copilot tokens. When you first try to view Copilot usage, you'll be prompted to authenticate with a device code flow using a Copilot-specific client ID. The token is stored separately at ~/.local/share/opencode/usage-copilot.json so it doesn't interfere with your regular Copilot authentication.

Antigravity (Google)

With Antigravity becoming a popular choice for accessing Google's models through OpenCode, this PR adds support for fetching usage data from Google's cloudcode API. This gives Antigravity users the same visibility into their quota consumption as other providers.

Screenshots

Usage Dialog Usage dialog showing provider quota

Sidebar Usage Section Sidebar with usage section

Sidebar Usage (Copilot) Sidebar showing Copilot usage

Toggle View (Remaining) Usage dialog showing remaining quota

Copilot Authentication Flow Copilot device flow authentication

Implementation Details

  • Created a new @/usage module with provider-specific implementations
  • Each provider gracefully handles 401/403 errors without crashing
  • Copilot uses its own client ID (Iv1.b507a08c87ecfe98) and stores tokens separately
  • Sidebar usage can be dismissed and restored via KV store preference
  • Sidebar usage refreshes when switching models/providers

Closes #9281

improdead avatar Jan 18 '26 22:01 improdead

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

Found potential related PRs:

  1. PR #6905 - feat: display Anthropic and OpenAI OAuth usage in status dialog and sidebar

    • https://github.com/anomalyco/opencode/pull/6905
    • This PR already implements similar functionality for displaying usage in a sidebar and dialog, specifically for Anthropic and OpenAI providers
  2. PR #7837 - feat: add usage tracking API and /usage dialog

    • https://github.com/anomalyco/opencode/pull/7837
    • This PR appears to add usage tracking and a /usage dialog, which overlaps with the core feature of PR #9301
  3. PR #9069 - feat: Multi-Account OAuth Rotation with Settings UI and CLI Enhancements

    • https://github.com/anomalyco/opencode/pull/9069
    • May be related to OAuth handling for provider authentication

Why they're related: PRs #6905 and #7837 appear to address the same core feature set - displaying AI provider usage information through both a /usage command dialog and sidebar integration. You may want to review these older PRs to understand if they were closed/abandoned, superseded, or if this PR is building upon them.

github-actions[bot] avatar Jan 18 '26 22:01 github-actions[bot]

@thdxr hey Dax added /usage like in claude code for all major connectors please check it out man

improdead avatar Jan 18 '26 22:01 improdead

tested github-copilot. banger

stevoland avatar Jan 18 '26 22:01 stevoland

can someone test claude I dont have it lol

improdead avatar Jan 18 '26 23:01 improdead

@improdead lol i created my issue in preparation of my own pr which i'm currently testing, because the previous prs didn't have linked issues. i'll check out your implementation

CasualDeveloper avatar Jan 18 '26 23:01 CasualDeveloper

@improdead lol i created my issue in preparation of my own pr which i'm currently testing, because the previous prs didn't have linked issues. i'll check out your implementation

oh lol

improdead avatar Jan 18 '26 23:01 improdead

Duplicate of #7837 . May need to be combined.

Mirrowel avatar Jan 19 '26 00:01 Mirrowel

Duplicate of #7837 . May need to be combined. thanks for mentioning I took at look it shows the quota of all models at once all together while I show quota of the model user is currently using I think it is more convenient that way

improdead avatar Jan 19 '26 00:01 improdead

Tried this on codex mode. Works great! Good job

madebyshaurya avatar Jan 19 '26 01:01 madebyshaurya

Hey @improdead — I took a whack at my own implementation to close #9281 via an alternate approach in #9545.

Key differences:

  • /usage is server-side SSOT (GET /usage) vs client-driven fetchers
  • Config option tui.show_usage_scope for sidebar scope control
  • /usage --current|--all flags to override config
  • Claude token auto-refresh on expiry (mirrors anthropic plugin behavior)
  • Copilot usage token stored in auth.json under usage field

Thanks for the initial exploration!

@rekram1-node Happy to accept and close if either PR covers the use case.

CasualDeveloper avatar Jan 20 '26 04:01 CasualDeveloper