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 2 days 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