Menu
CoddyTech

Parallel Tool Calls

Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 7 of 22.

When a request holds several independent questions, the model asks for all the tool calls in one response. Each call is its own tool_use block with its own id:

messages = [{"role": "user", "content": "Look up the opening hours for Luna Cafe and Mango House"}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=TOOLS, messages=messages)
calls = [block for block in response.content if block.type == "tool_use"]
for block in calls:
    print(block.name, block.input)

This prints two lines: get_hours {'place': 'Luna Cafe'} and get_hours {'place': 'Mango House'}.

Run every call, then send all the results back together: one user message whose content is a list with one tool_result per call. The API checks this. If the message after the tool calls lacks a result for any of their ids, the request fails with a 400 error, so a loop that sends one message per result breaks as soon as two calls arrive. Sending them together also tells the model it may keep asking for several calls at once, which saves a round trip each time.

challenge icon

Challenge

Medium

The weather agent in the starter breaks when it asks for two cities: it sends each result in its own message. Fix the part inside the loop.

  1. Collect the tool_use blocks of the response and print Calls: and how many there are.
  2. Run get_weather for each one and put every tool_result in one list.
  3. Append that list as a single user message.

Keep the rest of the program as it is. For example, for the input What is the weather in Oslo and Lima? the program prints:

Calls: 2
Answer: Here is what I found:
- Oslo: 4 C and rainy
- Lima: 19 C and misty

Try it yourself

import coddy_ai as anthropic  # in production: import anthropic

client = anthropic.Anthropic()

WEATHER = {
    "Paris": "18 C and sunny",
    "Tokyo": "22 C and cloudy",
    "Rome": "25 C and sunny",
    "Oslo": "4 C and rainy",
    "Cairo": "31 C and clear",
    "Lima": "19 C and misty",
}


def get_weather(city):
    return f"{city}: {WEATHER.get(city, 'no data')}"


TOOLS = [
    {
        "name": "get_weather",
        "description": "Get the current weather for a city",
        "input_schema": {
            "type": "object",
            "properties": {"city": {"type": "string", "description": "The city name"}},
            "required": ["city"],
        },
    }
]

request = input()
messages = [{"role": "user", "content": request}]

response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=TOOLS, messages=messages)
while response.stop_reason == "tool_use":
    messages.append({"role": "assistant", "content": response.content})
    # Fix this part: print "Calls: " and the number of tool calls in this response,
    # then send all the results in ONE user message
    for block in response.content:
        if block.type == "tool_use":
            result = {"type": "tool_result", "tool_use_id": block.id, "content": get_weather(**block.input)}
            messages.append({"role": "user", "content": [result]})
    response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=TOOLS, messages=messages)

for block in response.content:
    if block.type == "text":
        print("Answer: " + block.text)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Tools and Agents

Practice on your own: Online Python compiler