sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] Detect and stop repeated tool-call loops

Open yonib05 opened this issue 1 month ago • 1 comments

Human Overview

No response

SDK Language

Both / Not language-specific

Problem Statement

An agent can get stuck repeating the same short sequence of tool calls, each returning the same result. Every repetition resends the full conversation, so in long-running or autonomous agents a loop can run for hours and consume a large token volume before anyone notices. Neither SDK nor the harness detects this today. Invocation limits cap the damage, but only after the budget is spent, and they can't tell a loop from long, legitimate work.

Proposed Solution

A loop guard, available as a vended plugin in both SDKs and on by default in the harness:

  • Fingerprint each tool call by tool name, normalized input, and a digest of the result, over a sliding window.
  • Detect a cycle of length 1 to N that repeats K times with identical fingerprints.
  • On first detection, append a short note to the tool result telling the model it is repeating the same calls with the same results.
  • If the cycle continues, stop the invocation with a distinct stop reason (for example loopDetected), leaving messages reinvokable the same way limits do.
  • Log at warn and emit a telemetry event when it fires.
  • Configurable window, maximum cycle length, repetitions, and action; same names and behavior in both SDKs.

Use Case

  • Unattended coding or operations agents that re-run the same commands without making progress.
  • A cost ceiling that reacts to the loop itself rather than to a fixed budget.
  • Surfacing model or prompt regressions that show up as repetitive tool use.

Alternatives Solutions

  • Invocation limits (turns, totalTokens): bound cost but fire only after the budget is consumed.
  • Per-application hooks on AfterToolCallEvent: possible today, but every user reimplements the same logic.
  • Exact-duplicate detection of single calls: misses multi-step cycles.

Additional Context

Legitimate polling can look like a loop. Calls whose results change should not count, and tools should be able to opt out.

yonib05 avatar Oct 28 '26 17:10 yonib05

Hi, I'd like to implement the Python side of this as a vended plugin, if you're open to an outside contributor.

Where I'd put it: strands-py/src/strands/vended_plugins/loop_guard/ next to goal/ and context_offloader/, built only on the existing hook surface (AfterToolCallEvent, hooks/events.py:261, which already carries tool_use, result, retry). No change to the event loop or Limits, so it stays independent of #5288 (invoke limits) and #4976 (middleware).

Sketch:

agent = Agent(plugins=[LoopGuard(window=20, max_cycle=4, repetitions=3, action="stop")])

Fingerprint = tool name + normalized input + digest of result, over a sliding window, state kept per invocation (the point raised in #4988: not on the plugin object, since subagents share hooks). First detection appends a short note to the tool result; continued repetition stops with a new stop reason. Calls whose results change are never counted, and tools can opt out, which covers the polling and pytest -q cases from the #4988 thread.

Questions before I start: is a new StopReason (e.g. loop_detected, types/event_loop.py:50) acceptable, or would you rather the guard cancel via the existing mechanism; and do you want create_harness() to enable it by default in the same PR or a follow-up.

Plan: PR 1 core detector + unit tests (pure functions, no model); PR 2 plugin wiring, stop reason, telemetry event; PR 3 harness default (#4988). About 1000-1500 lines with tests, TypeScript parity left to a separate PR or contributor. Tests: fake-model repro like the one in #4988, cycle lengths 1..N, changing-result negative cases, per-invocation isolation, and failure paths. I would run the project's own hatch checks.

I use AI tools to assist; I review and can explain every line. Could you assign me if this direction is fine?

Cookie0103 avatar Nov 07 '26 13:11 Cookie0103