fix(core): reduce OAuth refresh frequency and honor Retry-After#40
Open
iceteaSA wants to merge 4 commits into
Open
fix(core): reduce OAuth refresh frequency and honor Retry-After#40iceteaSA wants to merge 4 commits into
iceteaSA wants to merge 4 commits into
Conversation
MIN_REFRESH_BEFORE_EXPIRY_MINUTES was 240 (4 hours) but Claude OAuth tokens expire in ~1 hour. This caused background refresh to fire on every tick (~30-60 requests/hour), triggering 429 rate limits. Reduced to 15 minutes — tokens refresh at 45 minutes instead of immediately. Multiple opencode instances no longer multiply pressure.
ClaudeOAuthRefreshError now captures the Retry-After header from 429 responses. Supports both seconds and HTTP date formats. Consumers can use error.retryAfter to respect the server's delay.
buildRefreshOperationError now checks ClaudeOAuthRefreshError.retryAfter before falling back to exponential backoff. Respects server-requested delay on 429 responses.
Tests asserting the old 4h minimum refresh window now use 10-minute expiry tokens so they trigger within the new 15m minimum.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MIN_REFRESH_BEFORE_EXPIRY_MINUTESis set to 240 (4 hours), but Claude OAuth tokensexpire in ~1 hour. This causes the background refresh timer to fire on every tick
(~30-60 requests/hour to the token endpoint), triggering 429 rate limits — especially
with multiple opencode instances running concurrently.
Additionally, 429 responses include a
Retry-Afterheader that is currently ignored.The backoff uses a flat exponential starting at 5 minutes regardless of what the
server requests.
Fix
Reduce
MIN_REFRESH_BEFORE_EXPIRY_MINUTESfrom 240 to 15 — tokens now refreshat 45 minutes (with 1-hour tokens) instead of continuously. Background refresh
fires once per token lifecycle instead of ~30-60 times.
Add
Retry-Afterheader support —ClaudeOAuthRefreshErrornow captures theRetry-Afterheader from 429 responses. Supports both seconds (60) and HTTPdate formats.
buildRefreshOperationErroruses it as the backoff duration whenavailable, falling back to exponential backoff otherwise.
Impact
Changes
packages/core/src/accounts.tspackages/core/src/auth.tsretryAftertoClaudeOAuthRefreshError, parse headerpackages/opencode/src/index.tspackages/opencode/src/tests/auth.test.tspackages/opencode/src/tests/accounts.test.tspackages/opencode/src/tests/index.test.tsSummary by cubic
Reduce OAuth refresh frequency and honor server Retry-After on 429s to stop hammering the token endpoint. This cuts refresh requests by ~30–60x/hour and reduces rate limits across multiple instances.
Retry-Afteron 429s:ClaudeOAuthRefreshErrorparses seconds and HTTP-date values;buildRefreshOperationErroruses it for backoff, falling back to exponential when absent.Written for commit 3200ef9. Summary will update on new commits. Review in cubic