fix(budget): debit from metered usage instead of input-only estimates - #592
Open
yzxcj797 wants to merge 1 commit into
Open
fix(budget): debit from metered usage instead of input-only estimates#592yzxcj797 wants to merge 1 commit into
yzxcj797 wants to merge 1 commit into
Conversation
The budget priced every model at a fixed $5/MTok over an input-token estimate: output tokens (the larger share of a real bill) were invisible, cheap-input models were overcharged up to 5x, and the provider-metered usage the harnesses already report through recordLlmRequest was never consulted (yc-software#586). Add modelCallCostUsd(rec, usage): provider-computed costUsd when positive; metered input+output at the fixed rate when tokens exist without a provider price; the legacy input-only estimate otherwise. The turn path's recordLlmRequest now debits the budget with the metered figure, charging only the positive difference over the upfront estimate already booked for that step's input (no double count, no refunds). The upfront estimate is untouched so ceilings still trip promptly for turns that never report usage.
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
Implements the "smallest useful change" from #586.
Root cause (as diagnosed in the issue, verified)
The budget debited
estimateCostUsd(rec.inputTokens)— a fixed $5/MTok over an input-only estimate — at everyrecordModelCallsite, while the provider-metered numbers (input,output,cacheRead/Write,costUsd) were already arriving one callback over, onrecordLlmRequest, and being persisted tosession_llm_requestswith full usage. Consequences per the issue's table: cheap-input models overcharged up to 5×, and output tokens — the larger share of a real bill — completely invisible.Fix
modelCallCostUsd(rec, usage)(inratelimit/budget.ts) resolves one call's cost in priority order:usage.costUsdwhen positive → used verbatim;input + outputat the existing fixed rate when tokens exist but the provider didn't price them (output no longer invisible);recordLlmRequestcallback now also debits the budget with the metered figure — charging only the positive difference over what the upfront input-only estimate already booked for that step's input. No double-counting (the upfront charge isn't refunded when metered cost is lower), no refunds.recordModelCallestimate is untouched — budget ceilings still trip promptly for turns that never report usage (detection, compaction, security-screen paths).Scope note
The
turn_metricsobservability gap (no cost/output columns at the metrics sink) is the other half of the issue and is deliberately left for its own change.Tests
modelCallCostUsdresolution tests intest/budget.test.ts(provider cost verbatim; metered-tokens fallback prices input+output; null/undefined usage falls back to the estimate). Budget suite: 5/5 green.Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.