8/17
Part 3 of 7 · When things go wrongLearn and do

Errors go back to the model as results, and every loop needs limits

When a tool fails in the middle of a run

The order service will time out some day, in the middle of a run. If the harness lets that exception escape, the run dies and the customer gets nothing. Anthropic's docs say to send a tool_result block with is_error set to true when a tool fails or a call is invalid, so the model can decide what to do next.

The code below uses run_tool(tools, name, args), which runs one call and returns a result with an is_error flag. You write its last piece in this step's task. Here it calls an order service that times out once, then calls it again, then asks for a tool that doesn't exist.

tries = {"n": 0}


def flaky_lookup(order_id: str) -> str:
    """The order service times out on its first call."""
    tries["n"] += 1
    if tries["n"] == 1:
        raise TimeoutError("order service timed out")
    return look_up_order(order_id)


print(run_tool({"look_up_order": flaky_lookup}, "look_up_order", {"order_id": "A17"}))
print(run_tool({"look_up_order": flaky_lookup}, "look_up_order", {"order_id": "A17"}))
print(run_tool({}, "cancel_everything", {}))
Predict

The first call to flaky_lookup raises a timeout. What does the second print line show?

Pick one to see what it prints.
Pick an answer above to see the rest