fix: cap balance preflight output estimate - #217
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
OpenClaw can send a model's huge default max_tokens (e.g. 128k on Opus-class models); the low-balance preflight treated that as guaranteed spend, so paid calls fell back to a free model even with $5+ in the wallet. Preflight now caps assumed output at 4096 tokens; estimateAmount() stays exact for strict cost caps and cached-balance accounting, and the free-fallback path clears the stale paid estimate so it is no longer deducted from the cached balance.
|
Landed on main as |
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