Self-Modifying Multi-Agent Systems: Lessons from a Failed Attempt and a Successful Rebuild
Self-modifying loops—where multiple LLM agents collaborate and iterate on their own code—are a promising approach for long-running tasks, but success depends entirely on constraints, not the loop itself. This article walks through a hands-on experiment: a first attempt that burned 6,000+ API calls in a day producing unusable "spaghetti" code, and a second attempt that completed a real complex task in about 40 minutes after fixing model selection, acceptance criteria, structured logging, and staged validation. It closes with practical recommendations for designing your own multi-agent pipelines.
Background: Why Multi-Agent Self-Modification Matters
A technical approach has been gaining attention in the community recently: having multiple LLM agents collaborate on complex tasks while allowing them to modify and iterate on their own working code or configuration during execution, forming an “execute–feedback–improve” closed loop. These are often called “self-modifying loops” or “long-loop agents.”
The appeal comes down to two things:
- Continuity for long-running tasks. In the traditional single-turn invocation model, a model produces output once and for all—when facing complex tasks, one early mistake derails everything downstream. A self-modifying loop lets agents inspect results after each step and correct course.
- Lower human intervention. In theory, with a well-designed loop, agents can autonomously handle the full chain of planning, coding, testing, and fixing—the human only needs to state the final goal.
After seeing a blogger share a “loop execution + multi-agent collaboration” design, I was impressed by its rigorous architecture and broad applicability, so I spent two days replicating it in a self-built agent framework. This article documents the whole process—including one complete failure and one mostly successful attempt—hoping it helps others exploring the same path.
First Attempt: 6,000 API Calls in One Day, Producing a Pile of Junk
For my first attempt, I went aggressive: I let the agent “add features and modify itself,” choosing a fast-inference but relatively weak model, hoping high-speed iteration would compensate for capability.
The results were brutal:
- Runaway API consumption. Over 6,000 calls in a single day, with no meaningful convergence.
- Steadily degrading code quality. The agent generated mountains of files full of duplicate definitions, dead modules, and scripts overwriting each other—classic spaghetti structure.
- Divergence instead of convergence. Without effective constraints, the agent kept creating new files rather than organizing existing code—things got messier with every change.
The post-mortem identified three core causes:
First, the model’s capability didn’t match the task’s complexity. Complex self-modification demands strong planning and context-management abilities. A fast model lacking reasoning depth accumulates errors across a long loop, and each error becomes noise that derails subsequent steps.
Second, no clear termination condition or acceptance criteria. A self-modifying loop without a clear definition of “done” becomes an infinitely diverging process. The agent has no idea when it’s finished, so it just keeps going.
Third, no version boundaries or rollback mechanism. Every modification was written to disk immediately, so once a change introduced a problem, all subsequent work was built on a broken foundation.
Second Attempt: Better Model, Added Constraints, Full Solution in 40 Minutes
The next day, I restarted with a stronger model that had ample quota. I kept the same overall architecture but added the pieces missing from attempt one. It completed the target solution in about 40 minutes with satisfying quality.
1. Use a sufficiently capable model as the controller
The “controller/coordinator” role in multi-agent collaboration must be handled by a model with strong planning ability—it’s the brain of the entire loop. Execution subtasks can be distributed to cheaper, faster models, forming a layered structure: strong models make decisions, lightweight models do the grunt work.
2. Define task boundaries and acceptance criteria up front
In the task definition, I explicitly specified:
- The concrete form of the target output (which files, which functionality);
- Checkpoints for each step (mandatory self-checks after each step);
- The overall termination condition (end when all acceptance items pass).
3. Introduce structured process records
I required the agent to output structured summaries and feedback at each stage rather than freewheeling. The direct benefit: the controller model receives clean, compressed context at every step, avoiding context explosion in long loops.
The conceptual flow looks like this:
while not task.done():plan = controller.analyze(task, feedback)result = executor.run(plan) # Execute the current subtaskfeedback = checker.verify(result) # Structured self-checktask.update(result, feedback)log(step=plan, result=feedback) # Leave an audit trail for later tracing
4. Layered validation: small steps, fast feedback, full traceability
Give each execution phase an independent artifact, verify it first in a sandbox or temporary directory, and only merge into the official output after it passes—preventing a single point of failure from contaminating everything.
Validation on a Real Complex Task
For validation, I skipped toy problems and ran a one-off, real, complex engineering task. The results:
- Each phase produced clear outputs, and the process was fully traceable;
- Final deliverable quality was good—the code structure was complete and needed no major manual rework;
- API call volume dropped by an order of magnitude compared with the first attempt.
To be clear, this was a real complex task with a definite endpoint, not a theoretically “infinite self-modifying loop.” However, the experiment showed that under the current configuration, simply relaxing the termination condition would give the system everything it needs to run in continuous-loop mode—whether to enable infinite looping is a cost and risk decision, not a technical limitation.
Practical Recommendations
- Model selection first. For the controller, prefer “expensive but strong”; execution models can be “cheap and fast.” Never assign planning duties to a weak model to save on call costs—it usually makes the total cost higher.
- Define “done” before you talk about “looping.” A self-loop without acceptance criteria is out of control by definition. Bake acceptance criteria into the task definition early in system design.
- Never skip process logs. Once a multi-agent system fails, structured logs are practically the only way to locate problems. Log the inputs, outputs, and decision rationale of every step.
- Leverage cloud supporting services. As task scale grows, use general cloud services (e.g., object storage for intermediate artifacts, logging services for execution audits, monitoring and alerting for cost and anomaly tracking) to offload the engineering plumbing and keep your focus on agent orchestration logic.
Conclusion
Self-modifying multi-agent collaboration is a genuinely viable technical path, but its success doesn’t depend on whether you can loop—it depends on whether the loop is effectively constrained. The same architecture, paired with an unsuitable model and missing acceptance mechanisms, produces a pile of useless files; paired with clear boundaries and structured feedback, it completes a complex task cleanly in 40 minutes. I hope this complete post-mortem, from failure to success, saves you the tuition I paid.
