Start learning free

LESSON 86 OF 129 · AI ENGINEERING: AGENTS

Budgets: Steps and Cost in Python

Practice

An agent picks its own steps, so it can also pick too many. Every step may look reasonable, and the bill still grows. A budget is a hard limit that your code enforces, whatever the model wants.

Two budgets. A step budget stops the loop after a fixed number of steps. A cost budget stops it when the money spent passes a limit. You pay per token, and every call sends the whole conversation again, so even a few steps can be expensive. With the example prices of $1 per million input tokens and $5 per million output tokens, a call with 2,000 input tokens and 100 output tokens costs:

(2000 * 1.00 + 100 * 5.00) / 1000000  =  0.0025 dollars

Both checks stand at the top of the loop, before the model is asked again, so a step you refuse costs nothing. Whichever budget runs out first ends the run. The agent then prints why it stopped: without that line you can't tell a finished run from one that was cut off.

Today the model doesn't stop: it checks the stock of one product after another. With budgets of 6 steps and $0.01, your program's output must end like this. The check also runs your agent on two more goals:

…
Step 6
  Thought: Now the mugs.
  Tool: check_stock {'product_id': 1} -> {'name': 'Mug', 'stock': 10}
Stopped: the step budget ran out
Steps: 6 Cost: 0.0087
Model calls: 7
The check runs the agent on order 3, with a model whose calls cost three times as much. The last two lines of that trace:
Stopped: the cost budget ran out
Steps: 2 Cost: 0.0109
The check runs the agent on order 4, which the model answers at step 2. The last two lines of that trace:
Stopped: the model answered
Steps: 2 Cost: 0.0036

Your task: the function cost_of and the two prices are ready. Set MAX_STEPS to 6 and MAX_COST to 0.01, add up the cost of every call (the plan call too), put the two budget checks at the top of the loop, and after the loop print the reason, the steps and the cost.


Assignment
Follow the Coding Tutorial to give the agent a step budget and a cost budget, and to print why it stopped.


Hint
The loop has three ways out, and each one sets reason before its break. The step check compares step with MAX_STEPS before step grows, and the cost is printed with round(cost, 4).


STEP 1 OF 12
    Ctrl + Enter to check

    Ai Budgets in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free