Cost-first delegation patterns and decision frameworks for multi-AI coordination
Delegates tactical work to FREE/cheap AI spawners (Gemini/Codex/Copilot) while you focus on strategy. Triggers when you use keywords like "orchestrator," "delegation," or "subagent" for complex tasks requiring research, code generation, or git operations.
/plugin marketplace add Shakes-tzd/htmlgraph/plugin install htmlgraph@htmlgraphThis skill inherits all available tools. When active, it can use any tool Claude has access to.
reference.mdUse this skill for delegation patterns and decision frameworks in orchestrator mode.
Trigger keywords: orchestrator, delegation, subagent, task coordination, parallel execution, cost-first, spawner
Orchestration means delegating tactical work to specialized subagents while you focus on strategic decisions. This saves Claude Code context (expensive) by using FREE/CHEAP AIs for appropriate tasks.
Basic pattern:
Task(
subagent_type="htmlgraph:gemini-spawner", # FREE - use for exploration
description="Find auth patterns",
prompt="Search codebase for authentication patterns..."
)
When to use: Complex tasks requiring research, code generation, git operations, or any work that could fail and require retries.
For complete guidance: See sections below or run /multi-ai-orchestration for model selection details.
Claude Code is EXPENSIVE. You MUST delegate to FREE/CHEAP AIs first.
<details> <summary><strong>Cost Comparison & Pre-Delegation Checklist</strong></summary>Ask these questions IN ORDER:
Can Gemini do this? → Exploration, research, batch ops, file analysis
Is this code work? → Implementation, fixes, tests, refactoring
Is this git/GitHub? → Commits, PRs, issues, branches
Does this need deep reasoning? → Architecture, complex planning
Is this coordination? → Multi-agent work
ONLY if above fail → Haiku (fallback)
| Task | WRONG (Cost) | CORRECT (Cost) | Savings |
|---|---|---|---|
| Search 100 files | Task() ($15-25) | Gemini spawner (FREE) | 100% |
| Generate code | Task() ($10) | Codex spawner ($3) | 70% |
| Git commit | Task() ($5) | Copilot spawner ($2) | 60% |
| Strategic decision | Direct task ($20) | Claude Opus ($50) | Must pay for quality |
WRONG (wastes Claude quota):
- Code implementation → Task(haiku) # USE Codex spawner
- Git commits → Task(haiku) # USE Copilot spawner
- File search → Task(haiku) # USE Gemini spawner (FREE!)
- Research → Task(haiku) # USE Gemini spawner (FREE!)
CORRECT (cost-optimized):
- Code implementation → Codex spawner # Cheap, sandboxed
- Git commits → Copilot spawner # Cheap, GitHub-native
- File search → Gemini spawner # FREE!
- Research → Gemini spawner # FREE!
- Strategic decisions → Claude Opus # Expensive, but needed
- Haiku → FALLBACK ONLY # When spawners fail
</details>
Orchestrator (You):
Executor (Subagent):
Why separation matters:
What looks like "one bash call" becomes many:
Context cost comparison:
Direct execution (fails):
bash call 1 → fails
bash call 2 → fails
bash call 3 → fix code
bash call 4 → bash call 1 retry
bash call 5 → bash call 2 retry
= 5+ tool calls, context consumed
Delegation (cascades isolated):
Task(subagent handles all retries) → 1 tool call
Read result → 1 tool call
= 2 tool calls, clean context
Token savings:
Ask yourself these questions:
Will this likely be ONE tool call?
Does this require error handling?
Could this cascade into multiple operations?
Is this strategic or tactical?
Rule of thumb: When in doubt, DELEGATE. Cascading failures are expensive.
</details> <details> <summary><strong>Three Allowed Direct Operations</strong></summary>Only these can be executed directly by orchestrator:
Task() - Delegation itself
Task(subagent_type="htmlgraph:gemini-spawner", ...)AskUserQuestion() - Clarifying requirements
AskUserQuestion("Should we use Redis or PostgreSQL?")TodoWrite() - Tracking work items
TodoWrite(todos=[...])SDK operations (create features, spikes, bugs):
sdk.features.create()sdk.spikes.create()sdk.bugs.create()Everything else MUST be delegated.
</details>Decision tree (check each in order):
Is this exploration/research/analysis?
Is this code implementation/testing?
Is this git/GitHub operation?
Does this need deep reasoning?
Is this multi-agent coordination?
All else fails → Task() with Haiku (fallback)
Spawner Subagent Types:
htmlgraph:gemini-spawner - FREE, 2M tokens/minhtmlgraph:codex-spawner - Cheap code specialisthtmlgraph:copilot-spawner - Cheap git specialistgeneral-purpose - Generic Claude (use as fallback or when spawners fail)Task(
subagent_type="htmlgraph:gemini-spawner",
description="Analyze authentication patterns",
prompt="""
Analyze codebase for:
- All authentication patterns
- OAuth implementations
- Session management
- JWT usage
"""
)
Best for:
Task(
subagent_type="htmlgraph:codex-spawner",
description="Implement OAuth middleware",
prompt="""
Implement OAuth authentication:
- Sandbox mode: workspace-write
- Add JWT token generation
- Include error handling
- Write unit tests
"""
)
Best for:
Task(
subagent_type="htmlgraph:copilot-spawner",
description="Commit and create PR",
prompt="""
Commit changes and create PR:
- Message: "feat: add OAuth authentication"
- Files: src/auth/*.py, tests/test_auth.py
- Create PR with description
"""
)
Best for:
Task(
prompt="Design authentication architecture...",
subagent_type="sonnet" # or "opus"
)
Sonnet (Mid-tier):
Opus (Expensive):
Simple exploration:
Task(
subagent_type="htmlgraph:gemini-spawner",
description="Find all auth patterns",
prompt="Search codebase for authentication patterns and summarize findings"
)
Code implementation:
Task(
subagent_type="htmlgraph:codex-spawner",
description="Implement OAuth endpoint",
prompt="Implement OAuth authentication endpoint with JWT support"
)
Git operations:
Task(
subagent_type="htmlgraph:copilot-spawner",
description="Commit changes",
prompt="Commit changes with message: 'feat: add OAuth authentication'"
)
</details>
<details>
<summary><strong>Parallel Delegation (Multiple Independent Tasks)</strong></summary>
Pattern: Spawn all at once, retrieve results independently
# Create all tasks in parallel (single message)
Task(
subagent_type="htmlgraph:gemini-spawner",
description="Research auth patterns",
prompt="Analyze existing authentication patterns..."
)
Task(
subagent_type="htmlgraph:codex-spawner",
description="Implement OAuth",
prompt="Implement OAuth flow..."
)
Task(
subagent_type="htmlgraph:copilot-spawner",
description="Create PR",
prompt="Commit and create pull request..."
)
# All run in parallel, optimized for cost:
# - Gemini: FREE
# - Codex: $ (cheap)
# - Copilot: $ (cheap)
Benefits:
Pattern: Chain dependent tasks in sequence
# 1. Research existing patterns
Task(
subagent_type="htmlgraph:gemini-spawner",
description="Research OAuth patterns",
prompt="Find all OAuth implementations in codebase..."
)
# 2. Wait for research, then implement
# (In next message after reading result)
research_findings = "..." # Read from previous task result
Task(
subagent_type="htmlgraph:codex-spawner",
description="Implement OAuth based on research",
prompt=f"""
Implement OAuth using discovered patterns:
{research_findings}
"""
)
# 3. Wait for implementation, then commit
Task(
subagent_type="htmlgraph:copilot-spawner",
description="Commit implementation",
prompt="Commit OAuth implementation..."
)
When to use: When later tasks depend on earlier results
</details> <details> <summary><strong>HtmlGraph Result Retrieval</strong></summary>Subagents report findings automatically:
When a Task() completes, findings are stored in HtmlGraph:
# SDK can retrieve results
from htmlgraph import SDK
sdk = SDK(agent='orchestrator')
# Get recent spike (subagent's findings)
spike = sdk.spikes.list(limit=1)[0]
findings = spike.get_findings()
Pattern: Read findings after Task completes
# 1. Delegate exploration
Task(
subagent_type="htmlgraph:gemini-spawner",
description="Analyze auth patterns",
prompt="Find all authentication patterns..."
)
# 2. Read findings from HtmlGraph
sdk = SDK(agent='orchestrator')
recent_spike = sdk.spikes.list(limit=1)[0]
findings = recent_spike.get_findings()
# 3. Use findings in next delegation
Task(
subagent_type="htmlgraph:codex-spawner",
description="Implement based on findings",
prompt=f"Implement authentication:\n{findings}"
)
</details>
<details>
<summary><strong>Error Handling & Retries</strong></summary>
Let subagents handle retries:
# WRONG - Don't retry directly as orchestrator
bash_result = Bash(command="git commit -m 'feat: new'")
if failed:
# Retry directly (context pollution)
Bash(command="git pull && git commit") # More context used
# CORRECT - Subagent handles retries
Task(
subagent_type="htmlgraph:copilot-spawner",
description="Commit changes with retry",
prompt="""
Commit changes:
Message: "feat: new feature"
If commit fails:
1. Pull latest changes
2. Resolve conflicts if any
3. Retry commit
4. Handle pre-commit hooks
Report final status: success or failure
"""
)
Benefits:
How it works:
CLAUDE_ORCHESTRATOR_ACTIVE=trueWhy: Preserve orchestration discipline after context compact
What you see:
To manually trigger:
/orchestrator-directives
Environment variable:
CLAUDE_ORCHESTRATOR_ACTIVE=true # Set by SDK
</details>
<details>
<summary><strong>Session Continuity Across Compacts</strong></summary>
Features preserved across compact:
What's lost:
Re-activation pattern:
Before compact:
- Work on features, track in HtmlGraph
- Delegate with clear prompts
- Use SDK to save progress
After compact:
- Orchestrator Skill auto-activates
- Re-read recent spikes for context
- Continue delegations
- Use Task IDs for parallel coordination
</details>
Principle 1: Delegation > Direct Execution
Principle 2: Cost-First > Capability-First
Principle 3: You Don't Know the Outcome
Principle 4: Parallel > Sequential
Principle 5: Track Everything
Delegation > Direct Execution. Cascading failures consume exponentially more context than structured delegation.
Cost-First > Capability-First. Use FREE/cheap AIs before expensive Claude models.
| Operation | MUST Use | Cost | Fallback |
|---|---|---|---|
| Search files | Gemini spawner | FREE | Haiku |
| Pattern analysis | Gemini spawner | FREE | Haiku |
| Documentation research | Gemini spawner | FREE | Haiku |
| Code generation | Codex spawner | $ (70% off) | Sonnet |
| Bug fixes | Codex spawner | $ (70% off) | Haiku |
| Write tests | Codex spawner | $ (70% off) | Haiku |
| Git commits | Copilot spawner | $ (60% off) | Haiku |
| Create PRs | Copilot spawner | $ (60% off) | Haiku |
| Architecture | Claude Opus | $$$$ | Sonnet |
| Strategic decisions | Claude Opus | $$$$ | Task() |
Key: FREE = No cost | $ = Cheap | $$$$ = Expensive (but necessary)
</details>from htmlgraph import SDKCost-First Orchestration:
Orchestrator Rule: Only execute: Task(), AskUserQuestion(), TodoWrite(), SDK operations
Everything else → Delegate to appropriate spawner
When in doubt → DELEGATE
Use when working with Payload CMS projects (payload.config.ts, collections, fields, hooks, access control, Payload API). Use when debugging validation errors, security issues, relationship queries, transactions, or hook behavior.
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code, generative art, algorithmic art, flow fields, or particle systems. Create original algorithmic art rather than copying existing artists' work to avoid copyright violations.