Skip to main content

Loop Detection (loop_detect)

Contents​

Metric Description​

Loop detection is a trace-level metric: it measures whether an agent got stuck repeating the same tool calls on a full OpenInference-format trace. It scores repeated calls, not whether the run achieved its goal (Task Completion), whether the agent stopped at the right moment (Termination Correctness), or whether the path was cheap (Trajectory Efficiency).

The score runs from 0 (a loop is still the run's final decisions) to 100 (nothing was charged: no repeat, or every repeat was a repetition the agent had no other choice but to make).

A repeat is a loop when the agent could have done something else at that point — stopped and informed the user, asked for missing input, changed the arguments, or used another tool — and chose to repeat itself anyway. A repeat is a repetition when the agent had no other reasonable choice, such as polling a job that is still pending.

How a repeat is found

  1. A call is a tool name plus its arguments. The same call is counted once per model turn, including when that turn fired it twice in parallel. A call is repeated when it appears again anywhere in the trace, including inside another sub-agent.
  2. Repeated calls that the same turns issued together form one group. A, B, and C fired together three times is one group of three, not three loops. A sibling that changes every time is left out of the group. If A and B are fired together twice and then A is fired alone, that is a group AB (twice) and a group A (three times).
  3. Calls to one tool whose free-text arguments were reworded form a similar cluster. Free text is a string of three words or more, such as a search query or an instruction. The calls must have the same argument fields, every other value must be identical (ids, numbers, short strings), and every free-text field that changed must score at least 93 out of 100 on semantic similarity. Each field is compared on its own. While grouping, a cluster's variants count as the same call, so a reworded call groups with its partners exactly like an identical one: A and B fired together twice, then a reworded A with B, is one group of three.
  4. The judge first reads every cluster's actual arguments and sorts them into variants that ask for the same thing. Variants that ask for different things despite the close wording (another date, place, entity, quantity, or question) are split apart, and the groups are rebuilt without that merge: in the example above, a reworded A that asks for something else leaves a group AB (twice) and a group B (three times). A rebuilt group takes the verdict of the judged groups it shares a call with; a loop wins over a repetition. A rebuilt group with no judged parent stays uncharged. A rebuilt group with no more occurrences than max_allowed_repetitions is dropped. Reworded tries that ask for the same thing are judged like identical calls. Both steps happen in the same judge call. Groups go out in batches; a cluster that reaches more than one batch stays merged only where every batch put the variants in the same class. A batch that failed, or skipped a cluster, keeps those variants apart.
  5. For each repeat after the first, the judge sees what that turn's input added since the previous try, and what the tools returned, and decides whether the agent had another option. A pending status with a retry hint, a user instruction to run it again, or a result that changed in a way that needs one more check makes the repeat a repetition. The same error coming back again is a loop: the agent could have reported it instead. One call the agent had an alternative to makes the whole group a loop.

How it is scored

  • Nothing charged scores 100: no repeated group, every group a repetition, or repeated groups the judge left uncharged when none of them ends the run.
  • A loop the agent left lowers the score by the share of duration and tokens the repeats past max_allowed_repetitions used. Calls inside that allowance are not waste; with the default of 1, that is the first try. A call that sits in two charged groups is charged once. Equal weight when both measurements were recorded; otherwise the one that was recorded. When neither was recorded, or the charged repeats measured no time and no tokens, the score is the share of calls that were not repeats. A charged loop scores below 100.
  • The score is 0 when a loop is the run's ending: more of the final decisions are still that loop than max_allowed_repetitions, text-only turns included. With the default of 1, the last two turns are still the loop. A turn has left the loop when it called something outside the group, issued only part of the group, or answered without a tool call. Ending on a single loop call after a turn that did other work leaves the score above 0. The waste is still reported.
  • Repetitions do not change the score by default. With penalize_repetitions set to true, their repeats past the allowance lower the score the same way as a loop's, but a repetition never sets the score to 0, even when it ends the run.
  • A group the judge skips, or whose batch fails, is left uncharged. When that group is the run's ending, there is no score.

A call to one of allowed_repeat_tools joins no group, so it is never judged or charged, even beside a looping tool and even with penalize_repetitions. A later turn that calls only an allowed tool, or an allowed tool beside the looping one, has left the loop.

Important

This metric matches calls by name and normalized arguments, and also joins free-text arguments that were only reworded (similarity of at least 93, then confirmed by the judge). It does not join a changed id, number, or short string, and it does not score path efficiency or the stopping point.

How to interpret the score​

  • 100: nothing was charged. That covers no repeated call (reworded calls the judge split apart are not repeats), repetitions while penalize_repetitions is off, and repeats the judge did not answer when the run did not end on one of them.
  • Less than 100: the agent looped (or, with penalize_repetitions, made repetitions), then moved on. The gap from 100 is the share of time and tokens the repeats past max_allowed_repetitions used.
  • 0: a loop is still the run's final decisions, for more turns than max_allowed_repetitions.
  • No score (null): the trace was not a non-empty list of span objects, a metric argument was the wrong type, the judge returned no verdict for any repeated group, or a group the judge did not answer is the run's ending. Those judge cases return the generic evaluation error.

API usage​

Prerequisites

After the environment variables are configured, the next step is to create a JSON payload. loop_detect scores a whole trace. An inline trace is scored with POST /evaluate: send trace and omit span_id. A stored trace is scored with POST /runs/trace.

Shortname: loop_detect

Default threshold: 100

Inputs (each object in data)

  • trace (list[object] required): An OpenInference-format trace — an ordered list of span dictionaries that share a trace_id and are linked by parent_id. The metric reads tool name, arguments, output, which model turn issued the call, and that turn's input.

metric_args

  • max_allowed_repetitions (number, optional, default 1): How many identical calls are still ignored. They are not checked, and they are not charged once a later repeat crosses the limit. 1 means the second identical call is checked and, when it is a loop, charged; the first stays free. Anything other than an integer of at least 1 fails evaluation, including booleans, decimals, and strings.
  • allowed_repeat_tools (string[], optional): Tools whose repetition is expected, such as a status poll. A call to one of these tools joins no group. When it is repeated beside a tool that is not listed, only that other tool is judged and charged. A later turn that calls only an allowed tool, or an allowed tool beside the looping one, has left the loop. A value that is not a list of strings fails evaluation.
  • penalize_repetitions (boolean, optional, default false): When true, repetitions (repeats the agent had no other choice but to make) also lower the score, by the share of duration and tokens of the repeats past max_allowed_repetitions. Unlike a loop, a repetition that ends the run does not drop the score to 0. Loops lower the score either way. Any value other than true or false fails evaluation.

Evaluation metadata​

On a scored evaluation, eval_metadata contains loops and repetitions. Both lists are empty when nothing repeated. Invalid input and a judge failure omit eval_metadata.

  • loops: confirmed loops. Each entry has match, tools, call_keys, times_called (how many times the group occurred), escaped, ended_run, steps, wasted_steps, wasted_duration_ratio, wasted_tokens_ratio, and reason.
  • repetitions: groups the agent had no other choice but to repeat. Each entry has match, tools, call_keys, times_called, steps, wasted_steps, wasted_duration_ratio, wasted_tokens_ratio, and reason. Their waste is always reported, and counts toward the score only when penalize_repetitions is true.

match is exact when every try used identical arguments, and similar when at least one try reworded the free text; a similar entry lists every variant in call_keys. Reworded calls the judge split apart are listed only under the groups they still belong to.

wasted_duration_ratio and wasted_tokens_ratio are that group's waste as a share of the run, from 0 to 1. Either is null when that measurement was not recorded. wasted_steps lists the steps those ratios were measured on, decisions included.

escaped is true when, from the group's last occurrence on, a turn issued only part of the group, made a call outside it, or answered without a tool call. ended_run is true when more of the run's final decisions are still this group than max_allowed_repetitions, text-only turns included. A call shared by two charged groups appears in both waste lists; the score counts it once.

A reconstructed turn in steps or wasted_steps is named by its step_id. A logged span keeps its integer index.