The Loop Pattern
The foundation of all AI agent orchestration.
Core Concept
while task not complete:
1. Read current state
2. Execute action (call AI)
3. Verify result
4. Update state
This simple pattern is the backbone of reliable AI automation. It’s not sophisticated, but it works.
Why Loops Matter
AI models are probabilistic, not deterministic. They:
- Sometimes fail to follow instructions
- Occasionally hallucinate
- May produce incomplete output
- Can misunderstand context
A loop compensates for this by trying again until verified success.
The Basic Loop
def basic_loop(task):
max_attempts = 5
attempt = 0
while attempt < max_attempts:
attempt += 1
# 1. Execute
result = call_ai(task)
# 2. Verify
if verify(result):
return result # Success!
# 3. Log and retry
print(f"Attempt {attempt} failed, retrying...")
raise Exception("Max attempts exceeded")
Key Components
1. State Management
Track what’s been done and what remains:
state = {
"task": "Write a haiku",
"status": "pending",
"attempts": 0,
"result": None
}
2. Execution
Call the AI model with the task:
response = client.messages.create(
model="claude-sonnet-4-20250514",
messages=[{"role": "user", "content": task}]
)
3. Verification
Confirm the output meets requirements:
def verify(result):
# Check length, format, content, etc.
return len(result.split('\n')) == 3 # Haiku has 3 lines
4. Safety Limits
Prevent infinite loops:
if attempt >= max_attempts:
raise CircuitBreakerTripped("Too many attempts")
Loop Variations
Sequential Loop
Process tasks one at a time:
for task in task_queue:
result = loop_until_done(task)
save_result(result)
Retry with Backoff
Add delays between attempts:
import time
delay = 1 # seconds
for attempt in range(max_attempts):
result = try_task()
if verify(result):
return result
time.sleep(delay)
delay *= 2 # Exponential backoff
Conditional Retry
Only retry certain types of failures:
result = try_task()
if result.error_type == "rate_limit":
time.sleep(60)
retry()
elif result.error_type == "invalid_output":
retry_with_clearer_prompt()
else:
fail_permanently()
Anti-Patterns
No Exit Condition
# BAD: Can loop forever
while True:
result = try_task()
if verify(result):
break
No State Persistence
# BAD: Loses progress on crash
while not done:
result = try_task() # If this crashes, start over
Trusting AI Self-Assessment
# BAD: AI says it's done, but is it?
result = ai.complete_task()
if result.says_complete: # Don't trust this alone
return result
Best Practices
- Always have a max attempts limit
- Persist state externally (file, database)
- Use external verification when possible
- Log every attempt for debugging
- Implement graceful degradation
Next Steps
- Learn about Verification Strategies
- Understand Circuit Breakers
- See it in action: Lab 01: Your First Loop