# Agent Loop Patterns

> Design patterns for building autonomous agent loops, task queues, and multi-agent crew systems with Claude. Use when building self-directed agents, task decomposition systems, or role-based agent workflows. Trigger when users want autonomous execution, agent loops, task planning, crew-based agents, or self-improving workflows.

- Skill: `ultroncore/agent-loop-patterns` (Agent Skill, multi-file: 6 files)
- Install (CLI): `npx skillmds@latest add ultroncore/agent-loop-patterns`
- Raw SKILL.md: https://api.skillmd.com/api/skills/ultroncore/agent-loop-patterns/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: UltronCore (https://skillmd.com/u/ultroncore)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/ultroncore/agent-loop-patterns

---


# Agent Loop Patterns

Autonomous agent loops allow Claude to plan, execute, observe, and iterate without constant human input. These patterns come from AutoGPT, crewAI, and babyagi — distilled into Claude Code-compatible implementations.

## Pattern 1: Minimal task loop (BabyAGI-style)
The simplest autonomous loop: create task, execute, generate next tasks.

```python
from collections import deque

task_queue = deque()
task_queue.append("Research topic X and summarize findings")
completed = []

while task_queue:
    task = task_queue.popleft()
    # Execute with Claude
    result = execute_task(task)
    completed.append({"task": task, "result": result})
    # Generate next tasks based on result
    new_tasks = generate_next_tasks(task, result, completed)
    task_queue.extend(new_tasks[:3])  # cap expansion
```

## Pattern 2: Role-based crew (crewAI-style)
Assign specialized roles to agents for collaborative work.

```
Crew:
  - Researcher: finds information, returns structured findings
  - Analyst: interprets findings, identifies patterns
  - Writer: synthesizes analysis into final output

Each agent:
  - Has a clear role and backstory
  - Receives only relevant context
  - Passes output to next agent
  - Does NOT have access to other agents' tools
```

In Claude Code terms: spawn one subagent per role, pass context explicitly.

## Pattern 3: ReAct loop (Reason + Act)
Standard agentic loop: think → tool call → observe → think again.

```
Step 1: THOUGHT — What do I need to do?
Step 2: ACTION — Call tool (bash, read, web search)
Step 3: OBSERVATION — What did the tool return?
Step 4: THOUGHT — Did this complete the goal? If not, repeat.
Step 5: FINAL ANSWER — Return result when done
```

In Claude Code: this is the default behavior. Reinforce it by structuring prompts as:
"Think step by step. Use tools as needed. Return only when you have a complete answer."

## Pattern 4: Swarm dispatch (AutoGPT-style parallelism)
Split independent tasks across parallel agents, merge results.

```python
# Split
subtasks = decompose_goal(main_goal)  # list of independent tasks

# Dispatch in parallel (Claude Code Agent tool)
# Each agent handles one subtask independently
# Coordinator merges results

# This maps directly to superpowers:dispatching-parallel-agents
```

## Guardrails for agent loops
- Always cap iteration count (max_iterations=10 default)
- Always cap task queue growth (max_new_tasks=3 per step)
- Always have an explicit completion condition
- Log every step — loops that don't log become undebuggable
- Test with a 2-step loop before running full autonomy

## Related skills
dispatching-parallel-agents, subagent-driven-development, claude-usage-orchestrator

## Related Skills
- `multi-agent-orchestration` — orchestrating multiple agents
- `dispatching-parallel-agents` — parallel agent execution
- `self-healing-execution` — error recovery in agents

## GitNexus Index
This skill is indexed by GitNexus for knowledge graph traversal.
Index path: /Users/localuser/.claude/skills/agent-loop-patterns/.gitnexus
Last indexed: 2026-05-23

