Repository navigation
feat(claude-cli): stop when a request is charged to usage credits - #96
Merged
Merged
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.
What and why
Two changes to
@pi-plugins/claude-cli, taken from a comparison with earendil-works/pi-claude-code:Usage-credit guard. Until now the plugin could quietly bill Claude usage credits (formerly "extra usage") once a plan limit ran out, or for models and modes that always use them. Claude Code sends a
rate_limit_eventwithisUsingOveragefor every response. The plugin used to stop reading atmessage_stopand never saw that event.message_stopback until Claude Code'srate_limit_event, itsresultor the end of its output. It ignores any stream events that come aftermessage_stop.isUsingOverageis true, the turn fails with an error that tells the user how to stay on plan limits or how to allow credits.isRetryableAssistantErrorwon't retry them.PI_CLAUDE_CLI_ALLOW_USAGE_CREDITS=1turns the guard off. It is read withConfig.Booleanin the layer.README. I removed the claim that using the subscription this way "stays within Anthropic's terms of service". In its place is a note: Anthropic announced in May 2026 that
claude -pwould be billed separately, paused that in June, andclaude -pstill draws from plan limits for now. There's also a new "Usage credits" section.Verification
I ran real Claude Code 2.1.302 with
--include-partial-messages.rate_limit_eventarrives about 10 ms aftermessage_stopand beforeresult. That holds for a text reply and for atool_usereply too, where Claude Code's tool attempt has already failed beforemessage_stop. No second API request is made.Through
ClaudeCli.streamwith real Claude Code (Haiku), I compared the time from the last content event todone, with and without this change, on a tool-use turn: 73/54 ms on master and 88/55 ms on this branch. The extra wait is negligible.pi --no-extensions -e plugins/claude-cli --model claude-cli/claude-haiku-4-5 -p …loads the plugin and answers.New tests:
The fake's replies now end with a
rate_limit_eventand aresult, as the real CLI's do.cipasses.Review findings skipped
The correctness review found nothing. I fixed all the standards findings except one. The
stream_eventhandler stays a plain function that returnsEffect.succeed(Option.match(...)), because written withEffect.fnUntracedit has noyieldand oxlint'srequire-yieldrejects it.Assumptions and open questions
claudeprocess serves exactly one request, so its first response always triggers arate_limit_event, since Claude Code emits one when the info changes. If it ever doesn't, the response still completes onresultor exit, just without the check.allowUsageCredits: truerather than setting the env var. Effect's defaultConfigProvidertakes a snapshot ofprocess.envon first use, so the module-level runtime can't pick the variable up partway through the test run.