Generative AI & Agentic AIAgent workflows and state

Build a tool-calling loop with explicit termination

PK
Pankit Kumar
Sr. Data Scientist at Parexel (a Goldman Sachs–backed company) · 20 September 2026 · 2 min read
Technically reviewed by Ishaan Sharma
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.

python
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 True

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

Reference: OpenAI function-calling guide.

PK
Pankit Kumar
Lead Instructor, NeuraPath Academy

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
Counselling is free · no obligation

Not sure which programme fits?

Tell us your background and we will map it to the right entry point — including saying so when a cheaper programme is the better fit. A counsellor replies within one working day.