Start learning free

LESSON 43 OF 129 · AI ENGINEERING: TOOL USE

Several Calls at Once in Python

Practice

A reply from the model is a list of blocks, and it can hold several tool requests at once. Each request is a tool_use block with its own id. Fewer calls to the model make the conversation faster and cheaper.

Say a weather assistant is asked about two cities. One reply can ask for both, and you answer both in one user message:

assistant:  text         "I'll check both cities."
            tool_use     id toolu_A   get_weather {"city": "Paris"}
            tool_use     id toolu_B   get_weather {"city": "Rome"}
user:       tool_result  tool_use_id toolu_A   {"temp": 18}
            tool_result  tool_use_id toolu_B   {"temp": 24}

The model waits for an answer to every request of its reply. A message with only some of the results is refused. Each result finds its request through its tool_use_id, so one result can be an error while the other is fine.

You can ask the model to work this way with a system prompt. With it, your finished program needs two calls to the model instead of four, and prints:

Model: I'll look up both orders.
Tool: lookup_order {'order_id': 4} -> {'id': 4, 'ordered': '2026-04-18', 'status': 'shipped'}
Tool: lookup_order {'order_id': 40} -> {'error': 'Order not found'}
Tool results sent in one message: 2
Answer: Order 4 has shipped. I couldn't find an order with the number 40. Could you check that number?

Your task: create the system prompt SYSTEM and send it with every call, print the model's text blocks, and print how many tool results go back in the one message.


Assignment
Follow the Coding Tutorial to let the model ask for both lookups in one reply and to send both results back in one message.


Hint
The for loop in run_tools already runs every request and collects every result. A block's type tells you whether it is text or a tool request.


STEP 1 OF 5
    Ctrl + Enter to check

    Ai Parallel Tools 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