Repository navigation
Conversation
Each finished request in a turn is booked against the cap, using the provider-reported cost when present and llm-budget's dated price table otherwise. A request that cannot be priced stops the turn instead of being booked at zero. Adds InterruptionReason variants for both cases.
|
|
This branch has not been deployed
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.
Forge can cap a turn by request count (
max_requests_per_turn) and by turns (max_turns), but it has no way to cap a turn by money. This PR addsmax_cost_per_turn, a hard limit in USD for one turn.Why the existing
Usage.costfield is not enough on its own: it is only filled when the provider reports a cost (the OpenRouter-stylecostfield, the Anthropic ping cost, the Google cost field, and theopenai_responsespath). A direct Anthropic or OpenAI response leaves itNone, so a cap built on that field alone would book $0 for those requests and never fire.How it works:
llm-budgetcrate, which looks the model up inllm-prices. Each price row there records the provider pricing page it came from and the date it was checked.Disclosure: I maintain llm-budget and llm-prices.
Changes:
Cargo.toml,crates/forge_app/Cargo.toml,Cargo.lock: addllm-budget = { version = "0.10.0", default-features = false }. The lock file gains onlyllm-budgetandllm-prices;serde,serde_jsonandthiserrorwere already in the tree.crates/forge_config/src/config.rs,forge.schema.json: new optionalmax_cost_per_turn: Option<f64>. Unset means no dollar cap, so default behavior does not change.crates/forge_app/src/turn_budget.rs: newTurnBudgetwrapper aroundllm_budget::AgentBudget.crates/forge_app/src/orch.rs: build aTurnBudgetper turn when the setting is present, book each finished request, and sendChatResponse::Interruptwhen it reports a stop.crates/forge_domain/src/chat_response.rs: two newInterruptionReasonvariants,MaxCostPerTurnLimitReached { limit_usd, spent_usd }andUnpricedRequest { model, reason }.Eqis dropped from the derive because the new variant holdsf64; nothing relied onEq(ChatResponseand the error type do not derive it).crates/forge_main/src/ui.rs: messages for the two new variants.Tests (
cargo test -p forge_app --lib turn_budget, 4 passed):UnpricedRequestand books nothing;cargo clippy -p forge_app -p forge_main -p forge_domain -p forge_config --all-targetsis clean andcargo fmt --checkpasses.