A Claude Code plug-in for running fresh context agents:

https://github.com/omarclaflin/cletus-loop

I’ve been using this for nearly a year, attempted to submit this to the Claude Code Marketplace 8 months ago, but finally heard back this month that the Bash script included in it caused it to be rejected. Unfortunately, the Claude-proposed solution (paste the script into an MD file, so Claude Code can then paste and run it, essentially was the proposed solution shrouded in a lot of extra fancy AI-harness lingo) will not work. Briefly, the reason is because it turns what should be a deterministic execution step into an indeterministic one.

More colorfully, its because Claude starts crafting ‘interpretations’ (e.g. neurotically firing off pseudo-Socratic questions, shotgun-grepping to answer its own pontifications, and then truncating random chunks of the script based on a superficial word similarities to unknown earlier instructions or fine-tuned behaviors; then later ‘rationalizing’ a myriad of made-up reasons when asked for why it didn’t run the script as is) as it intervenes unnecessarily in what should be a straightforward script. The answer is not injecting more “‘DO NOT xyz” into the conflation engine, but simply run the bash script I created a year ago as a plug-in (which you can install yourself from my repo). Somewhat amusingly, the failed pattern of the suggested fix by Claude is, in itself, the meta-problem Cletus Loops was designed to solve.

Also, giving Claude itself ‘awareness’ of what Cletus Loops are (by having it retranscribe the script, or adding rationale to its design, or even awareness of its purpose), grants it a much higher probability chance of Claude imploding in recursive confusion as it attempts to implement its ‘own’ cletus loop inside the cletus loop, or free-form improv-jazzer-coding its own broken stopping conditions for the cletus loop, or make unmentioned alterations to its own copy-pasted Cletus Loop, or much worse, “deduce” the ultimate end state of the loop and jump to it itself.

Problem: Context Contamination

Cletus Loops is solution I came up with a year ago to context-contamination, which forces Claude Code to spin up an independent version of itself with strict rules via a bash script. You may think it does that already, but it doesn’t. Since I made this tool, they’ve closed old issues via a stale bot but not fixed the problem, and added a new parameter ‘fork’ which in theory, when turned off (but its turned on by default), doesn’t send the entire parent context to the child agent. However, a quick Claude-internet search reveals this contamination issue is still alive 8 months later:

