The loop at a glance
Tool calling, which some providers call function calling, lets a model ask your application to do something for it, such as looking up an order. The model never runs your tool itself. It returns a structured request; your code decides whether to run it, runs it and sends back the result. That separation is what makes tool use controllable.
- 1. User request
Your app sends the conversation and the tool definitions to the model.
- 2. Tool request
The model replies with a tool name and arguments instead of an answer.
- 3. Validate
Your code checks the tool is allowed and the arguments are valid.
- 4. Approve
A person confirms any action with real-world effects.
- 5. Execute
Your code runs the function with its own, limited credentials.
- 6. Return result
Your app sends the result back, linked to the request.
- 7. Final answer
The model uses the result to answer the user.
Stages 2 to 6 can repeat several times before the final answer. Stage 4 applies only to consequential actions.
- Model
Chooses a tool and proposes arguments. Writes the final answer.
- Your application
Validates, asks for approval, executes, enforces limits.
- Person
Approves or rejects consequential actions.
Everything with an effect in the outside world happens on the application side.
What happens at each hop
Stage 1: the request carries the tool definitions
Your application sends the conversation together with a list of tools. Each tool has a name, a description that tells the model when to use it, and a parameter schema. The model knows only what these definitions say, so a vague description leads to vague tool use. Send only the tools the current task needs.
Stage 2: the model asks; it does not act
If the model decides a tool would help, its response contains a tool request: the tool's name and the arguments, often with an ID that links the eventual result to this request. Your code has to notice the request and act on it. At this point nothing has happened in the outside world.
Stage 3: validation sits in your code
Treat the request as untrusted input. Check that the tool is on the allowlist for this user and task, that the arguments match the schema, and that your business rules hold: the order belongs to this customer, the amount is within limits. If a check fails, do not execute; return an error result that says what was wrong.
Stage 4: approval sits between validation and execution
For anything consequential, such as sending a message, moving money, deleting data or changing infrastructure, pause the loop and show a person exactly what will happen. Enforce this in code so the tool cannot run without a recorded approval. Read-only tools, like the order lookups in beginner projects, normally run without one.
Stage 5: execution uses your credentials, not the model's
The function runs with whatever access your application gives it, so give it the least it needs. Apply timeouts, catch exceptions and turn them into error results, and make write operations idempotent so that a retry cannot repeat an action.
Stage 6: the result goes back as data
Send the result, or a structured error, back to the model as a tool result that refers to the original request. Keep it small and relevant. Tool results are untrusted too: a document or web page can contain text that tries to give the model new instructions.
Stage 7: the loop ends on your terms
The model may request another tool or give its final answer. Your loop should also stop when it reaches a step, time or cost limit, and tell the user what happened instead of failing silently.
The loop in code: a vendor-neutral sketch
The sketch below shows the control flow in Python. call_model, tool_result_message and ask_person_to_approve stand in for your provider's SDK and your own code, and each tool object is assumed to offer spec, validate, needs_approval and run. The shape of the loop is what matters.
MAX_STEPS = 6
def handle(request, tools):
"""Validate, approve and run one tool request. Always returns a result for the model."""
tool = tools.get(request.name)
if tool is None:
return {"error": "Unknown tool: " + request.name}
problem = tool.validate(request.arguments)
if problem:
return {"error": problem} # the model can correct its arguments and try again
if tool.needs_approval and not ask_person_to_approve(tool.name, request.arguments):
return {"error": "A person declined this action."}
return tool.run(**request.arguments)
def run_conversation(messages, tools):
for _ in range(MAX_STEPS):
reply = call_model(messages, tool_specs=[tool.spec for tool in tools.values()])
messages.append(reply.as_message())
if not reply.tool_requests:
return reply.text # no tool needed: this is the final answer
for request in reply.tool_requests:
result = handle(request, tools)
messages.append(tool_result_message(request.id, result))
return "Stopped: the step limit was reached before an answer was produced."Illustrative pseudo-code, not a runnable program.
General principle versus vendor detail
| General principle (any provider) | Vendor-specific detail (check the docs) |
|---|---|
| Your application, not the model, executes the tools you define. | The names and shapes of the tool request and tool result, and how a result is matched to its request. |
| Validate arguments before execution. | Whether a strict or validated mode holds arguments to your schema. |
| Require human approval for consequential actions. | Server-side tools, such as web search, that some providers run for you. |
| Bound the loop with step, time and cost limits. | Settings that let you require, allow or forbid tool use on a turn. |
| Treat tool results as untrusted data. | Whether several tool requests can arrive in one response. |
For the provider-specific details, see Tool use with Claude (opens in a new tab) and Function calling with the Gemini API (opens in a new tab). PromptFable is independent of both providers.
Where loops usually go wrong
- Running the request without validation because the model 'usually gets it right'.
- Letting the model's own text decide whether approval is needed, instead of a rule in code.
- Returning huge tool results that crowd the rest of the conversation out of the context window.
- Raising an exception on a bad tool request instead of returning an error the model can act on.
- Having no step limit, so a confused model can keep calling tools indefinitely.
Related lessons
- Let the Agent Use One ToolBuild Your First Claude AI Agent
- Connect Gemini to a FunctionBuild Your First Google Gemini AI Agent
- Agent Components: Model, Instructions, Tools, State and RuntimeAI Agent Development
- What Is an AI Agent? Concepts, terminology and examplesAI Agent Development
- Written by
- PromptFable Editorial
- Last updated 13 Nov 2026
- Review
- Not yet reviewed
PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.