# Subagents

Delegating a scoped task to a separate agent with its own context, returning only the result to the parent.

**Rests on:** Agent Loop

**Leads to:** Agent Communication Protocols, Routing and Expert Specialization

*AI Agents · AI Agents*


## Why learn this

Subagents are the building block of multi-agent systems. What you learn here — how to delegate, why a clean context matters, what makes a handoff good — is the same skill you'll apply when you move on to agent orchestrators, role-based agent teams, and production-scale agent workflows. If you can't decompose work into a parent agent and subagents that return clean results, every multi-agent design you meet later will feel like magic instead of structure. Master this and you unlock: Agentic Workflows (where subagents implement pipelines), Multi-Agent Systems (where the handoff is the whole game), and Agent Memory (where shared context is the hard part).


## The intuition

Imagine you are a project manager at a consulting firm. A client asks for a market analysis report. You don't sit down and write the whole thing from memory. You have a researcher who looks up data, an analyst who makes charts, and a copywriter who drafts the text. Each person gets a clear task, works with their own notes and files, and hands you back a finished piece. You never see every spreadsheet they opened or every draft they wrote — only the deliverables. Subagents are the same idea, but for AI models. Instead of one model trying to do the whole job in a single stretched-out conversation, you have a parent agent (the manager) that decides the plan, and it spins up a separate agent (the researcher) to do one focused piece of work. The subagent has its own context — its own tiny conversation window or scratchpad — and it doesn't know what the parent is doing beyond the task it was given. It works, produces a result, and sends only that result back. The parent never carries the subagent's working notes, its false starts, or its intermediate steps. That separation is the whole point. It keeps the parent's mind clear, so the parent can keep planning and coordinating instead of being swamped with trivia.


## An analogy

Think of a busy restaurant kitchen. The head chef (the parent agent) reads the orders coming in. They don't cook every dish themselves. Instead, they call out a ticket to the grill station, a ticket to the saucier, a ticket to the pastry section. Each station is a subagent: it has its own workspace (its grill, its pans, its ingredients), it knows the recipe for its dish, and it works in parallel with the other stations. When the grill station finishes a steak, it doesn't send the whole ticket back — it sends the plated steak and a quick note like 'medium rare, rested'. The head chef never knows every flip of the spatula. The analogy holds up well, but it breaks down in one important place: subagents in AI don't have their own bodies or hands. They are still just software — the same model architecture, the same kind of code — but wrapped so that they appear to operate separately. The 'station' is a separate context window, and the 'steak' is a text result. That means you, the architect, get to decide what a subagent can see and what it returns. A real kitchen has physical walls; in AI, the wall is something you draw in code.


## Definition

A subagent is a separate agent instance, launched by a parent agent, given a scoped task and its own execution context, that returns only a result to the parent.


## How it connects

You already know what an AI agent is: a loop of decide, act, observe, repeat, with tools and memory. A subagent is still an agent — it has its own loop — but it is deliberately made smaller and more focused. You also know about tool use: a model calling a function to get data. Subagents feel like a more powerful version of a tool call — instead of just getting data, the subagent does a whole sequence of reasoning and tool calls and returns a conclusion. If you have notes on multi-agent systems, you'll see subagents are the building block of them: a parent agent coordinating several subagents, each with its own context, is exactly a multi-agent system. This concept also connects to agent memory: the parent and subagent each have their own memory, and deciding what gets shared (and what gets retired) is the handoff quality that matters.


## The mathematics

There's no heavy arithmetic here — this is an architectural concept, not a numerical one. But there is one quantity worth understanding: the cost. Imagine every token (roughly a word or a part of a word) that the model sends and receives costs a tiny bit of money and a tiny bit of compute. Running one long conversation with a parent carrying thousands of tokens of extra context is more expensive than running a short parent and a short subagent. The maths is just addition and multiplication: the parent's context length plus the subagent's context length, and that sum is what you pay for. The branch of mathematics here is arithmetic — just counting and adding — because that's all that's needed. There are no derivatives, no vectors, just a running total that you want to keep small.

C_total = C_parent_start + C_instruction + C_subagent_work + C_subagent_result


## A worked example

```
Suppose the parent agent starts with a task description of 200 tokens. If it does everything itself, and the work involves 800 tokens of intermediate reasoning (reading a file, checking a schema, iterating on a draft), the parent's context grows to 1000 tokens. With a subagent, the parent writes a short task description for the subagent — 50 tokens — and the subagent does the 800 tokens of reasoning in its own context. The parent's context stays at 250 tokens (the original task plus the 50-token instruction), and the subagent sends back a 100-token summary.

Step 1: Parent only: 200 + 800 = 1000 tokens in context   # The parent carries the whole job.
Step 2: With subagent: parent keeps 200 + 50 = 250 tokens   # The parent writes the instruction and moves on.
Step 3: Subagent uses 800 tokens, returns 100 tokens   # The 800 tokens are spent in the subagent's cheap context.
Step 4: Total tokens seen by parent: 250 + 100 = 350 tokens   # The parent now only sees the summary, not the 800-token reasoning.

Result: The parent's context is 350 tokens instead of 1000 — roughly a third of the cost, purely from delegating. You can do this arithmetic without any formal maths; it's just addition and comparison. The savings grow as tasks get longer, which is why subagents matter at scale.
```


