All insights

Inference economics

How to detect agent loops from request traces before cost or latency spikes

Operators can detect a likely agent loop by evaluating active request traces for recurring model-and-tool sequences, repeated arguments or outputs, increasing call depth or fan-out, limited state change, and continued activity near time, token, or cost budgets. No single signal proves that a loop exists. Detection works best when multiple signals are evaluated in a rolling window, compared with workflow-specific baselines, and paired with enforceable limits or cancellation paths.

Operators can detect a likely agent loop by evaluating active request traces for recurring model-and-tool sequences, repeated arguments or outputs, increasing call depth or fan-out, limited state change, and continued activity near time, token, or cost budgets. No single signal proves that a loop exists. Detection works best when multiple signals are evaluated in a rolling window, compared with workflow-specific baselines, and paired with enforceable limits or cancellation paths.

What qualifies as an agent loop in operational terms?

An agent loop is a recurring sequence of model and tool activity that consumes time, tokens, or cost without making meaningful progress toward the task. The sequence might involve one tool called repeatedly, several operations cycling in the same order, or an expanding tree of model and tool calls that does not move the run closer to completion.

This is an operational definition rather than a universal protocol rule. Each team must define meaningful progress for its agents, tools, and task types.

Repetition is a signal, not proof of a loop

Repeated activity can be legitimate. An agent may need to:

  • Retry an operation after a transient failure.
  • Poll an asynchronous job until its status changes.
  • Request successive pages from a paginated data source.
  • Refine a search or plan over several iterations.
  • Process a collection one item at a time.
  • Revisit a tool after new context becomes available.

The question is therefore not simply, “Did this tool run again?” Operators should ask whether the repeated activity was expected, bounded, and accompanied by measurable progress.

A retry with a changed status, a pagination cursor that advances, or a planning step that produces a new subtask is different from a tool call that repeatedly receives equivalent inputs and returns equivalent outputs.

Define lack of progress in terms of state, output, and budget consumption

Progress should be observable wherever possible. Depending on the workflow, useful indicators can include:

  • A task, subtask, or checklist item moving to a completed state.
  • A pagination cursor, record offset, or job status changing.
  • New information appearing in the agent’s working state.
  • A tool returning materially different results.
  • The plan becoming shorter or converging on a final action.
  • An external system accepting a requested change.

A suspected loop combines low progress with ongoing resource consumption. For example, the trace may show the same call pattern continuing while elapsed time, model usage, or estimated cost rises. Usage and cost can only be assessed when the relevant systems capture those fields.

Definitions should be tuned by agent, workflow, tool, and task type. A document-processing agent and an interactive support assistant are unlikely to need the same limits or baselines.

Build a trace that connects the agent run, model calls, tools, and retries

The minimum useful trace hierarchy connects a root request or agent run to its model calls, tool calls, retries, and downstream operations. Without those relationships, operators may see high aggregate usage but struggle to determine which decision caused the recursive behavior.

Represent the root request and its parent-child span hierarchy

A conceptual trace might look like this:

Agent run / root request
├── Model call: choose next action
├── Tool call: search records
│   └── Downstream database or service request
├── Model call: evaluate result
├── Tool call: search records
│   └── Retry
│       └── Downstream database or service request
└── Model call: evaluate result

Each operation should be connected through trace and span identifiers and parent-child relationships. This allows an operator or detector to reconstruct whether a tool invocation followed a new decision, retried a failed dependency, or repeated an earlier branch.

The hierarchy should preserve distinctions among:

  • The complete agent run.
  • Individual model calls.
  • Tool selection and tool execution.
  • Application-level retries.
  • Transport or dependency retries.
  • Downstream database, API, queue, or service operations.

That distinction matters because a tool may appear to execute repeatedly even when the recursion originated in an agent decision, a retry policy, or a downstream failure.

Capture timing, status, usage, operation names, and request attributes

Useful conceptual trace fields include:

  • Trace ID, span ID, and parent span ID.
  • Agent, workflow, task type, and operation name.
  • Tool name and a normalized tool-call signature.
  • Start time, end time, and duration.
  • Success, error, cancellation, or timeout status.
  • Retry number and the reason for retrying.
  • Model usage, token data, or cost estimate when available.
  • Recursion depth, step number, and branch information.
  • Remaining execution budget when the application tracks it.

