| name | managing-long-running-tasks |
|---|---|
| description | Patterns for orchestrating multi-session tasks with background agents. Use when a task requires many agent iterations, has rate limits, or needs state tracking across sessions. |
Create a state file at task start:
{
"total": 100,
"completed": ["item1", "item2"],
"pending": ["item3", "item4", ...],
"errors": {"item5": "rate limited"}
}Update after each agent completes. Never infer state from output files.
- Batch size: 30-50 items max per agent
- Pass explicit lists: Give agents the exact items to process, not "figure out what's remaining"
- Verify before continuing: Check counts match expected before launching next agent
Include in every agent prompt:
- Explicit item list - The exact items to process this batch
- Output file path - Where to write results
- Schema - Full schema, every time (agents have no memory)
- Boundaries - "ONLY process items from this list, nothing else"
- Exit conditions - Specific criteria, not just "DO NOT STOP"
Example structure:
Process these 30 items: [list]
Output file: /path/to/results.json
Schema: {full schema}
ONLY add items from the list above.
Stop when: all 30 processed OR rate limited 3x in a row.
After each agent completes:
- Run deduplication on output file
- Update state file (move completed items)
- Verify counts:
completed + pending + errors = total - Launch next agent with remaining items
When agents hit rate limits:
- Log to errors in state file
- Wait before relaunching
- Retry failed items in next batch
Task is complete when:
pendingis empty- All items in
completedorerrors - Final verification: output file count matches expected