## From scratch, in Python

```python
You don't need a heavy framework to see how subagents work. Let's model one with plain Python — just a function that acts like a subagent. I'll assume you know what a dictionary is (a thing with named slots, like a paper form). A subagent is just a function that takes a task, does some work in its own 'context' (a local variable), and returns a result. The parent is just another function that calls it.

def subagent_work(task):
    # The subagent has its own scratchpad — this is its private context
    scratchpad = []
    scratchpad.append("Started task: " + task)
    scratchpad.append("Thinking...")
    scratchpad.append("Reading a file...")
    # The subagent's work is done; it returns only the result
    return {"result": "found 3 matching records", "summary": "all good"}

def parent_agent(overall_goal):
    # Parent does not do the work itself
    instruction = "Query the database for recent orders."
    result = subagent_work(instruction)
    # Parent only sees the result, not the scratchpad
    return "Goal: " + overall_goal + ". " + result["result"]

# Run it
print(parent_agent("prepare daily report"))
```


## The same thing, with a framework

```python
In practice, you often use a library like LangGraph, CrewAI, or OpenAI's Assistants API. For example, with LangGraph you create separate nodes for your agents and connect them with edges. The library handles the message passing and context isolation underneath. The principle is exactly the same: your parent agent calls a function that runs the subagent's loop, and the subagent returns a structured result.
```


## What the code does

The subagent_work function makes a local list called scratchpad. That list is the subagent's private context — the parent never sees it. The result is a dictionary with two keys, result and summary. The parent calls subagent_work with an instruction, gets back the dictionary, and only extracts the result. The parent never sees the first two scratchpad lines, so its context stays clean. In a real framework, the 'function call' is a message loop, but the idea of isolation and a return value is the same.


## Common misconceptions

**Subagents are a separate physical machine.** — A subagent is a different server running elsewhere.

It's the same model and the same computer; the separation is logical, not physical. It's a different context window, not a different processor. In fact: A subagent is just another agent instance created by the parent, but with its own context state. It runs on the same hardware.

**The parent sees everything the subagent does.** — If I delegate, I have to see all the steps to trust the result.

The whole point is that the parent only receives the final result. Seeing all the intermediate steps would bloat the context, which negates the benefit. In fact: The subagent's context is private; only the returned result crosses the boundary.

**Subagents are just a way to organise code.** — It's just a function call, so it's only about code structure.

The real effects are about context isolation and cost — not just code clarity. The parent's context stays small, which reduces token usage and keeps reasoning focused. In fact: Subagents are a context-management strategy. The code structure is a means, not the end.

**The more subagents, the better.** — Delegation is always good, so I should split every task into tiny pieces.

Each subagent adds overhead: setup, instruction, and result passing. Over-decomposing creates excessive handoff and slowing down, plus more failure points. In fact: Decompose only when the task is long or complex enough to justify the overhead. A good rule: delegate when the parent's context would otherwise grow large.


## In short

Subagents are agents with their own context, launched by a parent to do a scoped task and return only a result. The benefit is context isolation and cost reduction — the parent stays light — and the handoff quality (what goes in and what comes back) is the deciding factor between helpful delegation and wasted effort.


## Where this is used

Subagents are everywhere you need to divide work. In large language models, when you ask a model to 'write a report', an underlying orchestrator might spin up subagents for research, drafting, and fact-checking — each with a separate context so the model doesn't confuse facts across sections. In small models on a device, you can't afford a 10,000-token context, so a subagent approach keeps each task tiny enough to run in a local context window. In retrieval-augmented generation, a subagent might be responsible for a single document's retrieval and reading, returning only the extracted answer; that way, the main agent doesn't have to hold every document in memory. In agentic systems that plan and call tools, subagents are how you delegate a tool call that involves multiple steps: the subagent handles the sequence, the parent just receives the final output. In everyday applied ML, think of a data pipeline: a subagent cleans a dataset, another builds a feature, another trains a model — each on its own, with results passed along. A developer writing this would use a framework like LangGraph, defining nodes for each subagent and connecting them. What breaks when it's done badly: the subagent's context leaks into the parent (defeating the purpose), the handoff doesn't include enough information (so the parent redoes the work), or the subagent is given too much scope and ends up nearly as big as the parent. The cost arithmetic I showed — keeping the parent's context small — is exactly what makes 70-billion-parameter models useful for long tasks: if you tried to do a ten-step task in one context, you'd blow your token budget.


---

Read this on the web: https://basavapurushottam.com/learn-ai/subagents
