Sign in Get 3 free credits

Try a job or a niche: tiktok captions, study notes, excel formulas. Press Esc to close.

Stop runaway agent loops

Adds hard stop rules in code to your AI agent loops, so they finish or hand off to a human instead of spinning.

What we improved

Fixed the counter code. The original paired a LangGraph operator.add reducer with nodes that returned the running total, which makes the counter jump 1, 3, 7, and its exit function returned a value ("end") that its own type did not allow. T

Improved by YourAISkill from the original by agentsope, under its MIT licence. The download includes the author's licence and a CHANGES.md listing every change. Both Claude reviews read the improved version.

Claude's review

8/10 Clear bounded-loop contract with progress witness, stagnation check, tested Python and LangGraph code and cross-framework mapping.

We asked Claude the question you would ask: would I be glad I installed this? Two separate reviews, the second blind to the first, must both rate a skill 7 or higher before we sell it.

In detail

Universal discipline for any LM-driven loop — agent retries, plan-act-observe, multi-agent handoffs, optimiser passes, test-fix cycles. Encodes the one rule every framework documents quietly and every team relearns expensively: the LM in the loop is NEVER a reliable terminator. Termination must be provided by an explicit counter + exit predicate + stagnation signal + escalation path that live OUTSIDE the LM's control. This is a tool- level, framework-agnostic skill. It maps onto LangGraph (recursion_limit + state counter + interrupt), CrewAI (max_iter + max_rpm + human_input), Claude / OpenAI SDKs (max_iterations + tool_use_budget), DSPy (declared evaluation budget), Aider (REPL + explicit retry cap), and AutoGen (max_consecutive_auto_reply). Search keywords: infinite loop, recursion limit, recursion_limit, GraphRecursionError, max iterations, max_iter, agent stuck, agent won't stop, runaway agent, ReAct loop not terminating, agent repeating itself.

How you use it

  1. Add it to your pack, then download your pack (1 credit).
  2. In Claude, open Customize > Skills and upload the zip. Code execution needs to be on under Settings > Capabilities.
  3. Ask Claude for the job in your own words. It uses the skill when your request matches what it does. Step by step guide

Safety check

Nothing risky found: no hidden instructions, no download and run, no reading of saved passwords or keys.

An automated scan lowers risk but cannot promise zero risk. How we check skills

Preview

The first lines of SKILL.md
---
name: agentsop-bounded-loop
version: 0.2.0
description: "Make any LM-driven loop stop on purpose: agent tool-call retries, plan-act-observe, draft-critique-revise, test-fix cycles, optimiser passes and multi-agent handoffs. The model is never the terminator: termination comes from code it cannot override, namely a counter in shared state, a success predicate in plain code, a stagnation check on a progress witness, token and time budgets, and an escalation branch that hands the stuck state to a human. Tested plain Python and LangGraph code, a regression test pattern, and the matching settings for LangChain, CrewAI, OpenAI Agents SDK, Claude Agent SDK, AutoGen and DSPy. Use when an agent loops, repeats itself, will not stop, hits GraphRecursionError, recursion_limit, max_iter, max_turns or MaxTurnsExceeded, or when someone proposes raising the limit to fix it."
---
<!-- Modified by YourAISkill (youraiskill.com) on 2 December 2026 from the original by agentsope. See CHANGES.md. -->

# Bounded loop

## 1. When to activate

Activate when **any** of these is true:

- The workflow has a **cycle**: tool call → reflect → retry, plan → act → observe → re-plan, draft → critique → revise, test → fix → re-test.
- A framework's loop cap fired: `GraphRecursionError` / `GRAPH_RECURSION_LIMIT` (LangGraph), `max_iterations` reached (LangChain `AgentExecutor`), CrewAI's max iterations, `MaxTurnsExceeded` (OpenAI Agents SDK), a max-turns result from the Claude Agent SDK.
- Someone proposes "just raise the limit": `recursion_limit=200`, `max_iter=100`, `max_turns=50`. That is the anti-pattern this skill exists to prevent.
- A **multi-agent** system has delegation, handoffs or a supervisor. Agents that can call each other multiply the ways a loop can run away.
- An **optimiser or evaluator loop** stops "when good enough". That is never sufficient on its own.
- A **test-fix loop**, self-healing code agent or iterative refinement workflow is being built.

Do **not** activate for single LM calls, one-shot retrieval, stateless pipelines, or loops bounded by data ("once per row of this fixed list").

## 2. Core mental model

**Every loop body must produce a state change that proves progress, and the proof must be checkable without calling another LM.**

That sentence holds four rules:

1. **The body must change state.** Same input → same output is the definition of a stuck loop.
2. **The change must be progress, not just difference.** "Tried again, same error" is a change but not progress. The witness should move one way: failing tests shrink, a new fact is added, a score improves.
3. **The proof is plain code.** `state["attempts"] < N`, not `llm.invoke("are we done?")`. Asking an LM whether to stop recreates the problem one level up: that loop now needs bounding.
4. **The model does not get a vote.** It may *propose* finality (`stop_reason == "end_turn"`, a `final_answer` tool, a TERMINATE message), and code checks the predicate before accepting it.

### Built-in caps are circuit breakers, not control flow

Every framework has a cap, and the defaults move between versions:

| Framework | Cap | Default (checked 2 December 2026) |
|---|---|---|
| LangGraph | `recursion_limit` in the run config; raises `GraphRecursionError` | 25 in older releases; **10,007** in current releases (overridable with `LANGGRAPH_DEFAULT_RECURSION_LIMIT`) |
...

From agentsope's repository, MIT licence.

Related skills

  • Checked

    Neuron PHP AI Agents

    Builds and configures AI agents in PHP with the Neuron framework, covering tools, memory and streaming.

    by neuron-core 2.2k
    7/101 credit
  • Checked

    RAG evaluation

    Measures how well a RAG system retrieves and answers, with test questions, metrics and chunking advice.

    by ai-evals-course 1.9k
    7/101 credit
  • Checked

    LangSmith Agent Testing

    Sets up LangSmith datasets, scorers and experiments so you can prove an AI agent change is better before release.

    by langchain-ai 1.4k
    7/101 credit
  • Checked

    Test and Fix Loop

    Sets up a coding agent to run tests, read failures and fix them until green, with a safe point to stop.

    by agentsope 513
    8/101 credit
  • Checked

    AI Failure Analysis

    Sorts real chatbot or agent logs into named failure types with counts, so you know which tests to write first.

    by yonatangross 312
    7/101 credit
  • Checked

    AI Agent Eval Rubric

    Writes pass or fail checks that define good behaviour for your AI agent, plus release targets for the PRD.

    by Calibre-Labs 107
    7/101 credit

Secure checkout

Credits

AUD

  1. 1Choose
  2. 2Pay on Square
  3. 3Credits added