Skip to content

Conversation

@CasualDeveloper
Copy link
Contributor

@CasualDeveloper CasualDeveloper commented Jan 20, 2026

Summary

Add /usage as the single source of truth for provider usage data (Codex, Copilot, Claude) with TUI integration and automatic token refresh. Users can now view rate limits via /usage command or sidebar panel, with provider-specific auth flows handling token expiry transparently.

Closes #9281, closes #728
Supersedes #6905, #7837
Alternate implementation to #9301

Changes

Core

  • New packages/opencode/src/usage/index.ts (~850 lines): usage fetchers for Codex (/backend-api/wham/usage), Copilot (/copilot_internal/user), Claude (/api/oauth/usage) with schema validation and caching
  • New packages/opencode/src/server/usage.ts: GET /usage endpoint with ?provider= filter and ?refresh= option
  • Claude token auto-refresh on 401 via console.anthropic.com/v1/oauth/token
  • usage.updated bus event for sidebar sync

Auth

  • packages/opencode/src/cli/cmd/auth.ts: second device-flow prompt during Copilot login using VS Code client ID (Iv1.b507a08c87ecfe98) to capture usage token for copilot_internal/user
  • packages/opencode/src/auth/index.ts: optional usage field in OAuth schema
  • packages/opencode/src/plugin/copilot.ts: attempt service token exchange via copilot_internal/v2/token; fallback to device token
  • packages/opencode/src/provider/auth.ts: persist usage token

TUI

  • New dialog-usage.tsx: renders usage windows with progress bars, reset times, credits/quota
  • prompt/index.tsx: /usage command with --current, --all flags; always fetches fresh data
  • sidebar.tsx: collapsible Usage panel; refreshes on usage.updated events and assistant turn completion
  • packages/opencode/src/config/config.ts: tui.show_usage_scope setting ("current" | "all")

SDK/OpenAPI

  • Regenerated packages/sdk/openapi.json and packages/sdk/js/src/v2/gen/types.gen.ts

Tests

  • New packages/opencode/test/server/usage.test.ts: mock auth scenarios, provider filtering, error handling

AI Assistance

  • Tools: OpenCode + openai/gpt-5.2-codex
  • Testing: Tested on test build (bun turbo typecheck && bun run --cwd packages/opencode test test/server/usage.test.ts && bun run --cwd packages/opencode build)
  • Review: Human operator personally guided and reviewed changes

Notes

  • Baseline test failures (snapshot/skill tests) are unrelated
  • Full bun turbo test not run this session
  • E2E tests for device-flow not added (manual verification)
  • Follow-up: docs for tui.show_usage_scope config

@github-actions
Copy link
Contributor

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

Potential Duplicates Found:

1. PR #9301: feat(tui): add /usage command and sidebar usage section

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

3. PR #9069: feat: Multi-Account OAuth Rotation with Settings UI and CLI Enhancements

@CasualDeveloper CasualDeveloper changed the title feat(usage): unified usage tracking with Copilot/Claude auth refresh [AI-assisted] feat(usage): unified usage tracking with Copilot/Claude auth refresh Jan 20, 2026
Add /usage as the single source of truth for provider usage data with TUI integration and automatic token refresh for Codex, Copilot, and Claude.

Core:
- New usage module with fetchers for each provider's usage endpoint
- GET /usage endpoint with provider filter and refresh options
- Claude token auto-refresh on 401 via console.anthropic.com
- usage.updated bus event for real-time sidebar sync

Auth:
- Second device-flow during Copilot login for copilot_internal/user token
- Optional usage field in OAuth schema for provider-specific tokens
- Service token exchange attempt via copilot_internal/v2/token

TUI:
- /usage command with --current and --all flags (always fetches fresh)
- Usage dialog with progress bars, reset times, credits/quota display
- Sidebar Usage panel refreshes on events and assistant turn completion
- tui.show_usage_scope config setting for default scope

Closes anomalyco#9281, anomalyco#728
Supersedes anomalyco#6905, anomalyco#7837
Alternate to anomalyco#9301
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch from ba7d89c to 9ce0a41 Compare January 20, 2026 11:14
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.

feat: add /usage usage tracking (codex/copilot/claude) /fork

1 participant