fix: restore /usage account limits in CLI + gateway (#958) #1016

Open
Rockachopa wants to merge 186 commits from fix/958 into main
Owner

Closes #958

Summary:

  • add agent/account_usage.py to fetch/render provider account limit snapshots for Codex, Anthropic OAuth, and OpenRouter
  • append account-limit sections to CLI /usage output with a bounded background fetch
  • append the same account-limit section to gateway /usage output, including persisted billing-provider fallback between turns
  • add targeted regression coverage for account-usage fetching/rendering and gateway /usage output

Verification:

  • PYTHONPATH=/tmp/BURN2-FORGE-ALPHA-3 python3 -m pytest -q tests/test_account_usage.py tests/gateway/test_usage_command.py
  • PYTHONPATH=/tmp/BURN2-FORGE-ALPHA-3 python3 -m py_compile agent/account_usage.py cli.py gateway/run.py tests/test_account_usage.py tests/gateway/test_usage_command.py
Closes #958 Summary: - add agent/account_usage.py to fetch/render provider account limit snapshots for Codex, Anthropic OAuth, and OpenRouter - append account-limit sections to CLI /usage output with a bounded background fetch - append the same account-limit section to gateway /usage output, including persisted billing-provider fallback between turns - add targeted regression coverage for account-usage fetching/rendering and gateway /usage output Verification: - PYTHONPATH=/tmp/BURN2-FORGE-ALPHA-3 python3 -m pytest -q tests/test_account_usage.py tests/gateway/test_usage_command.py - PYTHONPATH=/tmp/BURN2-FORGE-ALPHA-3 python3 -m py_compile agent/account_usage.py cli.py gateway/run.py tests/test_account_usage.py tests/gateway/test_usage_command.py
Rockachopa added 1 commit 2026-04-22 14:41:13 +00:00
fix: restore /usage account limits (#958)
All checks were successful
Lint / lint (pull_request) Successful in 9s
eab5635a7a
Rockachopa reviewed 2026-04-22 15:13:14 +00:00
Rockachopa left a comment
Author
Owner

Review: Well-architected account usage feature.

This PR adds account-limits reporting to /usage in both CLI and gateway, with support for Anthropic (OAuth), OpenAI Codex, and OpenRouter providers.

Strengths:

  • The AccountUsageSnapshot dataclass is clean and immutable (frozen=True).
  • Provider-specific fetch functions are properly isolated.
  • Both CLI and gateway fetch usage off the main thread (ThreadPoolExecutor / asyncio.to_thread) with timeouts, so slow provider APIs do not hang the UI.
  • Failures are non-fatal — account_snapshot defaults to None on any exception.
  • _resolve_codex_usage_url normalizes base URLs correctly.
  • _format_reset produces human-friendly relative time strings.
  • httpx.Client is used with explicit timeouts (10-15s).
  • Gateway fallback to persisted billing data (session DB) when no agent is resident is a nice touch.

Security observations:

  • API tokens are read from existing credential stores (resolve_anthropic_token, resolve_codex_runtime_credentials, resolve_runtime_provider) — no new credential handling.
  • The Anthropic OAuth usage endpoint includes the anthropic-beta header, which is correct for the beta API.

Minor observations (non-blocking):

  1. _parse_dt handles both Unix timestamps and ISO strings with Z suffix normalization — good.
  2. The OpenRouter path fetches both /credits and /key endpoints. The /key fetch is wrapped in try/except so it degrades gracefully if the key does not have permission.
  3. The test mocks are well-structured, patching at the gateway module level.
**Review: Well-architected account usage feature.** This PR adds account-limits reporting to `/usage` in both CLI and gateway, with support for Anthropic (OAuth), OpenAI Codex, and OpenRouter providers. **Strengths:** - The `AccountUsageSnapshot` dataclass is clean and immutable (`frozen=True`). - Provider-specific fetch functions are properly isolated. - Both CLI and gateway fetch usage off the main thread (ThreadPoolExecutor / asyncio.to_thread) with timeouts, so slow provider APIs do not hang the UI. - Failures are non-fatal — `account_snapshot` defaults to None on any exception. - `_resolve_codex_usage_url` normalizes base URLs correctly. - `_format_reset` produces human-friendly relative time strings. - `httpx.Client` is used with explicit timeouts (10-15s). - Gateway fallback to persisted billing data (session DB) when no agent is resident is a nice touch. **Security observations:** - API tokens are read from existing credential stores (resolve_anthropic_token, resolve_codex_runtime_credentials, resolve_runtime_provider) — no new credential handling. - The Anthropic OAuth usage endpoint includes the `anthropic-beta` header, which is correct for the beta API. **Minor observations (non-blocking):** 1. `_parse_dt` handles both Unix timestamps and ISO strings with Z suffix normalization — good. 2. The OpenRouter path fetches both `/credits` and `/key` endpoints. The `/key` fetch is wrapped in try/except so it degrades gracefully if the key does not have permission. 3. The test mocks are well-structured, patching at the gateway module level.
claude approved these changes 2026-04-22 16:09:59 +00:00
claude left a comment
Member

Looks good overall. The account usage module is well-structured with proper dataclass patterns, timezone handling, and provider-specific fetching. A few notes:

  1. The httpx timeout of 15s is reasonable for network calls.
  2. Error handling via try/except around provider fetches is appropriate for non-critical display data.
  3. The _parse_dt helper handles edge cases (Unix timestamps, ISO strings, Z-suffix) correctly.

No blocking issues.

Looks good overall. The account usage module is well-structured with proper dataclass patterns, timezone handling, and provider-specific fetching. A few notes: 1. The httpx timeout of 15s is reasonable for network calls. 2. Error handling via try/except around provider fetches is appropriate for non-critical display data. 3. The _parse_dt helper handles edge cases (Unix timestamps, ISO strings, Z-suffix) correctly. No blocking issues.
All checks were successful
Lint / lint (pull_request) Successful in 9s
This pull request has changes conflicting with the target branch.
  • cli.py
  • gateway/platforms/api_server.py
  • gateway/run.py
  • hermes_cli/models.py
  • hermes_cli/web_server.py
  • run_agent.py
  • tests/gateway/test_usage_command.py
  • tests/hermes_cli/test_web_server.py
  • tests/test_account_usage.py
  • tools/browser_tool.py
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin fix/958:fix/958
git checkout fix/958
Sign in to join this conversation.
No Reviewers
No Label
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Timmy_Foundation/hermes-agent#1016