fix: cap balance preflight output estimate - #217
Open
0xCheetah1 wants to merge 2 commits into
Open
Conversation
📝 WalkthroughWalkthroughAdds a 4096-token cap to balance preflight estimates, exposes the general estimator and capped preflight helper, updates balance checks and fallback accounting, and adds parameterized coverage for high-output models. ChangesBalance preflight estimation
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/proxy.ts`:
- Around line 4692-4694: In the preflight estimation flow around
estimateBalancePreflightAmount, store the capped result in a separate
preflightEstimatedCostMicros variable used only by checkSufficient. Keep
estimatedCostMicros tied to the uncapped estimate or actual settled payment so
maxCostPerRun enforcement and balance deduction use the full request cost.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: c6363729-225a-49ef-ba50-6a529320e210
⛔ Files ignored due to path filters (4)
dist/cli.jsis excluded by!**/dist/**dist/cli.js.mapis excluded by!**/dist/**,!**/*.mapdist/index.jsis excluded by!**/dist/**dist/index.js.mapis excluded by!**/dist/**,!**/*.map
📒 Files selected for processing (2)
src/proxy.balance-estimate.test.tssrc/proxy.ts
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.
Summary
Fixes balance preflight fallback for high-output-cap models.
OpenClaw can send the model default as
max_tokens, e.g.128000for Opus-class models. ClawRouter was treating that as guaranteed spend during the local balance check, so calls toanthropic/claude-opus-5could fall back to a free model even with$5+in the wallet. In practice, short Opus 5 calls cost closer to cents, but the preflight assumed worst-case full output.This adds a preflight-only output estimate cap. The actual request payload is unchanged, and x402/server payment still enforces the real quote.
Changes
estimateBalancePreflightAmount()for low-balance preflight checks.estimateAmount()exact for budget enforcement, max-cost checks, and cached balance accounting.Verification
v22.23.1npm run format:checknpm run typechecknpm run lintnpm testnpm run buildanthropic/claude-opus-5max_tokens: 128000$5.83Summary by CodeRabbit
Bug Fixes
Tests