Telemetry
AI and LLM SQL recipe

Detect Repeating AI Agent Tool Loops

Find agent runs that repeatedly call a small set of tools without reaching a successful outcome.

Intermediateagent_tool_callsReviewed 2026-07-27Tested with Apache DataFusion 45.2.0

Question answered

Which agent runs appear stuck in a repetitive tool loop?

A failed agent does not always raise an exception. Repeated calls to the same tools can consume time and tokens while the run remains technically active.

Event contract

Fields the query expects

FieldTypeWhy it exists
timestamp_utcTimestampTool-call completion time.
run_idUtf8Stable identifier for one agent run.
agent_nameUtf8Agent workflow name.
tool_nameUtf8Called tool name.
statusUtf8Tool-call outcome.
estimated_cost_usdFloat64Cost attributed to the step when available.
DataFusion SQL

Copy the query

sql
SELECT
  run_id,
  agent_name,
  COUNT(*) AS tool_calls,
  COUNT(DISTINCT tool_name) AS distinct_tools,
  SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) AS failed_calls,
  SUM(estimated_cost_usd) AS tool_loop_cost_usd,
  MIN(timestamp_utc) AS first_call_at,
  MAX(timestamp_utc) AS last_call_at
FROM agent_tool_calls
WHERE timestamp_utc >= now() - INTERVAL '24 hours'
GROUP BY run_id, agent_name
HAVING COUNT(*) >= 8
  AND COUNT(DISTINCT tool_name) <= 2
ORDER BY tool_calls DESC, tool_loop_cost_usd DESC
LIMIT 100;

This read-only query is planned and executed against an empty typed table with Apache DataFusion 45.2.0. The deterministic sample output is synthetic and reviewed separately; validate field types, thresholds, and business definitions against your own data. Read the testing methodology.

Query result

Repeated tool calls by agent run

The billing reconciler made 17 calls across only two tools and failed almost half of them.

run_idagent_nametool_callsdistinct_toolsfailed_callstool_loop_cost_usdfirst_call_atlast_call_at
run_9021billing_reconciler17280.8412:02:1112:06:48
run_9154support_agent11100.3112:44:1012:45:32

Synthetic example output. Run the query against your own event schema and thresholds before using it for operational decisions.

How the SQL works

  1. 1A loop candidate combines a high call count with very low tool diversity.
  2. 2Failed calls distinguish explicit retry loops from repeated successful calls that never advance state.
  3. 3Cost and elapsed timestamps show the financial and latency impact of the loop.

Edge cases to decide

  • Some batch agents legitimately call one tool many times; segment by agent workflow and expected item count.
  • A loop threshold should include a maximum run time or state transition in production.
  • Do not log raw tool arguments or returned customer content to diagnose loops.

Recommended dashboard

  • Bars: tool_calls by run_id
  • Stat: suspected loops and attributed cost
  • Table: tool sequence, status, and categorized error for selected runs

Alert guidance

Alert or terminate a run when repeated calls exceed the workflow-specific safety threshold without a state transition.

Read alert setup

Put the recipe to work

Related instrumentation and guides

Continue the analysis

Run it on real events

Create a table, adapt the fields, and save the result

Start free, send structured events, and use the query result as a chart, shared dashboard widget, or alert input.

Get an API key