Files

92 lines
2.4 KiB
Markdown

---
name: autonomous-loops
description: Autonomous loop patterns for AI agents — sequential pipelines, retry loops, DAG orchestration. Use when building self-correcting workflows or multi-step automation.
---
# Autonomous Loop Patterns
Based on ECC autonomous-loops skill.
## Pattern 1: Sequential Pipeline
Run steps A → B → C → D, each depending on the previous.
```python
result = step_a(input)
result = step_b(result)
result = step_c(result)
output = step_d(result)
```
**When to use**: Linear data processing, ETL, content generation pipeline.
**Key**: Each step validates its output before passing to next.
## Pattern 2: Retry with Self-Correction
Run task, check result, if fails → diagnose → fix → retry → max N times.
```
MAX_RETRIES = 3
for attempt in range(MAX_RETRIES):
result = run_task()
errors = validate(result)
if not errors:
break
fix_errors(errors) # Self-correct based on validation
else:
raise Exception(f"Failed after {MAX_RETRIES} attempts")
```
**When to use**: Code generation with validation, test fixing, migration scripts.
**Key**: The fix step must be SPECIFIC — generic retries don't work.
## Pattern 3: DAG Orchestration
Tasks with dependencies forming a Directed Acyclic Graph.
- Independent tasks run in parallel
- Dependent tasks wait for prerequisites
```
A ──→ B ───→ D
──→ C ──→
```
**When to use**: Multi-agent coordination, build pipelines, complex deployments.
**Key**: Detect cycles in dependency graph before execution.
## Pattern 4: Observer Loop
Continuous monitoring with alert-on-change.
```
while running:
state = observe()
if state != expected:
alert(state)
adapt()
sleep(check_interval)
```
**When to use**: CI monitoring, resource monitoring, system health.
**Key**: Avoid tight loops — add backoff, throttling.
## Observer Reliability
- **Memory explosion fix**: Use tail sampling (keep last N observations)
- **Throttling**: Rate-limit checks to avoid token waste
- **Lazy start**: Begin observations only after setup complete
- **Re-entrancy guard**: Don't start loop if already running
## Best Practices
1. Always have a MAX_RETRIES or timeout
2. Log every iteration for audit
3. Make errors SPECIFIC so fix step can act
4. Don't retry the same prompt — adapt it
5. For DAGs: validate no cycles before start
6. For observers: throttle, don't poll aggressively