Most relevant (parent's conversation or content reaching children):
- https://github.com/anthropics/claude-code/issues/95369
- https://github.com/anthropics/claude-code/issues/97076
- https://github.com/anthropics/claude-code/issues/93736
Parent's session state reaching children, contrary to the docs:
- https://github.com/anthropics/claude-code/issues/92750
- https://github.com/anthropics/claude-code/issues/87613
- https://github.com/anthropics/claude-code/issues/88886
Closed by the stale bot:
- https://github.com/anthropics/claude-code/issues/90782
- https://github.com/anthropics/claude-code/issues/60803

I haven’t read all these thoroughly but most seem to be concerned around token efficiency (from unnecessarily sending copies of parent conversations/tool lists/etc to child agent). However, my reasons for avoiding context contamination from parent-to-child agents are more functional.

Why Cletus Loops, Like Actually

If you’re trying to set up anything with an adversarial type of loop:

  • create new test –> separate agent to modify code so it passes test;
  • set up analytical workflow that isn’t pure code –> have an agent run that workflow without cheating
  • have one bot add –> one bot remove, and not have them be aware of each other, ‘conclude’ a final result and jump to that (“Ive moved your entire to-do list to another spot, you’re welcome”)

this was necessary step for me. Simply put, the ‘checking’ agent erroneously gets the reasons for the tests, how the test is built, and then starts conflating all those things in its token-map-fake-logic brain when it should be just be evaluating, and inventing failures or successes where there aren’t. Or, conversely, the ‘test writer’ doesn’t simply receive the issue and write a test, but gets all the other context and simply writes a test case that allows current code to pass. Or it first ‘infers’ how the current code should be fixed (which it shouldn’t even be allowed to see) and writes that into its test. And so forth.

Every hyped up article anthropomorphizing this behavior as some kind of unleashed sentient latent genius, makes my multiboiled blood very tired. Informational value is quickly degraded if context walls between them are corroded, which is why I wrote a simple bash script to firewall my Claude Agents.

What you can build with it

Nearly a year ago, I also built my own AI Coding framework using Cletus Loops as the fundamental unit here:
https://github.com/omarclaflin/AICodingFramework (Briefly, it takes in a spec sheet, builds what you want — using recursive patterns to keep adding requirements, in a firewalled way. Its also a very expensive way to use AI but when building a new engine from scratch, was the solution I came up with a year ago to actually accomplish it without pulling out all my hair finding endless hidden code messes and hidden gaps, despite a detailed spec sheet.)

.

.

.

Cletus Readme.md below:

https://github.com/omarclaflin/cletus-loop

.

.

.

.

cletus-loop

A Claude Code plugin that runs iterative agent loops with fresh context per iteration. Ralph Wiggum, meet the even simpler Cletus Spuckler.

Basically, a bash loop inside Claude Code. (Or a bash loop that calls Claude Code iteratively, if you prefer.)

Problem

Existing loop approaches in Claude Code have context problems:

ApproachIssue
Ralph loop (stop hook)Same session reused. History accumulates. Agent loses focus after many iterations.
Automated subagent loop (Task/Agent tool)Child results leak into parent context. Parent contaminates subsequent child prompts (via prompt generation contaminated by history).
LLM Orchestrator + Task tool with verbatim promptAs conversation history grows, prompt pass-through becomes increasingly nondeterministic — it paraphrases, summarizes, or injects its own context instead of forwarding verbatim.
External bash loopThis works, has clean isolation, but a plugin gives us agency options and abstractions inside CC.

Basically, I wanted to setup loops with proper firewalls/without context accumulation, but realized I had to keep restarting /ralph or just externally looping claude code. Why? Because of context accumulation and information leaking.

To make this clear, briefly, consider a meta-loop with a ‘diagnostic & improvement’ agent who should only see the outputs of an independent automated unit test, but instead the inner workings of that test is ‘leaked’ to it (via chat summaries between agents or chat history) allowing it to cheat/lazily hack around it.

I thought I was hallucinating issues until I saw claude-code#14118.

Solution

Cletus-loop spawns a new claude process per iteration. Each iteration:

  1. Starts with zero conversation history
  2. Reads a prompt file
  3. Reads tracker files on disk to know what’s done
  4. Does one unit of work
  5. Updates the tracker
  6. Exits

No parent agent. No accumulated history. Files are the only state.

Packaged as a CC plugin so agents inside CC can call it dynamically.

Install

# Local testing
claude --plugin-dir ./cletus-loop
# Or install from a marketplace
/plugin install cletus-loop@your-marketplace

Usage

Basic loop with a prompt file [–file]

/cletus-loop:cletus-loop --file PROMPT.md --max-iterations 10 --completion-string "ALL DONE"

Loop with a prompt string

/cletus-loop:cletus-loop "Read TASKS.md, pick one undone task, do it, mark it done. If all done output ALL DONE." --max-iterations 10

Kill agent after each unit of work rather than allowing it to stop on its own [–iteration-string]

(to prevent agent from creatively spiralling; sometimes with sloppy prompts, it ‘infers’ its own task list within a loop; also, be careful not to include terms it would use in its task work so it doesn’t end prematurely like “CHECK TASK LIST”)

/cletus-loop:cletus-loop --file PROMPT.md --iteration-string "TASK FINISHED" --completion-string "ALL DONE" --max-iterations 20

Named loop [-name]

(so you can cancel it if running multiple loops)

/cletus-loop:cletus-loop --file PROMPT.md --name proofreader --iteration-string "TASK FINISHED" --completion-string "ALL DONE"

Require multiple completion confirmations [–triplecheck]

/cletus-loop:cletus-loop --file PROMPT.md --completion-string "ALL DONE" --triplecheck 3

Multi-prompt loop [–subprompt]

Run a sequence of prompts that cycle in order: A, B, C, A, B, C, …

Each --subprompt starts a new group. Inside each group, specify --file or --prompt, and optionally --iteration-string to kill that agent the moment it signals done. --completion-string is shared — any subprompt outputting it stops the entire loop.

/cletus-loop:cletus-loop \
--subprompt --file STEP_A.md \
--subprompt --file STEP_B.md --iteration-string "B DONE" \
--subprompt --file STEP_C.md --iteration-string "C DONE" \
--completion-string "ALL DONE" \
--max-iterations 30

Inline strings work the same way with --prompt:

/cletus-loop:cletus-loop \
--subprompt --prompt "Do step A. If all done output ALL DONE." \
--subprompt --prompt "Do step B. Output B DONE when finished." --iteration-string "B DONE" \
--completion-string "ALL DONE" \
--max-iterations 20

Which subprompts check for completion: --completion-string is checked after every iteration regardless of which subprompt ran. If you only want prompt C to be able to stop the loop, write the completion string into C’s prompt file and omit it from A and B’s instructions — the agents control when they output it, so the prompt controls when completion is possible.

Which subprompts use iteration-string: only subprompts that declare --iteration-string get the kill-on-signal behavior. Subprompts without it run to natural completion. This lets you mix: A runs freely, B and C are killed the moment they signal done.

Variable substitution [–var]

Replace {{KEY}} placeholders in prompt content before each iteration.

/cletus-loop:cletus-loop --file PROMPT.md --var REPO=myapp --var BRANCH=main --max-iterations 10

If your prompt contains {{REPO}}, it becomes myapp before being passed to claude. Repeatable — pass --var once per variable. Works with both single and multi-prompt modes (vars are global, applied to all subprompts).

Limitation: substitution uses bash string replacement, so {{KEY}} must appear literally in the prompt. No nested or dynamic keys.

Cancel a running loop

/cletus-loop:cancel proofreader

Show help

/cletus-loop:help

From another agent/skill

"${CLAUDE_PLUGIN_ROOT}/scripts/cletus-loop.sh" --file PROMPT.md --max-iterations 10 --completion-string "ALL DONE"

Pattern for writing prompt files

Your prompt file should follow the one-item-per-iteration pattern:

Read TRACKER.md to see what's done.
If everything is done, output "ALL DONE".
Pick ONE item from SOURCE.md not in TRACKER.md.
Do the work. Write output to output_files/.
Append to TRACKER.md.
Output "TASK DONE".

Testing

The test/ folder has a ready-made single-prompt test (pick words one at a time into a collector) and a multi-prompt test (A picks, B uppercases, cycles until all words are processed).

Single-prompt test

Uses test/PROMPT.md and test/WORDS.txt. Each iteration picks one uncollected word and appends it to test/COLLECTED.txt. Stops when all 5 words are collected.

Bash version:

echo -n "" > test/COLLECTED.txt
./scripts/cletus-loop.sh --file test/PROMPT.md --completion-string "ALL DONE" --max-iterations 10

Claude Code version:

# Reset first
/cletus-loop:cletus-loop --file test/PROMPT.md --completion-string "ALL DONE" --max-iterations 10

Multi-prompt test

Uses test/multipromptA.md and test/multipromptB.md. Subprompt A picks one word into MULTIPROMPT_PICKED.txt and signals done immediately. Subprompt B takes the latest unprocssed word, uppercases it into MULTIPROMPT_UPPERCASED.txt, and outputs “ALL DONE” when every word is processed. Cycles A→B→A→B… for 10 iterations total.

Bash version:

rm -f test/MULTIPROMPT_PICKED.txt test/MULTIPROMPT_UPPERCASED.txt
./scripts/cletus-loop.sh \
--subprompt --file test/multipromptA.md --iteration-string "A DONE" \
--subprompt --file test/multipromptB.md \
--completion-string "ALL DONE" \
--max-iterations 20

Claude Code version (reset tracker files first):

/cletus-loop:cletus-loop --subprompt --file test/multipromptA.md --iteration-string "A DONE" --subprompt --file test/multipromptB.md --completion-string "ALL DONE" --max-iterations 20

Why not just…

Ralph loop (stop hook, same session)? Context accumulates. By iteration 15, the agent is dragging around the full history of iterations 1–14. Wasted tokens on finished work, and the model starts anchoring to earlier mistakes.

Subagent loop (Task tool)? The parent agent’s context leaks into child agents. Children aren’t truly independent — they’re influenced by what the parent has seen and done. See claude-code#14118.

Plain bash loop outside Claude Code? Clean isolation, but you lose agency. Other agents and skills inside CC can’t call it. It’s just a cron job at that point.

Cletus loop gets you fresh-process isolation (like bash) while staying callable from inside CC (like subagents). The tradeoff is that all inter-iteration state lives in files on disk — which is the point.

Summary

Children of LLM orchestrators are affected by the parent’s history and knowledge.

Ralph is a single child who never leaves his room accumulating context junk.

Cletus children are parentless so they never inherit anything.

I know its simple (basically a for loop for Claude Code). But maybe it’ll be helpful to you, goodluck and godspeed
–Omar Claflin

License

MIT

Posted in