Lesson 20 — Planning: break a goal into steps
Plan first, then act
For a bigger goal, an agent can ask the model for a plan first: a list of tool calls in order. Then the code runs them one by one.
Later steps often need earlier results. Our plan uses placeholders: "$1" means "the result of step 1". The helper fill() swaps them for the real values just before each step runs.
{"tool": "calculator", "args": {"expression": "125 - $1"}}
# after step 1 returned 18.75, this becomes "125 - 18.75"
Why it matters: planning makes long tasks more reliable and easier to debug — you can print the plan and see where it went wrong. The trade-off: a plan made at the start can be wrong, so advanced agents re-plan when a step fails.
Inside the loop: call the step's tool with **args, append the result to results, and print "step <n>: <tool> <args> -> <result>".
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based AI Agents Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for AI Agents Foundations books — check the publication year so the examples match current versions.
Search Amazon →