feat: expose rate limit information in APIError #7295
+105
−0
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.
Rate Limit Information Enhancement
Summary
This PR adds detailed rate limit information to OpenCode's
APIErrorschema, exposing provider-specific rate limiting details that were previously captured by the server but not accessible to SDK clients.Problem
When OpenCode encounters rate limits from AI providers (Anthropic, Google, etc.), it captures detailed information in debug logs including:
However, this information was not exposed through the API, forcing applications to:
Solution
Enhanced the
MessageV2.APIErrorschema with a newrateLimitInfofield containing:Implementation Details
1. Schema Enhancement (lines 34-41)
Added
rateLimitInfofield toAPIErrorZod schema as an optional object with 5 optional sub-fields.2. Helper Function (lines 611-805)
extractRateLimitInfo(error: APICallError)extracts rate limit details from:Standard Headers:
retry-after: Supports both seconds (number) and HTTP-date formatsAnthropic-specific Headers:
anthropic-ratelimit-unified-reset: Reset timestampanthropic-ratelimit-unified-status: "rejected" or "allowed"anthropic-ratelimit-unified-utilization: 0-1 percentageGoogle Response Body:
QuotaFailure: Violations with metrics, limits, and dimensionsRetryInfo: Retry delays in "Xs" format3. Integration (line 876)
Updated
fromError()to callextractRateLimitInfo(e)and include result inAPIErrorconstruction.Testing
Validated with actual rate limit responses:
Anthropic 429:
Google RESOURCE_EXHAUSTED:
{ "error": { "details": [ { "@type": "type.googleapis.com/google.rpc.QuotaFailure", "violations": [{ "quotaMetric": "character_count", "quotaLimit": "CharacterCountPerDay" }] }, { "@type": "type.googleapis.com/google.rpc.RetryInfo", "retryDelay": "22.362s" } ] } }Benefits
Example Usage
Backwards Compatibility
Files Changed
packages/opencode/src/session/message-v2.ts