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

[FEATURE] Vended plugin that detects a repeated tool batch and nudges, then ends the turn

Open serenearyal opened this issue 2 months ago • 2 comments

SDK Language

Python

Problem Statement

An agent sometimes asks for exactly the same batch of tool calls it already made: same tool names, same inputs. The results are already in the conversation, so the repeat spends a full cycle for nothing, and a model that has started circling often repeats again until the turn or token limit fires. Limits(turns=...) caps the damage but cannot tell a productive cycle from a wasted one. There is nothing in the SDK today that notices a repeated batch.

Proposed Solution

A small vended plugin, built entirely on public hooks, that ships next to the existing ones in strands-py/src/strands/vended_plugins/:

  • BeforeToolsEvent: compute a signature of the batch from the assistant message (sorted JSON of {name, input} per toolUse block; order insensitive, tool use ids ignored) and count a repeat in invocation_state. A per-tool interrupt re-fires this event with the same assistant message, so the same message object is counted once.
  • AfterToolsEvent: on the first repeat, append a short text block to event.message["content"], the user-role tool-result message, so the model reads the note directly under the results it already has. On the second repeat, set event.end_turn to a final text, so the run ends with stop_reason == "end_turn" instead of burning the remaining budget.
  • Two thresholds, nudge_after=1 and stop_after=2 (None for nudge only), and both texts configurable with a {tools} placeholder.
  • Non-idempotent tools (live readings, clocks) are excluded by name through ignore; the production loop does the same for its live probe tool.
from strands import Agent
from strands.vended_plugins.loop_guard import LoopGuard

agent = Agent(tools=[...], plugins=[LoopGuard()])

Nothing inside the event loop changes. State lives in invocation_state, so it resets per invocation and survives interrupt and resume without plugin-side memory. The guard never edits the assistant message, only the tool-result message and end_turn, so it is provider neutral.

A reference implementation is on a branch of my fork, already in the vended plugin layout: plugin and tests. I open the PR once you say this fits. The tests drive a real strands.Agent through the event loop with a scripted model and a real @tool, and pin: the note is the text block of the second tool-result turn; the second repeat ends with end_turn and the model is called three times, not four; a different batch is not a repeat; the same batch with calls shuffled is a repeat; stop_after=None nudges and lets the model finish; a batch split by an interrupt is counted once. Seven tests pass against main @ 66101c7d. I am happy to open the PR if this fits as a vended plugin, or to keep it as a community plugin if you would rather not vend it.

Use Case

ScanMyCar's AI mechanic runs a tool loop over an owner's logged drives (sensor series, stored codes, freeze frames) and has carried this guard in production since 2026-09-05. In one week, 11 of 90 jobs repeated a tool batch at least once, and 3 chat runs were stopped by the guard. Two real traces:

  • A chat on GPT-5.6 through the OpenAI Responses API: 7 rounds, two repeats (get_sensor_series re-requested identically at call 2, then a second series at call 5), stopped by the guard. The first repeat re-issued a call whose result had been error: unknown pidId, so the model was retrying a failing call verbatim; the note is what broke that.
  • A health check: 8 rounds, two repeats (get_session_stats, then get_sensor_series), nudged both times, and the structured report still landed.

The same need shows up for any agent with read-only data tools, where a repeat is always a wasted cycle.

Alternatives Solutions

  • Limits(turns=...): stops runaway loops but only at the cap, and cannot distinguish a repeat from useful work.
  • A Guide intervention on before_model_call: injects a separate user message rather than a note inside the tool-result turn, and the docstring says it bypasses session management.
  • Keep it as a community plugin outside the SDK. Works today; the reason to vend it is that the pattern is generic and the hooks it needs already exist.

Additional Context

  • Related but separate: #453 (per-cycle tool_choice). Our production loop pins tool_choice to a final submit_report tool after a repeat instead of ending the turn. end_turn covers the guard on its own; a hook-settable "force final cycle" would be a follow-up, not a requirement here.
  • Open question for maintainers: should end_turn remain the stop action, or should the plugin expose a callback so users can choose between ending the turn and forcing a final cycle once #453 exists?

serenearyal avatar Sep 10 '26 01:09 serenearyal

Hi @serenearyal, nice idea!

We have to very selective in the vended plugins that we ship in order to keep the SDK maintainable & focused; to that end making this a community plugin would be the best way forward. You can find instructions here for publishing and then getting it featured on the website.

Thanks!

zastrowm avatar Sep 28 '26 14:09 zastrowm

Thanks @serenearyal for the idea and for offering to publish it. A guard against repeated tool batches is a useful pattern.

Vended plugins in the SDK are kept selective, so we're closing this in favor of a community plugin. See Build a custom plugin for registering hooks, Publishing extensions for packaging, and Add Your Integration to get it listed.

If something is still missing, or the plugin hits a gap in the SDK, please open a new issue.

yonib05 avatar Oct 25 '26 23:10 yonib05