Skip to content
AgenticAIWiki
  1. Learn
  2. Part 11
  3. 58. Context engineering
  4. 58.4

Lesson 58.4 · 4 of 7 · 4 min read · Reviewed 24 Nov 2026

Subagent context isolation

By the end, you can

  • Use subagents to keep exploration out of the main context.
  • Design what a subagent returns.

Chapter 43 covered subagentsSubagent: A separate agent instance, with its own context and instructions, that a main agent delegates a task to. for parallel work. They’re also one of the best context tools.

How isolation helps

A subagent has its own context windowContext window: The maximum number of tokens a model can consider at once, counting the input and the output it generates.. It can read 30 files, run searches and try dead ends, then return a few hundred tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id.: “Auth is handled in src/middleware/auth.ts (session check) and src/api/login.ts (token issue). Rate limiting is separate, in src/lib/limits.ts.”

The main agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. sees only that summary.

Design the return

Ask the subagent for a compactCompaction: Summarising older conversation history so a long session fits in the context window while keeping what matters., predictable shape:

  • Findings, with file paths or sources.
  • Confidence or open questions.
  • Nothing else: no narrative of the search.

Costs

Subagents use more tokens overall, because each has its own context. You’re trading total tokens for a cleaner main context and often better results.

Key points

  • A subagent explores in its own context window and returns only a short result.
  • The main agent's context stays focused on the overall task.
  • Ask for a compact, structured result: findings, sources, and what's still unknown.
Hands-on labRuns in the Playground

Isolate a codebase survey in a subagent

Run a survey of many files in a separate subagent context and hand back only a short structured result, then compare the main agent's context with and without it.

You will have: A subagent function that returns findings, paths and open questions, and a comparison of main-context tokens.

Steps

  1. Read FILES: 30 generated source files, two of which handle authentication.
  2. Write subagent(task) with its own context list: it reads every file into that list and records findings.
  3. Return only a dictionary with findings (path and one line each), open_questions and files_read.
  4. Compare the main agent's context size when it reads all files itself and when it only receives the subagent's result.

Done when

  • The result names src/middleware/auth.py and src/api/login.py.
  • The main context with the subagent is at least 20x smaller.
  • The subagent's return has exactly three keys.

Open in the Playground No API key needed.

Starter code (Python)
import json
FILES = {"src/mod_%02d.py" % i: "def handler_%d(req):\n    return render(req)  # plain view code\n" * 20 for i in range(28)}
FILES["src/middleware/auth.py"] = "def check_session(req):\n    # verifies the session cookie\n    return req.session.valid\n" * 5
FILES["src/api/login.py"] = "def issue_token(user):\n    # issues an auth token after login\n    return sign(user)\n" * 5

def tokens(text):
    return max(1, len(text) // 4)

def subagent(task):
    context = [task]
    findings = []
    # TODO: read every file into context; when a file mentions "session" or "auth token",
    # add {"path": ..., "note": ...} to findings
    return {"findings": findings, "open_questions": [], "files_read": 0}

result = subagent("Find where authentication is handled.")
print(json.dumps(result, indent=1))
Show a solution
import json
FILES = {"src/mod_%02d.py" % i: "def handler_%d(req):\n    return render(req)  # plain view code\n" * 20 for i in range(28)}
FILES["src/middleware/auth.py"] = "def check_session(req):\n    # verifies the session cookie\n    return req.session.valid\n" * 5
FILES["src/api/login.py"] = "def issue_token(user):\n    # issues an auth token after login\n    return sign(user)\n" * 5

def tokens(text):
    return max(1, len(text) // 4)

def subagent(task):
    context = [task]
    findings = []
    for path, src in sorted(FILES.items()):
        context.append(src)
        if "session" in src:
            findings.append({"path": path, "note": "session check on each request"})
        elif "auth token" in src:
            findings.append({"path": path, "note": "issues the auth token at login"})
    return {"findings": findings, "open_questions": ["Is rate limiting separate from auth?"], "files_read": len(context) - 1}

result = subagent("Find where authentication is handled.")
print(json.dumps(result, indent=1))
alone = tokens("".join(FILES.values()))
isolated = tokens(json.dumps(result))
print("main context reading files itself:", alone, "tokens")
print("main context with subagent:", isolated, "tokens (%.0fx smaller)" % (alone / isolated))
print("return keys:", len(result))
Quick checkA main agent must survey 30 files to find where auth is handled. How do you protect its context?
How sure are you?
Pick an answer and how sure you are