Skip to content

Write the loop

Read · 1 of 2

Two things real loops must handle

Parallel calls. A model can ask for several tools in one reply ("look up all three prices"). Run them all and send all the results back in one message.

Failing tools. If a tool raises, send ERROR and the message as its result. The model decides what to do next. Your loop keeps going.

Read this lesson as text

Parallel tool calls and a tool that fails: your loop handles both.

Two things real loops must handle

Parallel calls. A model can ask for several tools in one reply ("look up all three prices"). Run them all and send all the results back in one message.

Failing tools. If a tool raises, send ERROR and the message as its result. The model decides what to do next. Your loop keeps going.

Exercise: The shopping agent

Input: a shopping list like 3 apple 2 banana. The stand-in model asks for every price at once with get_price(item).

Write the loop. For each tool call print tool: get_price <item> -> <result>, where a failing call's result is ERROR <message>. When the model is done, print its answer.