LangChain/Models
Intermediate14 min

Tool Execution Loop

Tool calling follows a three-step cycle — invoke the model, execute its tool calls, pass results back as ToolMessages, repeat. Understanding this loop is the foundation of every tool-using agent, but shipping it to production requires error handling, token budget awareness, and a clear decision on when to use a manual loop versus create_agent.

Quick Reference

  • model.bind_tools([fn]) attaches tool schemas to the model for the session
  • ai_msg.tool_calls is a list — handle all of them before the next invoke
  • tool.invoke(tool_call) executes and returns a ToolMessage automatically
  • tool_choice='any' forces at least one tool call; use only on the first turn
  • tool_choice={'name': 'fn_name'} forces a specific tool
  • Always wrap tool execution in try/except — return errors as ToolMessages
  • Set MAX_ITERATIONS (10 is a safe default) — bare while True loops are dangerous
  • from langchain.agents import create_agent for production agents

The Three-Step 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.

LLM (ChatModel)Decides: call a tool or respond?.bind_tools([search, calc, ...])tool_callsTool Executionweb_search()calculator()api_call()Executes requested tool, returns resultToolMessageLoopuntil donefinal answerResponseDelivered to user

The tool calling cycle: LLM requests tools, tools return results, LLM decides next step

The complete tool call cycle