Skip to content

fix(claude): stop asking for refused usage credits on every refresh - #124

Merged
bernardopg merged 1 commit into
mainfrom
fix/claude-prepaid-403
Nov 11, 2026
Merged

bernardopg merged 1 commit into
mainfrom
fix/claude-prepaid-403

Conversation

@bernardopg

Copy link
Copy Markdown
Owner

Summary

On a Claude Team plan a non-admin member gets HTTP 403 from GET /api/oauth/organizations/<org>/prepaid/credits. get-claude-usage only wrote claude-prepaid-cache.json on a 200, so the request was repeated on every run (each widget instance, each manual refresh), even while the usage request was served from its 120 s cache. That endpoint shares the account's rate limit with /api/oauth/usage, which then answered 429 with a one hour Retry-After (#123).

Every credits answer is now recorded as {checked_at, status, org, data}:

  • 403 / 404: not asked again for 24 hours (PREPAID_UNAVAILABLE_TTL); no credits are shown.
  • 429: starts the shared usage backoff (record_usage_backoff, Retry-After honoured), so both endpoints pause.
  • Any other failure (5xx, network, a 200 whose body is not a credits object): retried after the usual 10 minutes, keeping the last good balance.
  • Credits are not asked while a usage backoff holds, also when the usage request itself came from the cache (it was only skipped when the usage request had just failed).
  • A record for another organization is dropped, so a switch re-checks at once instead of showing the other organization's balance. A corrupt, empty or future-stamped record is not trusted. Records written before this change ({cached_at, data}) are still read.

Reproduced with a fake curl answering 403 on prepaid/credits: four runs made 4 credits requests before (1 usage request, from the cache) and 1 after; same for 404 and 500.

Docs: docs/provider-verification.md, docs/troubleshooting.md (Claude), CHANGELOG.md Unreleased (Fixes, Thanks). No UI string changed, so no locale update.

Related issues

Fixes #123. Refs #96.

Type of change

  • New provider
  • Bug fix
  • Enhancement
  • Documentation / i18n
  • CI / tooling

Checklist

  • plugin.json .version and CHANGELOG.md are consistent (no release in this PR)
  • New/changed provider scripts are executable and emit valid JSON
  • i18n/en.json keys have translations in every locale
  • Every commit is signed off (git commit -s) under the DCO
  • CI is green

Tests: tests/test-limit-resets.sh gains a Team-account section: a first refusal, the pre-change record format, 403 held for a day across instances and an expired usage cache, 404, organization switch, 500 and an HTML 200 keeping the last balance, corrupt/empty/future records, and a 429 on credits pausing the usage endpoint. Local: every non-QML suite, check-metadata, check-changelog --base origin/main, check-dco, shellcheck, git diff --check, and statement coverage 100.00% (2398/2398) with kcov built from the CI-pinned commit 460cd988.

A Team plan member gets HTTP 403 from the organization's
prepaid/credits endpoint. get-claude-usage only recorded a 200, so
every run asked again: each widget instance (one per monitor) and each
manual refresh, even while the usage request was served from its
120 s cache. That endpoint shares the account's rate limit with
/api/oauth/usage, which soon answered 429 with a one hour Retry-After.

Every credits answer is now recorded with its time, status and
organization:

- 403 or 404: not asked again for 24 hours, and no credits are shown.
- 429: starts the shared usage backoff, so both endpoints pause.
- any other failure (5xx, network, a 200 that is not a credits
  object): retried after the usual 10 minutes, keeping the last good
  balance.

Credits are not asked while a usage backoff holds, also when the usage
request itself came from the cache. A record for another organization,
a corrupt one or one stamped in the future is not trusted. Records
written before this change ({cached_at, data}) are still read.

Fixes #123

Signed-off-by: Bernardo Pinto Gomes <[email protected]>
@github-actions github-actions Bot added documentation Improvements or additions to documentation area:providers Provider adapters (providers/) area:ci CI, workflows, tooling labels Nov 11, 2026
@bernardopg
bernardopg enabled auto-merge November 11, 2026 18:21
@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Stop repeated Claude credits requests after access is refused

🐞 Bug fix 🧪 Tests 📝 Documentation 🕐 20-40 Minutes

Grey Divider

AI Description

• Remember unavailable Claude organization credits for 24 hours to prevent repeated requests and
 rate limits.
• Share credits-endpoint backoff with usage requests while preserving balances after transient
 failures.
• Document the behavior and test refusals, cache compatibility, organization switches, and backoff.
Diagram

graph TD
  A["Claude adapter"] --> B["Resolve organization"] --> C{"Credits cache fresh?"} --> D{"Usage backoff active?"} --> E["Credits endpoint"] --> F{"Response status?"} --> G["Credits cache"]
  C -- "yes" --> G
  D -- "yes" --> G
  F -- "429" --> H["Shared usage backoff"] --> G
Loading
High-Level Assessment

Keep the unified, organization-scoped credits record and reuse the existing usage backoff. A separate refusal cache or independent credits backoff would add synchronization and invalidation paths without a clear benefit for this fix.

Files changed (5) +149 / -9

Bug fix (1) +38 / -7
get-claude-usageCache credits outcomes and share rate-limit backoff +38/-7

Cache credits outcomes and share rate-limit backoff

• Records credits responses with their time, status, organization, and balance; applies a 24-hour TTL to 403/404 and a 10-minute TTL to other outcomes. Rejects stale or mismatched records, retains legacy cache support and last good balances where appropriate, and makes a credits 429 pause usage requests.

providers/get-claude-usage

Tests (1) +100 / -1
test-limit-resets.shCover Claude credits cache and refusal scenarios +100/-1

Cover Claude credits cache and refusal scenarios

