Skip to content

feat(usage): unified usage tracking with auth refresh (#9281)#9545

Open
CasualDeveloper wants to merge 1 commit into
anomalyco:devfrom
CasualDeveloper:feat/9281-usage-tracking
Open

feat(usage): unified usage tracking with auth refresh (#9281)#9545
CasualDeveloper wants to merge 1 commit into
anomalyco:devfrom
CasualDeveloper:feat/9281-usage-tracking

Conversation

@CasualDeveloper
Copy link
Copy Markdown
Contributor

@CasualDeveloper CasualDeveloper commented Jan 20, 2026

Summary

Adds built-in usage tracking for OAuth-authenticated providers: Anthropic Claude, GitHub Copilot, and OpenAI ChatGPT.

This PR implements Usage.Service as the shared usage orchestration layer, exposes GET /usage through both the default Hono route and Effect HttpApi adapter, regenerates SDK/OpenAPI support, and adds terminal TUI surfaces. In the terminal TUI, users can view usage through a /usage detail dialog and a sidebar Usage section. /usage is the detailed all-provider view, while the sidebar remains configurable as a glance view.

Web/app UI is intentionally left for a follow-up; client.usage.get(...) is available for that future integration.

Closes #9281
Related: #768
Supersedes #6905, #7837
Alternate implementation to #9301

Behavior

  • /usage refreshes all authenticated usage providers and opens the detail dialog.
  • /usage --used / /usage --remaining select the display mode for that dialog invocation.
  • /usage --background refreshes all provider usage data and updates the shared sidebar resource without opening the dialog.
  • tui.show_usage_provider_scope applies to the sidebar only (current / all).
  • tui.show_usage_value_mode controls the initial used/remaining display mode.
  • Sidebar refreshes after assistant turns and after /usage fetches by explicitly refetching the shared usage resource.

Core

  • Adds Usage.Service as the source of truth for usage orchestration, storage, cache reads/writes, usage.updated events, in-flight fetch dedupe, and response assembly.
  • Adds GET /usage under both packages/opencode/src/server/routes/instance/usage.ts and the Effect HttpApi usage group/handler.
  • Adds provider fetchers under packages/opencode/src/usage/providers/ for Anthropic Claude, GitHub Copilot, and OpenAI ChatGPT.
  • Normalizes provider responses into shared usage snapshot schemas with rate-limit windows, reset times, credits/quota, and plan type.
  • Keeps token-scoped usage snapshots with a 5-minute cache TTL.
  • Regenerates OpenAPI and JavaScript SDK output.

Auth

  • Extends the plugin auth contract with optional OAuth follow-up support (AuthOAuthFollowup).
  • Adds provider.oauth.followup.authorize / provider.oauth.followup.callback server endpoints in both route adapters.
  • Copilot usage tracking is opt-in: main Copilot login completes first, then CLI/TUI asks whether to run the extra GitHub device flow needed for copilot_internal/user usage data.
  • Follow-up credentials are merged into the existing OAuth auth entry.
  • Claude/OpenAI OAuth refresh persistence is handled by the usage service, with actionable Claude reauth errors when refresh is not recoverable.

TUI

  • Adds a scrollable /usage dialog with provider-specific windows, progress bars, reset labels, and credits/quota details.
  • Adds an internal sidebar Usage feature-plugin.
  • Dialog reset times are locale-aware absolute values; sidebar reset labels stay compact/relative.
  • End-of-turn refresh is throttled and only emits toasts when crossing warning thresholds (80/90/95%).

Follow-up

  • Add web/app UI using the same GET /usage endpoint, likely as a Usage tab in the existing top-right status popover rather than a custom side panel.

Verification

  • bun typecheck from packages/opencode
  • bun typecheck from packages/sdk/js
  • git diff --check
  • bun test --timeout 120000 test/server/usage.test.ts test/server/usage-copilot.test.ts test/cli/tui/usage-command.test.ts test/cli/tui/usage-format.test.ts test/cli/tui/usage-toast.test.ts test/plugin/copilot-auth.test.ts test/server/httpapi-bridge.test.ts test/server/httpapi-provider.test.ts test/server/httpapi-sdk.test.ts test/cli/tui/editor-context.test.tsx

94 tests pass in the targeted usage/HttpApi/TUI suite.

Push hook: bun turbo typecheck passed, 12/12 tasks.

AI Assistance

  • Tools: OpenCode + GPT-5.x family models
  • Review: Human-guided and reviewed throughout

@github-actions
Copy link
Copy Markdown
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
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch from ba7d89c to 9ce0a41 Compare January 20, 2026 11:14
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 4 times, most recently from 06731ef to a396eca Compare January 23, 2026 09:01
@CasualDeveloper CasualDeveloper changed the title feat(usage): unified usage tracking with Copilot/Claude auth refresh feat(usage): unified usage tracking with auth refresh (#9281) Jan 25, 2026
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 7 times, most recently from f0e277d to 60f388a Compare January 30, 2026 03:44
@thdxr thdxr force-pushed the dev branch 3 times, most recently from f1ae801 to 08fa7f7 Compare January 30, 2026 14:37
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 5 times, most recently from 4089f14 to 824997e Compare February 7, 2026 17:16
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch from 824997e to 7cdb698 Compare February 13, 2026 09:55
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 4 times, most recently from ca4c730 to aa38024 Compare April 25, 2026 15:53
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 4 times, most recently from 3db7c0e to 2e5be67 Compare April 27, 2026 02:09
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 2 times, most recently from 6c9a249 to b3616d6 Compare April 27, 2026 17:41
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 4 times, most recently from b3a7965 to c59711c Compare May 3, 2026 00:15
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 2 times, most recently from 70bcfac to d98eb27 Compare May 6, 2026 22:15
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 7 times, most recently from beb4f97 to b97f7bd Compare May 9, 2026 19:46
Adds Usage.Service as the shared usage tracking layer for OAuth-authenticated providers, including cached snapshots, in-flight fetch dedupe, token-scoped cache entries, usage.updated events, and OAuth refresh persistence with actionable Claude reauth errors.

Exposes /usage through both the default Hono route and Effect HttpApi adapter, with OpenAPI/SDK support for explicit provider, refresh, and showUsageValueMode query parameters.

Adds provider OAuth follow-up authorize/callback support for optional Copilot usage tracking, plus TUI /usage command parsing, usage dialog/sidebar display, idle refreshes, threshold toasts, and provider-specific usage formatting.

Covers usage fetching, Copilot/OpenAI/Claude auth behavior, HttpApi parity, and TUI usage command/format/toast behavior.
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] Add unified usage tracking via /usage (#9545)

3 participants