Skip to content

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

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

feat(usage): unified usage tracking with auth refresh (#9281)#9545
CasualDeveloper wants to merge 1 commit intoanomalyco: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 the shared GET /usage endpoint, generated SDK support, and 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 fetches all authenticated usage providers and opens the detail dialog.
  • /usage --used / /usage --remaining select the display mode for that dialog invocation.
  • /usage --refresh refreshes all provider caches and updates the sidebar 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 GET /usage under packages/opencode/src/server/routes/instance/usage.ts.
  • 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 a 5-minute usage snapshot cache with in-flight request deduping.
  • Publishes usage.updated when stored snapshots change.
  • 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.
  • 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.

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

  • ./script/generate.ts
  • bun turbo typecheck
  • bun --cwd packages/opencode test --timeout 600000 test/plugin/copilot-auth.test.ts 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

48 tests pass in the targeted usage suite.

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
Copilot AI review requested due to automatic review settings April 13, 2026 07:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a unified /usage API as the single source of truth for usage/rate-limit snapshots across OpenAI ChatGPT (Codex), GitHub Copilot, and Anthropic Claude, and wires it into the TUI (dialog + sidebar + toast warnings) with provider-specific auth handling (including optional Copilot “usage token” capture and Claude refresh-on-401).

Changes:

  • Add core usage module + provider fetchers with caching, schema validation, and usage.updated bus events.
  • Add GET /usage server route (provider filter + refresh + slash-command parsing) and regenerate SDK/OpenAPI types/clients.
  • Add TUI integrations: /usage command + dialog, sidebar usage panel, and automatic refresh/toast warnings after assistant turns; extend OAuth schema with optional usage token.

Reviewed changes

Copilot reviewed 32 out of 32 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/sdk/openapi.json Adds /usage endpoint + usage.updated event + OAuth usage field to OpenAPI schema.
packages/sdk/js/src/v2/gen/types.gen.ts Regenerates SDK types for /usage, event, and OAuth usage?: string.
packages/sdk/js/src/v2/gen/sdk.gen.ts Regenerates JS SDK client with client.usage.get(...).
packages/plugin/src/index.ts Extends plugin-facing OAuth result typing with optional usage token.
packages/opencode/src/auth/index.ts Extends persisted OAuth auth schema with optional usage token.
packages/opencode/src/usage/types.ts Defines zod schemas + TS types for usage snapshots.
packages/opencode/src/usage/registry.ts Defines supported usage providers and their auth requirements.
packages/opencode/src/usage/store.ts Adds usage cache storage, usage.updated bus event, and auth/provider helpers.
packages/opencode/src/usage/providers/openai.ts Implements ChatGPT usage fetcher (/backend-api/wham/usage) + normalization.
packages/opencode/src/usage/providers/github-copilot.ts Implements Copilot usage fetcher (copilot_internal/user) + token fallback parsing.
packages/opencode/src/usage/providers/anthropic.ts Implements Claude OAuth usage fetcher + refresh-on-expiry/401 flow.
packages/opencode/src/usage/index.ts Exposes usage module API (Usage.*) and provider fetchers.
packages/opencode/src/usage/command.ts Adds /usage command parsing and “current vs all” provider resolution logic.
packages/opencode/src/server/usage.ts Adds GET /usage route: filtering, refresh behavior, concurrency dedupe, cached fallback errors.
packages/opencode/src/server/instance.ts Registers /usage routes in server instance router.
packages/opencode/src/plugin/github-copilot/copilot.ts Updates Copilot auth flow (optional second device flow for usage token) + service token exchange + header fixes.
packages/opencode/src/config/tui-schema.ts Adds TUI config options for usage scope + used/remaining display mode.
packages/opencode/src/cli/cmd/tui/plugin/internal.ts Registers new sidebar usage internal plugin.
packages/opencode/src/cli/cmd/tui/feature-plugins/sidebar/usage.tsx Adds sidebar Usage panel rendering + refetch on events/turn completion.
packages/opencode/src/cli/cmd/tui/component/usage-data.ts Adds TUI-side usage data shapes.
packages/opencode/src/cli/cmd/tui/component/usage-client.ts Adds /usage client/resource + event-driven refetch logic.
packages/opencode/src/cli/cmd/tui/component/usage-format.ts Adds formatting helpers (labels, bars, resets, credits).
packages/opencode/src/cli/cmd/tui/component/usage-toast.ts Adds threshold-crossing detection + refresh/backoff helpers for toast warnings.
packages/opencode/src/cli/cmd/tui/component/dialog-usage.tsx Adds /usage dialog UI with used/remaining toggle.
packages/opencode/src/cli/cmd/tui/component/dialog-command.tsx Adds slashDescription field used for slash-command UI descriptions.
packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx Implements built-in /usage command handling + post-turn refresh/toast logic + submission refactor.
packages/opencode/test/server/usage.test.ts Adds server+fetcher tests for cache behavior, parsing, provider filtering, and error fallback.
packages/opencode/test/server/usage-copilot.test.ts Adds Copilot fallback behavior test when usage endpoint fails.
packages/opencode/test/plugin/copilot-auth.test.ts Adds tests for Copilot dual device-flow + optional usage polling + service token behavior.
packages/opencode/test/cli/tui/usage-toast.test.ts Adds tests for warning thresholds, refresh cooldown/backoff, and dedupe/eviction.
packages/opencode/test/cli/tui/usage-format.test.ts Adds tests for formatting/labeling and used vs remaining display conversion.
packages/opencode/test/cli/tui/usage-command.test.ts Adds tests for /usage command parsing and conflict validation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/opencode/src/plugin/github-copilot/copilot.ts
Comment thread packages/sdk/openapi.json
Comment thread packages/opencode/src/plugin/github-copilot/copilot.ts Outdated
@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 3 times, most recently from 8309992 to 1b0d195 Compare April 13, 2026 14:41
@CasualDeveloper
Copy link
Copy Markdown
Contributor Author

Fork updated to sync with dev (as of cb1a500).

@CasualDeveloper CasualDeveloper force-pushed the feat/9281-usage-tracking branch 2 times, most recently from 9e63bb7 to 552fe6c Compare April 18, 2026 23:18
@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 3 times, most recently from d7e9c27 to b3a7965 Compare April 29, 2026 17:57
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