• Extends fake curl responses and assertions for refused credits, legacy and invalid records, organization changes, transient failures, and shared 429 backoff. Checks that repeated refreshes avoid unnecessary credits requests.

tests/test-limit-resets.sh

Documentation (3) +11 / -1
CHANGELOG.mdRecord the Claude credits rate-limit fix +8/-0

Record the Claude credits rate-limit fix

• Adds an Unreleased fix entry explaining the refused-credits behavior and shared rate limit, and credits the reporter.

CHANGELOG.md

provider-verification.mdClarify Claude credits endpoint behavior +1/-1

Clarify Claude credits endpoint behavior

• Documents organization-scoped caching, the 24-hour refusal interval, and the shared backoff triggered by a credits 429.

docs/provider-verification.md

troubleshooting.mdExplain unavailable Claude credits +2/-0

Explain unavailable Claude credits

• Explains why some accounts show no credits, when a refused request is retried, and how the credits cache relates to usage backoff.

docs/troubleshooting.md

@qodo-code-review

qodo-code-review Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (3)

Grey Divider


Remediation recommended

1. Interrupted failures can erase credits 🐞 Bug
Description
The new unconditional write truncates PREPAID_CACHE in place even when a failed request is
supposed to retain its last good balance. If the process is terminated during that write, the next
run finds an empty or partial record instead of the saved balance; previously failed credits
requests did not rewrite that file.
Code

providers/get-claude-usage[549]

+        jq -cn --arg org "$ORG_UUID" --arg status "$prepaid_status" --argjson data "$prepaid_data" '{checked_at: (now | floor), status: $status, org: $org, data: $data}' > "$PREPAID_CACHE" 2>/dev/null || true
Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Rewriting the prepaid cache directly can destroy the last good balance if a failure-recording run stops mid-write.
## Fix Focus Areas
- providers/get-claude-usage[545-550]
- providers/get-claude-usage[76-87]
## Recommended Fix
Write the complete new prepaid record to a temporary file in the cache directory, then atomically rename it over the existing record. Preserve the old file if record construction or replacement fails.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


View medium (2)
2. Switched accounts can show old credits 🐞 Bug
Description
(.org // $org) == $org treats a legacy prepaid record without an organization as belonging to
whichever organization is currently signed in. If the user switches organizations while a
{cached_at, data} record is fresh, the adapter skips the credits request and passes the previous
organization's balance to the card.
Code

providers/get-claude-usage[508]

+            '(.org // $org) == $org' "$PREPAID_CACHE" >/dev/null 2>&1; then
Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Legacy prepaid records have no organization field, so the current comparison cannot establish that their balance belongs to the signed-in organization.
## Fix Focus Areas
- providers/get-claude-usage[506-523]
- tests/test-limit-resets.sh[176-206]
## Recommended Fix
Preserve reading legacy records where safe, but do not treat a missing organization as proof of a match. Ensure a switch from a legacy cached balance triggers a credits recheck before displaying that balance, and add a regression test for that transition.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


3. Credits rate limits lack a retry time 🐞 Bug
Description
record_usage_backoff persists a credits-endpoint 429, but the new call does not populate
USAGE_RETRY_AT or USAGE_RETRY_REASON for the current response. When usage came from its fresh
cache, the following refreshes also leave those fields empty until that cache expires, so the card
does not indicate the active pause.
Code

providers/get-claude-usage[544]

+            [ "$prepaid_status" != "429" ] || record_usage_backoff "$prepaid_status" "$prepaid_headers"
Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A credits 429 starts a shared backoff without reporting its retry time on responses served from the usage cache.
## Fix Focus Areas
- providers/get-claude-usage[543-550]
- providers/get-claude-usage[345-355]
- tests/test-limit-resets.sh[234-245]
## Recommended Fix
After recording a credits 429, populate the response retry fields from the active backoff. Also populate them when fresh cached usage is served during an existing backoff, and test both cases.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


Grey Divider

Tip of the day
💡 Did you know, you can import rules and skills only from the repositories your team follows

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@bernardopg
bernardopg merged commit 2c46c2d into main Nov 11, 2026
20 checks passed
@bernardopg
bernardopg deleted the fix/claude-prepaid-403 branch November 11, 2026 18:25
bernardopg added a commit that referenced this pull request Nov 11, 2026
Review follow-up to #124:

- The credits record is replaced with a rename (atomic_write), so a
  refresh killed mid-write keeps the last record and its balance;
  leftover temporary files are swept under the account lock. Failed
  reads rewrite the record since #124, which made an in-place write
  riskier than before.
- A record written by 2.4.0 ({cached_at, data}) names no organization.
  It was taken as the current one, so after a switch inside its TTL the
  previous organization's balance could be shown. Such a record is now
  re-checked once and never shown.
- A credits 429 starts the shared usage backoff but left
  USAGE_RETRY_AT/USAGE_RETRY_REASON empty, as did a run that served a
  fresh usage snapshot while a backoff held. The card now names the next
  try whenever the pause holds, as docs/architecture.md describes.

Refs #123

Signed-off-by: Bernardo Pinto Gomes <[email protected]>
@bernardopg bernardopg mentioned this pull request Nov 11, 2026
4 of 5 tasks
bernardopg added a commit that referenced this pull request Nov 11, 2026
Claude Team accounts no longer ask for refused usage credits on every
refresh (#123, #124, #125): a refusal is remembered for a day per
organization, a credits 429 pauses the usage endpoint too, and the card
names the next try.

Signed-off-by: Bernardo Pinto Gomes <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ci CI, workflows, tooling area:providers Provider adapters (providers/) documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Claude: prepaid/credits returns 403 on Team plans and is retried on every refresh, triggering HTTP 429 on the usage endpoint

1 participant