This prompt is designed for platform operators and reliability engineers who need an automated, model-driven circuit breaker for agent tool execution. It is not a replacement for hard limits in your orchestration layer—those should always be your first line of defense. Instead, this prompt acts as an intelligent last resort that analyzes execution traces to decide whether an agent is stuck in a non-progressing loop, making repeated identical calls, or burning budget without advancing state. Use it when you need a termination decision with auditable reason codes and a clear escalation path before a runaway loop exhausts your API budget or hits rate limits.
Prompt
Runaway Tool Loop Detection and Termination Prompt

When to Use This Prompt
Deploy an intelligent circuit breaker that analyzes agent execution traces to detect and terminate non-progressing tool loops before they exhaust your API budget.
The ideal deployment point is inside a middleware or guard layer that intercepts tool call sequences before they reach the model for the next planning step. You should feed this prompt a rolling window of recent tool calls, their arguments, their outputs, and any state-change indicators. The prompt returns a structured decision: continue, warn, or terminate. For terminate decisions, it must produce a reason code (such as repeated_identical_calls, no_state_progress, or budget_exhaustion_risk) and a recommended escalation path. This gives your operations team an auditable record of why the agent was stopped, rather than a silent timeout or a cryptic rate-limit error from your API provider.
Do not use this prompt as your primary rate limiter or budget enforcer. Hard numeric caps on call count, token usage, and wall-clock time belong in your orchestration code, not in a model's judgment. This prompt is for the ambiguous cases where a loop is technically within limits but semantically stuck—for example, an agent that retries a failing API call with slightly different parameters 40 times, each attempt consuming tokens but never succeeding. Your hard limits might allow 50 calls, but this prompt can detect the futility pattern at call 12 and terminate early, saving budget and reducing latency for other tasks in the queue.
Before deploying, test this prompt against a library of known loop patterns: rapid successive identical calls, calls with rotating but equivalent arguments, calls that produce outputs indistinguishable from prior outputs, and calls that toggle between two states without converging. You will also need to tune the trace window size—too small and you miss the pattern, too large and you add latency and token cost to every evaluation. Start with a window of 10-15 recent calls and adjust based on your agent's typical task depth. Wire the termination decision into your observability stack so you can track false positives (legitimate exploration terminated early) and false negatives (loops that slipped through) and refine the prompt's sensitivity over time.
Use Case Fit
Where the Runaway Tool Loop Detection and Termination Prompt works, where it fails, and the operational prerequisites for safe deployment.
Good Fit: Autonomous Agent Orchestrators
Use when: An agent is given a high-level goal and allowed to plan and execute multi-step tool sequences without human intervention. The prompt acts as a circuit breaker for planning loops or repetitive tool calls that make no progress. Guardrail: Combine with a hard execution timeout and a maximum step count at the harness level.
Bad Fit: Single-Step Tool Calls
Avoid when: The system makes exactly one tool call per user request with no iterative planning. The overhead of loop detection logic adds latency and false-positive risk for simple retrieval or classification calls. Guardrail: Use a lightweight timeout instead of a full loop-detection prompt for single-shot invocations.
Required Input: Structured Execution Trace
Risk: The prompt cannot detect loops from a single call; it needs a history of tool names, arguments, timestamps, and observed state changes. Without this, it will either miss loops or flag normal retries. Guardrail: Feed the prompt a rolling window of the last N tool calls with their inputs, outputs, and timestamps in a consistent JSON schema.
Operational Risk: False-Positive Termination
Risk: A legitimate retry loop with exponential backoff or a long-running data processing sequence may be misclassified as runaway, killing a valid workflow. Guardrail: Require the prompt to output a confidence score and distinct reason codes. Escalate medium-confidence decisions to a human-in-the-loop queue instead of auto-terminating.
Operational Risk: Adversarial Loop Induction
Risk: A malicious user or compromised tool response could craft inputs that cause the agent to loop while evading the detection prompt's pattern matching. Guardrail: Run the detection prompt on a separate, minimal-context model instance. Do not include the full user conversation history in the detection prompt's input window.
Bad Fit: Real-Time, Sub-Second Decision Loops
Avoid when: The agent's tool-calling loop runs at high frequency (e.g., robotic control, high-frequency trading simulations) where the detection prompt's inference latency exceeds the loop interval. Guardrail: Use deterministic, rule-based circuit breakers (e.g., identical argument repeat count) for sub-second loops and reserve the LLM-based prompt for asynchronous, post-hoc analysis and session termination.
Copy-Ready Prompt Template
A copy-ready system prompt for detecting runaway tool loops and returning a structured termination decision.
The following prompt template is designed to be injected into an agent's system instructions or used as a guardrail evaluation step between tool calls. It analyzes a provided execution trace—a sequence of recent tool calls, their arguments, and their results—and determines whether the agent is stuck in a non-productive loop. The output is a strict JSON decision object that your application harness can use to halt execution, escalate to a human, or switch to a fallback strategy. Use square-bracket placeholders to inject the specific trace, policy thresholds, and output schema before sending the request to the model.
textYou are a runtime safety monitor for an autonomous agent. Your sole task is to analyze the provided agent execution trace and determine if a runaway tool loop is occurring. A runaway loop is defined as a sequence of tool calls that exhibits one or more of the following patterns without making meaningful progress toward the stated goal: - Repeating the same tool call with identical or trivially different arguments more than [MAX_REPEATED_CALLS] times. - Cycling through a set of tool calls (A -> B -> C -> A) without a change in external state. - Making tool calls whose results do not alter the agent's subsequent actions or understanding. - Exceeding [MAX_CONSECUTIVE_CALLS] total tool calls without a terminal action or user-facing output. You will be given: - [AGENT_GOAL]: The high-level objective the agent is trying to accomplish. - [EXECUTION_TRACE]: A JSON array of the last [TRACE_WINDOW_SIZE] tool calls, each containing `tool_name`, `arguments`, `result_summary`, and `timestamp`. Analyze the trace and return a single JSON object conforming to the [OUTPUT_SCHEMA]. [OUTPUT_SCHEMA] { "termination_decision": "terminate" | "continue" | "escalate", "confidence": 0.0-1.0, "reason_code": "repeated_call" | "cyclical_loop" | "no_progress" | "call_limit_exceeded" | "healthy", "loop_pattern_identified": "string describing the detected loop, or null", "recommended_action": "A concise instruction for the harness, e.g., 'Stop execution and return the last valid state to the user.'" } [CONSTRAINTS] - If `termination_decision` is "escalate", the `recommended_action` must include a clear summary of the loop for a human operator. - Do not hallucinate tool calls or results not present in the [EXECUTION_TRACE]. - If the trace shows clear, non-repeating progress, the decision must be "continue" with reason_code "healthy".
To adapt this template, adjust the [MAX_REPEATED_CALLS] and [MAX_CONSECUTIVE_CALLS] thresholds to match your application's tolerance for latency and cost. For high-stakes financial or healthcare agents, set these thresholds low and prefer an escalate decision. The [TRACE_WINDOW_SIZE] should be large enough to capture a full loop cycle but small enough to fit within your context budget; a window of 20-30 calls is a practical starting point. The harness that processes this output should treat any confidence score below 0.85 for a terminate decision as a signal to log the trace for human review rather than automatically halting, preventing premature termination from a false positive.
Prompt Variables
Inputs required for the Runaway Tool Loop Detection and Termination Prompt. Each placeholder must be populated before the prompt is assembled and sent to the model. Validation notes describe how to verify the input is well-formed before execution.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[TOOL_CALL_HISTORY] | Ordered list of recent tool calls with timestamps, arguments, and results for loop detection analysis | [{"tool":"search","args":{"q":"acme revenue"},"result":"...","ts":"2025-01-15T10:00:00Z"},{"tool":"search","args":{"q":"acme revenue"},"result":"...","ts":"2025-01-15T10:00:02Z"}] | Must be a valid JSON array. Each entry requires tool, args, result, and ts fields. Array length must be at least 2 for loop detection. Timestamps must be ISO 8601 and monotonically increasing. |
[MAX_CONSECUTIVE_IDENTICAL_CALLS] | Threshold for identical tool-argument pairs before triggering termination | 3 | Must be a positive integer. Values below 2 disable detection. Recommended range: 2-5. Parse as integer and reject non-numeric or negative values. |
[MAX_CALLS_WITHOUT_STATE_CHANGE] | Maximum allowed tool calls where no observable state progress occurs | 5 | Must be a positive integer. State change is defined by the application layer. Null allowed if this check is disabled. If set, must be >= [MAX_CONSECUTIVE_IDENTICAL_CALLS]. |
[SESSION_TIME_BUDGET_SECONDS] | Total wall-clock time allowed for tool execution before forced termination | 300 | Must be a positive integer or null. If null, time-based termination is disabled. If set, compare against elapsed time from first tool call timestamp in [TOOL_CALL_HISTORY]. |
[TERMINATION_REASON_CODES] | Allowed reason codes the model can output when recommending termination | ["IDENTICAL_CALL_LOOP","NO_STATE_PROGRESS","TIME_BUDGET_EXCEEDED","REPEATED_FAILURE"] | Must be a non-empty JSON array of strings. Each code must match an enum defined in the system. Reject unknown codes. Validate against allowed set before prompt assembly. |
[ESCALATION_POLICY] | Instructions for what happens after termination: who to notify, what metadata to include, and whether a human can override | {"notify":"oncall-channel","include_trace":true,"allow_override":false,"cooldown_seconds":60} | Must be a valid JSON object with required keys: notify (string), include_trace (boolean), allow_override (boolean), cooldown_seconds (positive integer or null). Reject if required keys are missing or types mismatch. |
[CURRENT_TASK_CONTEXT] | Description of the task the agent is attempting to complete, used to assess whether repeated calls are expected or pathological | "Research ACME Corp's Q4 2024 financial performance and summarize key metrics" | Must be a non-empty string. Max 2000 characters. Should describe the goal, not the steps. Used by the model to distinguish legitimate multi-step retrieval from stuck loops. |
Implementation Harness Notes
How to wire the runaway loop detection prompt into an agent orchestration layer with validation, circuit breakers, and escalation.
This prompt is not a standalone safety net. It must be embedded inside the agent's execution loop as a pre-invocation guard that runs before every tool call after a configurable threshold (e.g., after 5 consecutive tool calls or 30 seconds of tool-only activity without a user-visible response). The harness should maintain a rolling window of recent tool calls—including tool name, arguments, timestamp, and whether the call produced a state change—and pass that window into the prompt's [TOOL_CALL_HISTORY] placeholder. Do not rely on the model's internal context window alone; the harness must own the ground-truth counter and pass it explicitly.
Validation and circuit-breaking happen in the application layer, not inside the model. Parse the model's JSON output and enforce these rules: if termination_decision is terminate but reason_code is missing or not from the allowed enum, treat it as continue and log a malformed-output incident. If the termination threshold is breached (e.g., 3 termination decisions within a single session), the harness must hard-stop all tool execution and escalate to a human operator via the configured [ESCALATION_CHANNEL]. Implement a cooldown window after any termination: block all tool calls for that session for a minimum period (e.g., 60 seconds) and surface the reason to the user. Log every termination decision with the full tool-call history, model response, and session ID for post-incident analysis.
Model choice matters. Use a fast, cheap model for this guard prompt (e.g., a small fine-tuned classifier or a low-latency API model) because it runs on every tool call after the threshold. The guard model does not need the full reasoning capability of the primary agent model; it needs consistent JSON output and low false-positive rates on the terminate decision. Test extensively against rapid successive calls with identical arguments (the most common runaway pattern), oscillating calls where the agent flips between two tools without progress, and legitimate long chains (e.g., paginated API calls) to calibrate the threshold. Track false-positive and false-negative rates in a dedicated eval set before raising the guard to production. Wire the guard's output into your observability stack so you can correlate termination events with user-reported failures and adjust the prompt's [LOOP_PATTERN_DEFINITIONS] over time.
Expected Output Contract
Fields, format, and validation rules for the termination decision JSON returned by the Runaway Tool Loop Detection and Termination Prompt. Use this contract to parse, validate, and act on the model's output in your agent harness.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
termination_decision | enum: terminate | warn | allow | Must be exactly one of the three allowed values. Reject any other string. | |
reason_code | string from allowed code list | Must match a code defined in [ALLOWED_REASON_CODES]. Reject unknown codes. | |
loop_signature | object | Must contain tool_name (string) and repeated_arguments (boolean). repeated_arguments must be true for termination decisions. | |
consecutive_identical_calls | integer >= 0 | Must be a non-negative integer. If termination_decision is terminate, this must be >= [TERMINATION_THRESHOLD]. | |
state_progression_detected | boolean | Must be true or false. If false and consecutive_identical_calls >= [TERMINATION_THRESHOLD], termination_decision must be terminate. | |
escalation_path | enum: human_review | circuit_break | log_only | null | Must be one of the allowed values or null. If termination_decision is terminate, must not be null. | |
recommended_action | string | Must be a non-empty string describing the concrete next step. If termination_decision is allow, must explain why the loop is considered safe. | |
evidence_summary | string | Must reference specific tool names, call counts, or timestamps from [LOOP_TRACE]. Reject summaries with no trace grounding. |
Common Failure Modes
Runaway tool loops are the fastest path to production cost overruns and throttled systems. These failure modes cover what breaks first and how to guard against it.
Non-Progressing State
What to watch: The agent makes repeated tool calls with identical or trivially varied arguments without advancing toward the goal. The output of each call does not meaningfully change the agent's next action. Guardrail: Track a state hash of the last N action-observation pairs. If the hash repeats more than the configured threshold, terminate with reason code NO_PROGRESS and escalate to a human operator.
Unbounded Retry on Transient Errors
What to watch: The agent encounters a 429, 503, or timeout and retries immediately without backoff, or retries indefinitely without a maximum attempt limit. This amplifies load on an already degraded service. Guardrail: Enforce a maximum retry count per tool call and require exponential backoff with jitter. After the retry budget is exhausted, terminate with reason code RETRY_EXHAUSTED and log the full error chain.
Runaway Parallel Tool Invocation
What to watch: The agent spawns an unbounded number of parallel tool calls in a single step, often due to an unconstrained loop generating sub-tasks. This can saturate API rate limits and inflate costs multiplicatively. Guardrail: Enforce a hard cap on concurrent tool calls per step. If the agent requests more than the cap, reject the step with reason code CONCURRENCY_LIMIT and require the agent to prioritize or batch its requests.
Goal Drift and Scope Creep
What to watch: The agent's tool calls gradually shift from the original objective to tangentially related exploration, often triggered by interesting but irrelevant tool outputs. The original task is never completed. Guardrail: Include the original task objective in every agent step context. Before each tool call, require the agent to self-assess relevance. If a configurable number of consecutive calls are scored below a relevance threshold, terminate with reason code GOAL_DRIFT.
Silent Argument Hallucination
What to watch: The agent calls a tool with arguments that pass schema validation but are semantically nonsensical—such as fabricated IDs, impossible date ranges, or parameters that don't correspond to real entities. The tool returns empty or error results, but the agent continues as if the call succeeded. Guardrail: Validate tool outputs for emptiness or error indicators. If a tool returns a null set or an explicit error, block the agent from using that result as evidence for further actions. After N consecutive empty results, terminate with reason code INVALID_ARGUMENTS.
Missing Termination Condition
What to watch: The prompt or agent loop lacks an explicit stop condition, so the agent continues making tool calls until it hits a platform-enforced timeout or token limit. This wastes resources and delays failure detection. Guardrail: Every agent prompt must include an explicit termination policy with a maximum step count, a maximum wall-clock time, and a task-completion signal. When any limit is reached, the harness must force termination with reason code STEP_LIMIT or TIME_LIMIT, regardless of the agent's self-assessment.
Evaluation Rubric
Use this rubric to test the Runaway Tool Loop Detection and Termination Prompt before production deployment. Each criterion targets a specific failure mode observed in agent tool loops.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Rapid Successive Calls Detection | Prompt correctly identifies when the same tool is called more than [MAX_CALLS_PER_WINDOW] times within [TIME_WINDOW_SECONDS] seconds and flags it as a loop. | Output does not flag the loop, or flags a call sequence that is within the defined rate limit. | Simulate a trace with [MAX_CALLS_PER_WINDOW]+1 identical tool calls within [TIME_WINDOW_SECONDS]. Assert that the termination decision is true and the reason code is RAPID_SUCCESSIVE_CALLS. |
Repeated Arguments Detection | Prompt correctly identifies when [REPETITION_THRESHOLD] consecutive calls use identical arguments and no state change is observed in tool outputs. | Output fails to detect argument repetition, or falsely flags calls where arguments differ or state changes are present in outputs. | Feed a trace where the last [REPETITION_THRESHOLD] calls have identical arguments and outputs show no state progression. Assert termination decision is true and reason code is REPEATED_ARGUMENTS. |
Non-Progressing State Detection | Prompt correctly identifies when tool outputs show no meaningful state change over [STAGNATION_WINDOW] calls, even if arguments vary. | Output does not flag stagnation, or flags a sequence where outputs show clear state progression. | Provide a trace with [STAGNATION_WINDOW] calls where outputs are semantically equivalent or empty. Assert termination decision is true and reason code is NON_PROGRESSING_STATE. |
Budget Exhaustion Termination | Prompt correctly terminates when cumulative tool call cost exceeds [BUDGET_CAP] and provides remaining budget in the output. | Output continues execution past the budget cap, or fails to report the remaining budget accurately. | Set [BUDGET_CAP] to a low value and feed a trace that exceeds it. Assert termination decision is true, reason code is BUDGET_EXHAUSTED, and remaining_budget is less than or equal to 0. |
Escalation Path Inclusion | When termination is triggered, the output includes a valid escalation path from [ESCALATION_PATHS] with a clear summary of the loop state. | Termination output is missing the escalation_path field, or the path is not one of the allowed values in [ESCALATION_PATHS]. | Trigger any termination condition. Assert that escalation_path is present and its value is a member of [ESCALATION_PATHS]. Assert that loop_summary is a non-empty string. |
False Positive Resistance | Prompt does not terminate a valid, progressing sequence of tool calls that stays within all defined limits. | Prompt flags a termination for a trace that is within rate limits, shows argument variation, and has progressing state. | Construct a trace with varied arguments, progressing outputs, and call counts below [MAX_CALLS_PER_WINDOW]. Assert termination decision is false. |
Schema Compliance | Output is valid JSON matching the [OUTPUT_SCHEMA] with all required fields present and correctly typed. | Output is not valid JSON, or is missing required fields such as termination_decision, reason_code, or escalation_path. | Parse the output with a JSON validator against [OUTPUT_SCHEMA]. Assert no parse errors and all required fields are present with correct types. |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Adapt This Prompt
How to adapt
Add structured logging, retry-aware loop detection, state-diff analysis, and a full termination decision schema. Include escalation_path, evidence, and recommended_action fields. Wire in tool-call hashing to detect semantically identical calls with different surface forms.
codeYou are a production tool-loop monitor. Analyze the execution trace and produce a termination decision. [EXECUTION_TRACE] [STATE_DIFF_SUMMARY] [BUDGET_REMAINING] Return JSON matching [OUTPUT_SCHEMA]: { "terminate": boolean, "reason_code": "LOOP_DETECTED"|"PROGRESS_STALLED"|"BUDGET_EXCEEDED"|"RATE_LIMITED"|"NORMAL", "confidence": 0.0-1.0, "evidence": ["list of specific observations from trace"], "escalation_path": "AUTO_TERMINATE"|"HUMAN_REVIEW"|"CONTINUE_WITH_WARNING", "recommended_action": "string", "audit_record": { "loop_pattern": "string", "repeated_call_count": number, "state_progress_detected": boolean, "budget_consumed_percent": number } }
Watch for
- Silent format drift in nested audit_record fields
- Missed loops when tool names differ but arguments are identical
- Budget exhaustion not triggering before the next call is made
- Confidence scores that don't correlate with actual loop severity

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us