Build a tool-calling loop with explicit termination
In this article (5 sections)
A tool loop without an exit contract can repeat the same lookup, spend indefinitely or hide partial failure. Define terminal states before the first call.
Execute a bounded loop
The agent controls lab runs two in-memory steps.
from agent_cases import tool_loop_case
result = tool_loop_case()
assert result["maximum_calls"] == 3
assert len(result["calls"]) == 2
assert result["status"] == "success"
assert result["explicit_termination"] is TrueThe policy lookup returns found; calculation returns complete, and the loop stops below its three-call limit. This tests control flow, not model planning.
Define terminal states and budgets
Use success, permanent failure, approval pending, cancelled and budget exhausted as explicit states. Bound tool calls, total elapsed time, repeated identical actions and model tokens/cost. A timeout in one step must respect the overall deadline.
Validate every proposed call against an allowlist and typed arguments. Detect no-progress cycles by hashing action plus relevant state. Do not let a model declare success when required invariants or outputs are missing.
Test failure paths
Simulate a tool that always times out, repeated invalid arguments, empty results and contradictory outputs. Verify useful final status, preserved trace and no side effect after the budget. Check streaming/cancellation cleanup.
The Generative & Agentic AI course connects termination to typed state, retries and runbooks.
Exercise
Implement a loop with five terminal states and three budgets. Inject no-progress repetition and a permanent error, then prove both stop with reproducible traces.
Continue learning
This article is part of the Agent workflows and state sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Agent versus workflow: choose the least complex reliable design.
- Continue with Validate tool arguments before execution.
Reference: OpenAI function-calling guide.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Generative & Agentic AI programme — 3 months. Add practical GenAI, retrieval and agent-building skills to your existing toolkit.
Explore Generative & Agentic AI