feat: expose subscription rate-limit snapshots via rate_limits output#1519
Open
alperenuzun wants to merge 1 commit into
Open
feat: expose subscription rate-limit snapshots via rate_limits output#1519alperenuzun wants to merge 1 commit into
alperenuzun wants to merge 1 commit into
Conversation
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.
Closes #1518
What
When Claude Code runs with claude.ai subscription (OAuth) authentication, the SDK stream emits
rate_limit_eventmessages carrying the subscription window state (status,utilization0-100,resetsAt, window type). This PR captures the latest snapshot per window during the existing message loop and exposes it as a newrate_limitsaction output (JSON, keyed by window type), on both the main action and the standalone base-action.This lets workflows that bill to a Pro/Max subscription report how full the 5-hour and weekly windows are (the same numbers the
/usagepanel shows) and alert before reviews start failing with 429s — see #1518 for the paths that don't work today (statusline hooks never fire in SDK mode,GET /api/oauth/usageis unreliable, response headers are unobservable from inside the action).Example
Output value:
{ "five_hour": { "status": "allowed", "rateLimitType": "five_hour", "utilization": 25, "resetsAt": 1752684000 }, "seven_day": { "status": "allowed_warning", "rateLimitType": "seven_day", "utilization": 91, "resetsAt": 1753029000 } }Design notes
rate_limit_eventis part of theSDKMessageunion that the run loop already iterates. The experimental usage control API (usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET) was deliberately avoided.rate_limit_eventmessages are emitted there, mirroring the SDK's own semantics (rate_limits_available: false).run-claude-sdk.ts(same pattern asexecution-file.ts) covers both entrypoints —src/entrypoints/run.tsand the standalone base-action — without duplicating logic.rateLimitTypewith latest-wins semantics when multiple events arrive for the same window.Testing
base-action/test/run-claude-sdk.test.ts: latest-snapshot-per-window capture across multiple events, and norate_limitsoutput when no events are received.bun test: 801 pass (root) + 154 pass (base-action), 0 fail.bun run typecheckclean in both packages;bun run format:checkclean.🤖 Generated with Claude Code