Mastering Claude Code: How to Optimize Token Usage with Sub-Agents
糖果姐姐API服务 的 AI API 使用建议
糖果姐姐API服务 面向需要 OpenAI 兼容接口、Claude/Gemini/GPT 多模型切换、包月额度管理和图像模型调用的用户。阅读本文后,可以结合本站的模型清单、独立使用文档和个人面板,把教程内容直接落到实际调用流程中。
Mastering Claude Code: How to Optimize Token Usage with Sub-Agents
As AI-powered coding tools become more integrated into the developer workflow, managing efficiency and cost—specifically token usage—has become a top priority. Anthropic recently released a set of best practices for its CLI-based AI coding tool, Claude Code, focusing on the concept of "Sub-agents."
By treating AI agents like web browser tabs, developers can maintain cleaner contexts, work faster, and significantly reduce unnecessary token consumption. In this post, we’ll dive into how to use sub-agents effectively and when it's better to stick to a single session.

The Analogy: AI Agents as Web Browser Tabs
One of the most helpful ways to visualize Claude Code sub-agents is to think of them as tabs in a web browser.
When you research a complex topic, you don't try to cram everything into one window. You open a main tab for the overview and several secondary tabs for specific documentation, code examples, or bug reports.
In Claude Code, sub-agents work the same way. Rather than making the primary agent handle every single line of code and every test case—which bloats the context window and increases costs—you delegate specific, isolated tasks to sub-agents.
5 Effective Ways to Use Sub-Agents
Anthropic suggests five primary scenarios where spawning a sub-agent is the most efficient choice:
1. Handling Specialized Tasks
If you need to generate documentation, write unit tests, or refactor a specific module, a sub-agent is perfect. It focuses exclusively on that task without being distracted by the rest of the application's logic.
2. Exploring Unfamiliar Codebases
When jumping into a new project, you can use a sub-agent to "scout" the architecture. While you work on a feature in the main session, the sub-agent can map out dependencies or explain how a specific service works in the background.
3. Parallelizing Your Workflow
AI doesn't have to be sequential. You can have a sub-agent running a long-winded migration or a comprehensive test suite while you continue to prompt the main agent for logic changes. This multi-tasking approach speeds up the development cycle.
4. Maintaining State Isolation
Sometimes you want to try a "what if" scenario without ruining your current working state. A sub-agent allows you to experiment with a risky refactor in a sandbox-like environment. If it fails, you simply close the "tab."
5. Deep Dives into Specific Bugs
Debugging often requires looking at logs, stack traces, and environment variables. Spawning a sub-agent specifically for a single bug prevents your main conversation from being cluttered with hundreds of lines of error logs that won't be relevant once the bug is fixed.
When to Stop: Avoiding the Anti-Patterns
While sub-agents are powerful, overusing them can lead to fragmentation and confusion. Anthropic warns against several common mistakes:
- Over-Fragmentation: Don't spawn a sub-agent for every tiny change. If a task takes 10 seconds to explain but only 5 seconds to do, keep it in the main session.
- State Dependency: Avoid using sub-agents for tasks that require constant, real-time feedback from the main session. If the two agents need to "talk" too much, the overhead of syncing them outweighs the benefits.
- Ignoring the Context Window: Remember that even sub-agents use tokens. If you provide a sub-agent with too much unnecessary global context, you aren't saving as much as you think.
Conclusion: A New Era of CLI Efficiency
Claude Code represents a shift in how we interact with LLMs—moving from simple chat interfaces to sophisticated, agentic workflows. By mastering the use of sub-agents, developers can keep their project contexts lean, their token costs low, and their focus sharp.
Next time you feel your AI session getting sluggish or cluttered, remember: open a new tab.
在本站快速上手 Claude / GPT
本文涉及的能力可以直接在本站的中转 API 上调用,兼容 OpenAI / Anthropic 官方 SDK:
无需科学上网,国内可直连,5 分钟完成接入。