Intermediate7 min
Tool Execution Loop
When a model returns tool calls, execute them and pass results back as ToolMessages. This three-step cycle — invoke → execute → invoke again — is the foundation of every tool-using agent.
Quick Reference
- →model.bind_tools([fn]) binds tools to the model for the entire session
- →ai_msg.tool_calls is a list of {'name', 'args', 'id'} dicts
- →tool.invoke(tool_call) executes the tool and returns a ToolMessage
- →tool_choice='any' forces the model to call at least one tool
- →tool_choice={'name': 'fn_name'} forces a specific tool
The Execution Cycle
Tool calling follows a three-step cycle: the model generates a tool call, you execute it, then you pass the result back so the model can synthesize a final answer. The model can call multiple tools across multiple turns before producing a final response — you keep looping until ai_msg.tool_calls is empty.
Complete tool call cycle