Event contract
Fields the query expects
| Field | Type | Why it exists |
|---|---|---|
| timestamp_utc | Timestamp | Tool-call completion time. |
| run_id | Utf8 | Stable identifier for one agent run. |
| agent_name | Utf8 | Agent workflow name. |
| tool_name | Utf8 | Called tool name. |
| status | Utf8 | Tool-call outcome. |
| estimated_cost_usd | Float64 | Cost attributed to the step when available. |
Copy the query
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_id | agent_name | tool_calls | distinct_tools | failed_calls | tool_loop_cost_usd | first_call_at | last_call_at |
|---|---|---|---|---|---|---|---|
| run_9021 | billing_reconciler | 17 | 2 | 8 | 0.84 | 12:02:11 | 12:06:48 |
| run_9154 | support_agent | 11 | 1 | 0 | 0.31 | 12:44:10 | 12: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
- 1A loop candidate combines a high call count with very low tool diversity.
- 2Failed calls distinguish explicit retry loops from repeated successful calls that never advance state.
- 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 setupPut the recipe to work
Related instrumentation and guides
Continue the analysis
Calculate LLM Cost by Feature and Model
Attribute model spend, tokens, request volume, and cost per request to product features.
Open recipeMeasure Accepted AI Outputs per Dollar
Connect model spend to accepted, saved, or otherwise useful product outcomes.
Open recipeQuery Nested AI Tool-Call Events
Filter dotted nested fields and rank failing tools without flattening the original event payload.
Open recipeRun 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.