Request attributes should describe the workflow well enough to establish meaningful baselines without indiscriminately copying prompt, argument, or output content into telemetry.

Normalize tool-call arguments and outputs without exposing sensitive content

Detecting duplicate calls does not always require storing complete arguments or outputs. A safer design can normalize selected fields and compare fingerprints instead.

For example, a team might remove volatile timestamps, sort unordered fields, retain only operationally relevant attributes, and calculate a hash of the normalized representation. Output comparison can use status, result category, record count, cursor position, or another compact state summary.

The appropriate design depends on the sensitivity of prompts, tool parameters, and returned data. Apply minimization, redaction, hashing, access restrictions, and retention controls before making trace content broadly available. Highly sensitive values may need to remain outside the observability system entirely.

Detect candidate loops while the request is still active

Post-run analysis helps with tuning, but it cannot contain a run that is currently expanding. Evaluate each active trace in a rolling window or whenever a relevant span completes.

Useful candidate signals include:

SignalTrace evidencePossible interpretationCommon false positivePossible response
Repeated tool-call signatureEquivalent tool name and normalized arguments recurThe agent may be retrying the same ineffective actionExpected polling or idempotent retryCheck output change and retry status
Recurring span sequenceThe same model-tool pattern repeats in orderThe workflow may be cycling through the same decisionsDeliberate iterative planningCompare state and plan changes
Increasing span depthEach cycle creates a deeper descendant branchRecursive delegation may be expandingValid nested task decompositionApply a workflow-specific depth limit
High fan-outOne decision creates many parallel tool or model callsA branch may be multiplying unexpectedlyIntended batch processingCompare fan-out with task size and baseline
Repeated arguments or outputsInput or result fingerprints remain stableThe run may not be learning from prior attemptsStable health checks or polling responsesRequire backoff or suppress duplicates
Retry burstMany retries occur over a short part of the traceA dependency or retry policy may be amplifying workBrief recoverable outageUse capped retries and circuit breaking
Low state changeTask state, cursor, plan, or result summary does not advanceContinued calls are producing little progressWork whose progress is not externally visibleImprove progress instrumentation
Budget proximityActivity continues as time, usage, or cost approaches a limitThe run may create unacceptable exposureLegitimate long-running taskCancel, degrade, or escalate based on policy

A detector can update per-trace features as spans arrive:

when an operation completes:
  update sequence, depth, fan-out, retry, and usage features
  compare input, output, and workflow-state fingerprints
  evaluate the features against this workflow's baseline
  if several risk signals coincide:
    warn, restrict, cancel, or escalate according to policy

The evaluation interval, thresholds, and response should be validated against actual workloads. A fixed call-count rule applied to every agent will create blind spots as well as false alarms.

Combine multiple signals and baseline normal behavior

A practical detector should treat loop behavior as a multi-signal condition. For example, repeated tool calls become more concerning when they also have equivalent arguments, equivalent outputs, no state transition, growing depth, and shrinking budget headroom.

Teams can implement this as a ruleset or an anomaly score. The precise method matters less than preserving explainability: operators should be able to see which signals triggered and why the run differed from its expected behavior.

Baseline normal behavior by dimensions such as:

  • Agent and workflow version.
  • Tool and operation type.
  • Task category and expected task size.
  • Interactive, asynchronous, or batch execution.
  • Successful, failed, and manually reviewed outcomes.

Baselines should account for legitimate variation. Pagination may normally produce many similar calls, but its cursor should advance. Polling may return the same state several times, but it should follow a backoff policy and remain within a deadline. Batch work may create high fan-out, but the fan-out should correspond to the batch size.

Review baselines after model, prompt, tool, or orchestration changes. Behavior that was normal for one agent version may not be normal for another.

Pair trace detection with enforceable containment controls

Tracing provides evidence of a suspected loop; it does not stop one by itself. The execution environment needs a path that can enforce a response.

Common controls include:

  • Maximum steps: stop or escalate after the run consumes its configured action allowance.
  • Recursion-depth limits: prevent unbounded delegation or nested agent execution.
  • Wall-clock deadlines: cancel work that exceeds the workflow’s acceptable duration.
  • Token or cost budgets: restrict further model activity when captured usage approaches a configured limit.
  • Per-tool call limits: cap operations that are expensive, slow, destructive, or dependency-heavy.
  • Duplicate-call suppression: reject or pause equivalent calls when the workflow has not changed.
  • Circuit breakers: interrupt calls to a failing dependency or unhealthy operation.
  • Cancellation propagation: ensure that stopping the root run also stops pending model, tool, and downstream work where possible.
  • Human escalation: preserve state and route the run for review when automatic termination would be inappropriate.

