# Sub-Agent

> The Sub-Agent is an AI agent launched by another agent for a separate subtask in its own context, often defined in a file with its own instructions and tools.

## What is a sub-agent and how do I create one?

The agent launched by a main agent for a specific subtask, such as searching a codebase or reviewing changes, in its own context window. It can be defined in a file with its own instructions, tools and model, and returns only a short result, keeping the main context clean.

- Topic: AI Agents
- Type: concept

## Example

Wrong:

```text
Fix the bug we discussed earlier.
```

Right:

```text
Fix the crash in src/cart/total.ts when the cart is empty.
Repro: cartTotal([]) throws 'Cannot read properties of undefined'.
Do not change the public API.
Return a short summary of the fix and the files changed.
```

The sub-agent cannot see the parent's chat, so the task must carry the context, the limits and the expected summary.

## Common mistakes

- The sub-agent redoes work because its prompt lacks key details.

## Key points

- The task given to the sub-agent includes all context it needs, since it does not see the parent's chat.
- The sub-agent returns a short summary, not its full transcript.
- Each sub-agent gets only the tools its job needs, for example a reviewer without write access.
- Its description says clearly when the main agent should delegate to it.

## Interview questions

### Why use sub-agents?

To keep the main context small: a sub-agent explores in its own context and returns only a summary. Independent sub-agents can also run in parallel.

### Do sub-agents share context with the main agent?

No. Each starts with a fresh context and gets only its task prompt, its own instructions and its tools.

### When is a sub-agent a bad idea?

For small tasks or work that needs the full conversation. Each handoff loses detail and costs extra tokens.

## Related cards

- [Multi-Agent System](https://wonderium.dev/ai/multi-agent-system.md)
- [Parallel Agents](https://wonderium.dev/ai/parallel-agents.md)
- [Agent Skills](https://wonderium.dev/ai/agent-skills.md)
- [Agent Permissions](https://wonderium.dev/ai/agent-permissions.md)
- [Context Window](https://wonderium.dev/ai/context-window.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/sub-agent
- Last updated: 2026-11-25
