Add pre-flight rate limiting with token accounting#2
Merged
Conversation
Extends the existing per-provider sliding-window rate limiter with token windows and a pre-flight check() that blocks requests before they hit the wire. Stops RWTH (and other) rate-limited providers from surfacing generic "An internal error occurred" messages after a 429 — the limit is learned on the first 429, persisted to opencode.json, and on subsequent runs the pre-flight gate throws a RateLimitError with a friendly "retry in Ns" message that the retry layer honours via resetAt. - rate-limit.ts: token windows (minute + day), check() gate, recordUsage, estimateRequestTokens, configure() to seed from opencode.json options, onRateLimitError persists token limits too - provider.ts fetch wrapper: pre-flight check, RateLimit.configure from options.rateLimit, tick with token estimate - session/processor.ts: call RateLimit.recordUsage on finish-step so the pending estimate is replaced with actual usage - session/retry.ts: RateLimitError is retryable; delay() honours resetAt - provider/error.ts: friendly message for 429 responses; mark retryable - session/message-v2.ts: RateLimitError NamedError, converts to APIError shape in fromError so the SDK type surface is unchanged - config/provider.ts: tokensPerMinute / tokensPerDay schema fields - cli/cmd/stats.ts: RATE LIMITS section reads persisted limits https://claude.ai/code/session_01S3gS4AgfQQBNHvgZqkuiRa
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.
Extends the existing per-provider sliding-window rate limiter with token
windows and a pre-flight check() that blocks requests before they hit
the wire. Stops RWTH (and other) rate-limited providers from surfacing
generic "An internal error occurred" messages after a 429 — the limit
is learned on the first 429, persisted to opencode.json, and on
subsequent runs the pre-flight gate throws a RateLimitError with a
friendly "retry in Ns" message that the retry layer honours via resetAt.
estimateRequestTokens, configure() to seed from opencode.json options,
onRateLimitError persists token limits too
options.rateLimit, tick with token estimate
the pending estimate is replaced with actual usage
shape in fromError so the SDK type surface is unchanged
https://claude.ai/code/session_01S3gS4AgfQQBNHvgZqkuiRa