Decides whether to call a tool or provide final output
AI Models & Assistants / Knowledge library
Tool-Use Loop
Alternates model decisions with tool execution until the agent returns a final result
Why learn Tool-Use Loop?
It lets a model gather information and change external state over multiple grounded steps.
The problem it solves
It connects reasoning with actions whose results were not available in the original prompt.
Where it came from
In a tool-use loop, the model receives current context, emits one or more structured tool calls, receives their results, and decides again. Limits, error handling, permissions, and idempotency belong to the harness around this loop.
Build your understanding
Work through these exercises in order. Try the task before opening a reference.
- Define Tool-Use Loop and identify its place in an agent architecture.
- Choose a task where Tool-Use Loop improves an agent system.
- Diagnose one failure caused by misusing Tool-Use Loop.
- Interpret a trace or state transition involving Tool-Use Loop.
- Explain one operational tradeoff of Tool-Use Loop.
Documentation and sources
- OpenAI Agents SDK: Running agents (openai.github.io)
Connect the ideas
Explore next
Real World Example
Automated Inventory Reconciliation Loop
Logistics and warehouse management systems requiring real-time stock verification.
The business problem
Manual reconciliation of inventory discrepancies between digital records and physical warehouse scanners is slow and prone to human error.
How this topic solves it
The Tool-Use Loop automates this by allowing the agent to iteratively query the database, trigger physical scanner APIs, and process results until the stock count matches or a final report is generated.
- Define an Agent with access to inventory database and scanner control tools.
- Initialize a Runner to manage the execution lifecycle.
- Invoke the Runner with the initial stock discrepancy report.
- The Runner executes the loop: LLM decides on a tool, SDK executes the tool, and results are fed back to the LLM.
- The loop terminates when the LLM produces a final text output confirming the reconciliation.
The process iterates between the LLM model and the tool execution environment until a final result is reached.
Orchestrates the loop and checks for final output
Runs local function tools and returns results
- LLM Model → Emits tool call or final output → Runner
- Runner → Executes tool → Tool Execution
- Tool Execution → Returns tool output → Runner
- Runner → Feeds output back to model → LLM Model
Implementation example
This code demonstrates the Runner orchestrating the Tool-Use Loop where the agent iteratively uses tools to resolve a specific inventory task.
from agents import Agent, Runner
# Define agent with inventory tools
agent = Agent(name="InventoryAgent", tools=[query_db, trigger_scanner])
# Runner initiates the loop: LLM -> Tool -> LLM
result = await Runner.run(agent, "Reconcile SKU 12345")
# Final output is returned once the loop finishes
print(result.final_output)Expected business outcome
Reduced latency in inventory adjustments and higher accuracy in stock reporting without constant human intervention.
Limitations and checks
The loop may enter an infinite cycle if the LLM repeatedly calls the same tool with identical inputs; this is checked by monitoring the MaxTurnsExceeded exception.