AI glossary · Agents and tools
What is a subagent in AI coding agents?
Also called: sub-agent, Claude Code subagent, custom agent
Definition
A subagent is a separate AI worker that a main agent hands a task to, running in its own context window with its own system prompt, tools and model, and returning only its result to the main conversation.
Explained
How does a subagent work?
The main agent sees only that final result, not the steps behind it, and the subagent often runs on a cheaper model. Claude Code, VS Code’s agent mode and agent SDKs such as the AI SDK all use the pattern. Claude Code is the worked example here; our Claude Code subagents guide covers setting one up in full.
In Claude Code, the main conversation can delegate a task, such as searching a codebase or reading a long log, to a subagent. The subagent starts fresh: it doesn’t see your conversation history or the files Claude has already read. It does load your CLAUDE.md, unless its file sets omitClaudeMd: true (the built-in Explore and Plan agents skip it), then works through its own tool calls and hands back a summary.
You define one as a Markdown file with YAML frontmatter, in .claude/agents/ for one project or ~/.claude/agents/ for all of them. Only name and description are required, and the body becomes its system prompt. Claude reads the description to decide when to delegate, so write it as “when to use me”. Built-in subagents include Explore, Plan and general-purpose, and by default a subagent can start its own, up to three layers below the main conversation.
Example
A log-summarising subagent and what its description costs
This file keeps long build and test logs out of the main conversation. tools limits the subagent to reading and searching, and model: haiku runs it on Claude Haiku 5.5, at $0.10 per million input tokens against $2 for Claude Sonnet 5.5 in our daily data.
The description is 36 tokens on OpenAI’s o200k_base tokenizer (Claude’s tokenizer counts differently, so treat it as an estimate). Descriptions take up space in the main context, which is why Claude Code warns at startup when your custom subagents’ descriptions together pass 15,000 tokens.
---
name: log-summariser
description: Reads long build, test or CI logs and returns only the failures, each with file, line and error message. Use proactively when a log is too long to read in full.
tools: Read, Grep, Glob
model: haiku
---
You summarise logs. Find every failure or error in the log you are given.
For each one, report the file, the line and the exact error message, then
one sentence on the likely cause. Never paste the whole log back.Cost and quality
Why use a subagent?
A subagent’s file reads, searches and tool output stay in its own context, so the main conversation stays short and focused, and every later turn re-sends less. Routing routine work to a cheaper model lowers cost further.
It isn’t free. A subagent spends tokens of its own while it runs, which count towards the same usage limits, and many subagents returning long reports can still fill the main context. Ask for short results.
Don’t mix up
Subagent vs skill: what is the difference?
- Subagent vs skill
- By default, a skill loads its instructions into the current conversation when it’s used; a skill with
context: forkruns in a subagent instead. A subagent runs in a separate context window and only its result comes back. - Subagent vs fork
- A normal subagent starts with an empty context plus its task. A fork starts with a copy of your conversation so far, then works separately.
FAQ
Questions people ask
What is the difference between an agent and a subagent?
An AI agent is the main loop you talk to: it plans, calls tools and keeps the conversation. A subagent is an agent that the main one starts for one bounded task, with a fresh context and often a limited tool set. The main agent sees only the subagent’s final report, not its intermediate steps, which keeps the main context window small.
What is an example of a subagent?
A code-review subagent in Claude Code: a Markdown file in .claude/agents/ with a name, a description of when to use it, a tools list limited to reading and searching files, and a body holding its review rules. When you ask for a review, the main agent delegates to it, and it returns a short list of findings instead of every file it read. The subagent builder writes such a file for you.
Can subagents run in parallel?
Yes. A main agent can start several subagents for independent tasks, such as searching different folders, and combine their results. In Claude Code, background subagents run concurrently while you keep working, up to 20 at once by default. Each one spends its own tokens, so parallel subagents finish sooner but can cost more in total than doing the work in one context.
Go deeper
Try it and read more
- Free toolClaude Code Subagent BuilderBuild and check subagent files for .claude/agents.
- Free toolAI Token CounterCount tokens for GPT, Claude, Gemini, DeepSeek, Qwen and more.
- Free toolTools for Claude Code, MCP and coding agentsFree Claude Code tools: build settings.json permissions, hooks, slash commands, subagents and skills, convert Cursor rules, and write CLAUDE.md and MCP configs.
- Guide · 14 min readClaude Code subagents: how to create and use themHow Claude Code subagents work: create one by asking Claude or in .claude/agents, pick tools and model, run them in parallel and see what they cost in tokens.
- Guide · 13 min readHow to reduce Claude Code token usage, and what each fix costs youReduce Claude Code token usage with /clear, /compact, plan mode, exact file references, the right model and effort, a lean CLAUDE.md and fewer MCP servers.
Related