Cost is the control that works when time and iteration caps do not: a loop that is fast and short still burns tokens on every round. CrewAI’s spend controls are max_iter, max_execution_time and max_rpm on agents, max_tokens on tasks, and a crew-level max_rpm, and the guidance is to cap spend before the first long run. Set the budget per task from a normal run’s token use, such as three times normal, and make the run stop with a message when the budget is spent, not silently truncate the answer. A budget you only discover in the invoice is not a budget.
What you need
- A normal run’s token use, read from the log or the trace
- A budget to enforce, for example three times the normal use
- A way to stop the run when the budget is spent
Step 1: Measure a normal run’s token use
Read the trace or the usage metrics for a normal run and note the tokens per task. A budget set without a measurement is a guess.
Step 2: Set the per-task token budget
Set max_tokens on the task, or the equivalent budget in your framework, to a small multiple of the normal use. Record the budget next to the task so it is visible.
Step 3: Run a looping task and check it stops on cost
Run a task that would loop and confirm it stops when the budget is spent, with a message that it stopped on cost. If it stops on time instead, the token budget is not the binding limit.
Worked example: synthetic token budget
The table below is a synthetic example. It shows the expected result of the token budget; no agent was run.
| Check | Expected | Result |
|---|---|---|
| Normal run uses 4,000 tokens | Yes | Pass |
| Budget set to 12,000 tokens | Yes | Pass |
| Looping run stops at the budget | Yes | Pass |
| Run reports it stopped on cost | Yes | Pass |
Expected result (UNTESTED). the looping run stops at the 12,000-token budget and reports that it stopped on cost. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- A budget only binds if the framework enforces it; a number in the instructions does not stop a run.
- Token use varies by task, so a budget that fits one task may starve another.
- A run that stops on cost returns a partial result, which must be marked partial.
- Cost limits that only email an alert never pause a run; treat them as alarms, not stops.
