Skip to content

Add max_cost_per_turn: hard USD cap per turn via llm-budget - #4103

Open
Mattbusel wants to merge 1 commit into
tailcallhq:mainfrom
tensorust-ai:deps/llm-budget-turn-cost-cap
Open

Mattbusel wants to merge 1 commit into
tailcallhq:mainfrom
tensorust-ai:deps/llm-budget-turn-cost-cap

Conversation

@Mattbusel

Copy link
Copy Markdown

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 adds max_cost_per_turn, a hard limit in USD for one turn.

Why the existing Usage.cost field is not enough on its own: it is only filled when the provider reports a cost (the OpenRouter-style cost field, the Anthropic ping cost, the Google cost field, and the openai_responses path). A direct Anthropic or OpenAI response leaves it None, so a cap built on that field alone would book $0 for those requests and never fire.

How it works:

  • Each finished request in the turn is booked against the cap. If the response carries a reported cost, that amount is used as is.
  • If it does not, the request is priced from its token counts (uncached input, cached input, output) using the llm-budget crate, which looks the model up in llm-prices. Each price row there records the provider pricing page it came from and the date it was checked.
  • If a request has no reported cost and the model has no known price, the turn stops with a clear message instead of booking the request at $0. A cap that silently counts a request as free does not cap anything.
  • The stop takes effect after the current request's tool calls are recorded, so once the cap is hit no further model request is made in that turn.

Disclosure: I maintain llm-budget and llm-prices.

Changes:

  • Cargo.toml, crates/forge_app/Cargo.toml, Cargo.lock: add llm-budget = { version = "0.10.0", default-features = false }. The lock file gains only llm-budget and llm-prices; serde, serde_json and thiserror were already in the tree.
  • crates/forge_config/src/config.rs, forge.schema.json: new optional max_cost_per_turn: Option<f64>. Unset means no dollar cap, so default behavior does not change.
  • crates/forge_app/src/turn_budget.rs: new TurnBudget wrapper around llm_budget::AgentBudget.
  • crates/forge_app/src/orch.rs: build a TurnBudget per turn when the setting is present, book each finished request, and send ChatResponse::Interrupt when it reports a stop.
  • crates/forge_domain/src/chat_response.rs: two new InterruptionReason variants, MaxCostPerTurnLimitReached { limit_usd, spent_usd } and UnpricedRequest { model, reason }. Eq is dropped from the derive because the new variant holds f64; nothing relied on Eq (ChatResponse and 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):

  • a reported cost stops the turn once the cap is reached (0.03 + 0.03 against a 0.05 cap);
  • a response with no reported cost is priced from tokens (gpt-4o, 10k input at $2.50/M plus 500 output at $10/M, books $0.03);
  • an unknown model with no reported cost stops the turn as UnpricedRequest and books nothing;
  • a negative limit is rejected.

cargo clippy -p forge_app -p forge_main -p forge_domain -p forge_config --all-targets is clean and cargo fmt --check passes.

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.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants