How do you define a cost per task budget for an agent and enforce it at runtime?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Define cost per task budgeting for an agent. Describe the enforcement mechanism and explain what happens when the budget is exceeded mid-task.
A cost per task budget caps the dollars one agent run may spend on tokens plus tool fees, enforced by a running counter that injects a graceful stop signal before the cap.
Imagine you give a worker a prepaid card and one job to finish. Every phone call they make and every paid lookup they buy comes off that card. You do not want them to keep spending forever, so you watch the balance. When the card is nearly empty, you do not snatch the phone away mid-sentence and lose everything they were doing. Instead you tap them on the shoulder and say the money is almost gone, please wrap up and tell me what you found so far. They write a short summary and stop. An agent budget works the same way. The card balance is the running cost, the tap on the shoulder is a polite message added to its notes, and the summary is a partial answer instead of a crash that throws away all the work.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Define cost per task as token plus tool cost over one run, explain the running counter and conservative pre-flight check, contrast graceful synthetic error injection with a hard exception, stress that a crash loses paid work, then close with tiered degradation and cost per successful task as the metric that matters.
def step_guard(state, budget):
spent = state.token_cost + state.tool_cost
next_estimate = budget.per_call + budget.per_tool
if spent + next_estimate > budget.cap:
# soft stop: model sees this and summarises
state.observations.append(
{"role": "tool", "name": "budget_exceeded",
"content": f"Budget cap ${budget.cap} reached. "
"Return a partial answer now."}
)
return "stop_signal_injected"
return "ok"| Stop strategy | How it triggers | Effect on completed work |
|---|---|---|
| Hard termination | Runtime raises an exception when the cap is hit | All in-progress work is discarded; user gets a crash |
| Graceful stop | Runtime injects a synthetic budget exceeded observation | Model summarises progress and returns a partial answer |
| Tiered degrade | Spend crosses a soft threshold below the cap | Loop switches to a cheaper model to extend the run |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Enforcing the cap by throwing an exception that kills the loop. A hard crash discards every step already paid for, instead of letting the model summarise and exit cleanly.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.