Start learning free

LESSON 40 OF 129 · AI ENGINEERING: TOOL USE

The Tool Loop in Python

Practice

So far the model asked for one tool, you ran it, and the model answered. Real questions are not that tidy. One reply can hold several tool requests, and after it gets their results the model may ask for more before it answers.

Your program can't know how many rounds a question needs, so it doesn't count them: it loops. As long as the reply's stop_reason is "tool_use", it runs every tool the reply asks for, sends all the results back, and asks the model again. This is the tool loop.

A loop without a count: while repeats as long as its test is true. This one reads a catalog page by page, without knowing how many pages there are:

page = get_page(1)
while page["next"] is not None:
    page = get_page(page["next"])

One rule: every tool_use block needs its own tool_result with the same id, and all the results of one reply go back together in one user message. If a result is missing, the API refuses the next call.

The customer asks Which of my orders 4, 6 and 7 have shipped? The model asks for orders 4 and 6, then for order 7, and only then answers. Your program must print this (the check adds the last line: it counts your calls to the model):

Tool: lookup_order {'order_id': 4} -> {'id': 4, 'ordered': '2026-04-18', 'status': 'shipped'}
Tool: lookup_order {'order_id': 6} -> {'id': 6, 'ordered': '2026-05-21', 'status': 'cancelled'}
Tool: lookup_order {'order_id': 7} -> {'id': 7, 'ordered': '2026-05-30', 'status': 'paid'}
Answer: Only order 4 has shipped. Order 6 was cancelled, and order 7 is paid but hasn't shipped yet.
Model calls: 3

Your task: write run_tools(reply), which runs every tool request in a reply and adds the round to messages. Then write a while loop that calls it as long as the model asks for tools.


Assignment
Follow the Coding Tutorial to build run_tools and the tool loop around it.


Hint
The two messages.append lines come after the for loop, not inside it: one assistant turn and one user turn for each reply. Inside the while, ask the model again, or reply never changes and the loop never ends.


STEP 1 OF 12
    Ctrl + Enter to check

    Ai Tool Loop 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