agent-lifecycle
# Agent Lifecycle Management
What this skill does
# Agent Lifecycle Management
Complete guide to managing sub-agent and teammate lifecycle in Claude Code — health checks, idle detection, cleanup, retention, and the mandatory audit loop.
## The Orchestration-First Principle
Claude should **prefer to orchestrate** rather than do work directly. When a task has multiple components or would benefit from specialized review:
1. Break the task into work units
2. Assign each to the best-fit agent type
3. Run agents in parallel where possible
4. Audit every agent's output
5. Clean up when done
**Direct work is the fallback, not the default.**
## Agent Lifecycle States
```
┌──────────┐
┌────────→│ active │──────────┐
│ └──────────┘ │
│ │ │
spawn/resume no output completes
│ >60s │
│ ↓ ↓
┌──────────┐ ┌──────────┐ ┌──────────┐
│ spawning │ │ idle │ │ completed│
└──────────┘ └──────────┘ └──────────┘
│ │
no response collected?
>120s │
↓ yes/ \no
┌──────────┐ ↓ ↓
│ stalled │ released orphaned
└──────────┘
│
terminate
↓
┌──────────┐
│ errored │
└──────────┘
```
## Health Check Protocol
### When to Check
- **During orchestration**: Every 2 minutes
- **After fan-out**: Once all agents are spawned
- **Before fan-in**: Before collecting results
- **On completion**: Before producing final output
### How to Check
For **foreground agents**: They return results when complete. No polling needed.
For **background agents**: Use `SendMessage` to check in:
```
SendMessage(to: "agent-id", message: "Status check: progress on your task?")
```
For **agent teams**: Teammates communicate via shared task list. Check `TaskList` for stale assignments.
### Check-In Message Template
```
"Status check on your assigned task: [{task}].
Reply with one of:
- WORKING: {current activity}
- BLOCKED: {what's blocking}
- DONE: {summary}
- NEEDS_HELP: {what you need}"
```
## Idle Detection & Cleanup
### Detection Signals
| Signal | Threshold | Action |
|--------|-----------|--------|
| No output | 60s | Send check-in |
| No response to check-in | 30s | Mark stalled |
| Stalled | 180s | Terminate, reassign |
| Completed but uncollected | 60s | Collect results |
| Errored | Immediate | Collect partial, log |
### Cleanup Procedure
```yaml
cleanup_steps:
1_identify:
- List all agent IDs from spawn records
- Check each agent's last known state
- Identify: completed, idle, stalled, errored
2_collect:
- For completed agents: gather results
- For errored agents: gather partial output + error
- For stalled agents: attempt final check-in
3_terminate:
- Release completed agents (results collected)
- Terminate stalled agents (no response after 3 min)
- Terminate errored agents (unrecoverable)
- Keep active agents running
4_report:
- Log cleanup summary
- Report any lost work
- Suggest task reassignment if needed
```
### Token-Aware Cleanup
Track cumulative token usage across all agents:
```
Per-agent budget = total_budget / num_agents
If agent exceeds 150% of per-agent budget → flag for review
If total usage exceeds 80% of budget → switch to cheaper models
If total usage exceeds 95% → terminate non-essential agents
```
## The Mandatory Audit Loop
**Every agent's work must be audited before acceptance.**
### Audit Flow
```
Agent completes task
↓
Orchestrator spawns audit agent (code-reviewer or audit-reviewer)
↓
Audit agent reviews:
- Completeness (did it do everything asked?)
- Correctness (is the output right?)
- Consistency (does it match project style?)
- Security (any vulnerabilities introduced?)
- Tests (are new things tested?)
↓
Audit verdict:
PASS → accept, merge into deliverables
PASS_WITH_NOTES → accept, log improvement notes
FAIL → send back to original agent with specific fixes
↓
Re-do (max 2 rework rounds)
↓
Re-audit
↓
If still fails → escalate to user
```
### Audit Agent Selection
| Original Agent | Audit Agent | Focus |
|----------------|-------------|-------|
| Builder/Implementer | `code-reviewer` | Correctness, completeness |
| Test writer | `code-reviewer` | Test quality, coverage |
| Security reviewer | `audit-reviewer` (opus) | False positives, missed vectors |
| Researcher | `audit-reviewer` (opus) | Source quality, bias |
| Doc writer | `code-reviewer` | Accuracy, completeness |
| Infrastructure | `security-reviewer` | Misconfigs, blast radius |
### Audit Depth Levels
```yaml
audit_levels:
quick:
checks: [completeness, correctness]
model: haiku
cost: low
standard:
checks: [completeness, correctness, consistency, security]
model: sonnet
cost: medium
thorough:
checks: [completeness, correctness, consistency, security, testing, documentation]
model: opus
cost: high
```
## Agent Team Lifecycle
### For Claude Code Agent Teams (Experimental)
Teams have additional lifecycle considerations:
```yaml
team_lifecycle:
spawn:
- Lead session creates team
- Lead defines task list with dependencies
- Teammates join and claim tasks
active:
- Teammates self-coordinate via TaskList
- Direct messaging via SendMessage
- Lead monitors overall progress
idle_detection:
- Teammate hasn't claimed new task in 120s
- Teammate's current task hasn't progressed
- TeammateIdle hook fires
cleanup:
- Lead sends final status check to all teammates
- Collect all teammate outputs
- TeamDelete to dissolve team
- Prune any worktrees created
audit:
- Lead reviews each teammate's work
- Cross-check: teammate A audits teammate B's output
- Final synthesis by lead
```
### Cross-Audit Pattern
In teams of 3+, use cross-auditing:
```
Agent A completes → Agent B audits A's work
Agent B completes → Agent C audits B's work
Agent C completes → Agent A audits C's work
```
This distributes audit load and provides diverse perspectives.
## Retention Policies
### When to Keep an Agent Alive
- **Pipeline stage**: Agent is between steps, waiting for upstream input
- **Expensive context**: Agent has loaded large codebase context that would cost tokens to rebuild
- **Follow-up planned**: User may ask for iterations on agent's work
- **Background monitoring**: Agent is running periodic checks
### When to Terminate
- **Task complete, results collected**: No further value
- **Idle >3 minutes, no planned follow-up**: Wasting potential tokens
- **Stalled despite check-ins**: Likely stuck, won't recover
- **Budget exceeded**: Cost ceiling reached
- **Circular work**: Agent is repeating itself
## Best Practices
1. **Always track agent IDs** — maintain a mental or written registry of every agent spawned
2. **Collect results immediately** — don't let completed agents sit uncollected
3. **Use background mode sparingly** — foreground is easier to manage
4. **Match model to task** — haiku for research, sonnet for implementation, opus for architecture/audit
5. **Set max_turns** — prevent agents from running indefinitely
6. **Use worktree isolation** — for agents that write code, prevent conflicts
7. **Audit before accepting** — never skip the second-round review
8. **Clean up before finishing** — no orphaned agents when orchestration ends
Related in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.