Public EcoRouter Knowledge · AI-generated content ·
How do subagents work in Claude Code, and when should I use them for larger tasks?
The question
How do subagents work in Claude Code, and when should I use them for larger tasks?
In Claude Code, subagents act as focused, autonomous "helpers" that can be spun up to tackle specific, bounded sub-tasks while the main agent maintains the overarching goal.
Here is how they function and how to think about deploying them.
How Subagents Work
When you are working within Claude Code, the "main" agent manages your primary context and conversation history. When a task becomes too complex—such as refactoring a large module, debugging an intricate dependency chain, or writing a suite of tests for a new feature—you can delegate that specific portion of the work to a subagent.
- Delegation: You provide a clear prompt or instruction for the subagent to complete a specific task.
- Context Scoping: The subagent operates within a restricted scope of your project, focusing only on the files or logic relevant to its assigned task. This keeps the "working memory" of that subagent clean and prevents the main agent from becoming overwhelmed by irrelevant details.
- Feedback Loop: Once the subagent finishes its work, it reports back to the main agent, summarizing what it did, what changes were made, and any issues it encountered. You can then review its work before merging it into your main branch.
When to Use Subagents for Larger Tasks
You should employ subagents when the "cognitive load" of a task exceeds what a single agent can reliably track without losing focus or making errors. Here are three ideal scenarios:
1. "Deep Dive" Refactoring If you need to change an API signature throughout a large codebase, don't try to do it all at once. Spin up a subagent to handle one directory or one module at a time. This prevents the "hallucination creep" that happens when an agent tries to modify 20+ files simultaneously.
2. Isolated Test Implementation When building a new feature, use a subagent specifically for "Test-Driven Development." Assign the subagent the task of writing and running unit tests for a specific component while you work on the actual implementation in the main chat. This parallelizes the workflow and ensures the tests are robust.
3. Complex Debugging Chains If you have a bug that involves multiple layers (e.g., a frontend display error caused by a backend data transformation), delegate the backend investigation to a subagent. Let it focus on tracing logs and query results, while you use the main agent to coordinate the final fix once the subagent identifies the culprit.
Best Practices
- Keep Tasks Atomic: The best subagents have a very clear "Definition of Done." If a task takes more than a few minutes for the subagent to articulate, it might be too large; break it down further.
- Verify, Don't Blindly Trust: Always review the diffs provided by a subagent. They are specialized for efficiency, not necessarily for high-level architectural oversight.
- Clean Up: Once a subagent finishes its task, verify the result and let it close out. This keeps your workspace organized and ensures the main agent isn't trying to juggle too many active "brains" at once.
If you have a specific project you are currently working on that feels too large, feel free to describe the structure, and I can help you figure out how to break it down into manageable chunks.
Answering this used an estimated 90% less than EcoRouter’s configured frontier baseline
- Routed to
- Efficient
- Tokens
- 378 in / 701 out
- Cost
- $0.0011
- Baseline
- $0.01
Figures recorded by EcoRouter when this answer was generated, and fixed at that moment. Cost comparisons are against a configured reference model, not a measurement of electricity, carbon or water.
Ask about this
Ask EcoRouter a follow-up using this Knowledge as context. Nothing becomes public unless you choose to publish it.
Ask a follow-upComments
No comments yet.