By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Binding a tool gets you a tool_calls request. Something still has to run the function and tell the model what happened. That is the tool loop, and it is smaller than it sounds: execute each requested call, append one ToolMessage per call with the matching tool_call_id, and call the model again. This lesson runs that round by hand with a deterministic stand-in, so you can see exactly what the agent runtime will later do for you.
from langchain.messages import AIMessage, HumanMessage, ToolCall, ToolMessage
from langchain.tools import tool
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
@tool
def get_weather(city: str) -> str:
"""Return the current weather for a city."""
return f"{city}: 31°C, humid, light rain at 17:00"
# Deterministic stand-in: turn 1 asks for the tool, turn 2 is the final answer.
model = GenericFakeChatModel(
messages=iter(
[
AIMessage(
content="",
tool_calls=[
ToolCall(
name="get_weather", args={"city": "Mumbai"}, id="call_1"
)
],
),
AIMessage(content="Rain is likely in Mumbai this evening. Take an umbrella."),
]
)
)
messages = [HumanMessage("Will it rain in Mumbai this evening?")]
# 1. Model turn: it asks for a tool; it has not answered yet.
reply = model.invoke(messages)
messages.append(reply)
# 2. Your code executes each request and appends the result.
for call in reply.tool_calls:
result = get_weather.invoke(call["args"])
messages.append(
ToolMessage(content=str(result), tool_call_id=call["id"], name=call["name"])
)
# 3. Model turn again: now it can answer from the tool result.
final = model.invoke(messages)
print(final.content)AIMessage whose tool_calls list names one or more tools with JSON arguments. Nothing has run yet.ToolMessage per call, with tool_call_id set to the id from the request. A mismatch is a conversation the provider will reject; the id is how the model ties result to request.One model turn can carry several tool calls at once. Execute them in any order, but append one ToolMessage per id — do not merge results into one message, and do not drop a call because it failed.
A tool that raises inside the loop can kill the run and lose the conversation. Prefer catching the error inside the tool and returning a compact, honest failure — {'error': 'unknown order 9999'} — so the model can apologise, ask for a correction, or offer another route. Keep the exception in your logs, not in the customer's way.
ToolMessage explaining why, so the model can offer the customer another route instead of retrying.Key takeaways
The tool loop is: model asks for tool calls, you execute them, append ToolMessage results, call the model again.
Each ToolMessage must match an AIMessage tool call by tool_call_id.
Return tool failures as readable results so the model can recover instead of crashing the run.
A loop ends on plain text, a step limit or a policy veto — not on hope.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.