Run It and Answer Back
executing a tool and sending tool result
Part of: Tool-Using Agent
The model asked for the calculator. You found the request. Now comes the part that makes this an agent instead of a fancy parser: you run the function, and you send the answer back in a specific shape the model can use. Step 1: run your own Python function The model never executes code. input["expression"] is a string it wants evaluated, and you write real Python to do that safely. Never run raw eval on untrusted text; a later lesson covers why: Step 2: send the result back as a tool result The API needs two messages appended to your history. First the assistant's turn, exactly what it sent you, tool use block and all. Then a user turn holding a tool result block that names which request it answers: Why the tool use id matters tool result is a user-role message, which feels backwards since your code wrote it, not a person. But from the API's side, anything that isn't the assistant is the other side of the conversation. The tool use id is what stitches the result to its request. If the model asks for two tools in one turn, each result has to carry the matching id, or the model can't tell which answer belongs to which question. Why this is the whole mechanism This round trip is all t
Challenge: Match Tool Results