Response policies can be progressive. An early warning may reduce concurrency, require backoff, or block one duplicate operation. A stronger condition may cancel the run. High-impact workflows may require human confirmation rather than automatic termination.

Controls should also define what the user experiences: an error, a partial answer, a queued review, or a safe request to try again. Operational containment without a product-level fallback can leave users with confusing failures.

Preserve enough context to investigate and tune the policy

An alert or incident record should explain both the observed behavior and the action taken. Useful contents include:

  • The trace and affected agent run.
  • The repeated operation or span sequence.
  • The signals and rule that triggered.
  • Elapsed time and current execution state.
  • Usage or estimated cost when available.
  • Remaining budget at the point of detection.
  • The control applied, such as warning, suppression, cancellation, or escalation.
  • Redacted state summaries needed to assess whether progress occurred.

Preserving the trigger explanation helps teams distinguish a genuine non-progressing loop from legitimate iteration. Reviewed incidents can then become tuning data: false positives refine exclusions and baselines, while missed cases reveal which progress or budget signals are absent.

A practical implementation workflow

A concise implementation sequence is:

  1. Instrument the run. Connect the root request to model calls, tool calls, retries, and downstream work.
  2. Normalize telemetry. Standardize operation names, statuses, retry semantics, and privacy-preserving call fingerprints.
  3. Derive per-trace features. Track sequences, repeated signatures, depth, fan-out, state change, elapsed time, and available usage.
  4. Evaluate active traces. Apply rules or anomaly signals in a rolling window rather than waiting for completion.
  5. Enforce budgets. Connect detection to step, depth, time, usage, tool, and cancellation controls.
  6. Alert with context. Preserve the trace, trigger, repeated sequence, estimated exposure, and response.
  7. Review and tune. Compare incidents with normal behavior by workflow and update thresholds after system changes.

Start with a small number of explainable signals for the workflows with the highest potential exposure. Expand only after operators can review alerts and understand why they fired.

Questions to ask observability and AI infrastructure providers

When evaluating a managed API, observability platform, self-deployed serving stack, or private inference control plane, buyers should ask:

  • Can one trace connect the agent run, model calls, tool calls, retries, and downstream operations?
  • Is context propagated across asynchronous jobs, queues, services, and tool boundaries?
  • How quickly can active trace data be evaluated?
  • Which usage fields are available, and at what level of granularity?
  • Can teams configure limits by agent, workflow, model, tool, tenant, or task type?
  • Can a policy warn, throttle, reject, cancel, or escalate a run?
  • Does cancellation propagate to pending downstream work?
  • How are arguments, outputs, and sensitive attributes minimized or redacted?
  • What retention and access controls apply to telemetry?
  • Can telemetry and policy operations remain under enterprise control in a private deployment?
  • How are policy changes tested, reviewed, and audited operationally?
  • Can incident records show the triggering signals and action rather than only a generic alert?

Buyers should verify the complete path from visibility to enforcement. A detailed trace is valuable, but limiting exposure requires the orchestration and serving environment to act on the resulting policy decision.

Connecting trace-driven policies to Token Forge Cloud serving-layer control

Token Forge Cloud Private LLM Inference focuses on private deployment and serving-layer optimization for enterprise AI workloads. Token Forge Cloud’s serving-layer capabilities include model routing, semantic caching, batching, quantization, and GPU scheduling. Agentic workflows should be treated as a distinct serving-policy problem because their recursive and tool-driven execution patterns differ from latency-sensitive chat or batch enrichment.

A trace-driven loop policy can sit alongside serving-layer controls: the agent and observability stack derive risk signals, while the execution architecture applies the organization’s chosen budgets, routing decisions, or cancellation path. Loop detection is not automatic, and the exact integration and enforcement design depends on the customer’s agent framework, telemetry system, and deployment architecture.

For teams validating model demand before private serving, Token Forge Cloud Managed Model APIs offer API-first model access and usage data. Once workloads become more predictable, teams can evaluate whether private deployment and greater serving-layer control fit their operational and economic requirements.

Contact Token Forge Cloud to discuss API access, private deployment, and LLM inference cost control.

Contact us