Sub topic 8
Giving Each Subagent Only the Tools It Needs
This chapter assumes you know what a subagent is, how an orchestrator hands work to one, and how tool calls work in an agent loop. It covers one design decision: which tools and permissions each subagent gets.
Why scope tools at all
A subagent acts through its tools. If every subagent can edit any file, run any shell command, and reach the network, one mistaken or misled subagent can damage the whole repository. A narrow tool set limits that damage. It also helps the model, because fewer tools means fewer wrong choices.
Apply least privilege: give a subagent the minimum access its brief requires, and nothing more.
Match tools to the role
Start from what the task must do, then list the tools that make it possible.
| Subagent role | Needs | Should not have |
|---|---|---|
| Code reader or researcher | Read files, search, list directories | Write files, run shell commands |
| Test writer | Read files, write to `tests/`, run the test command | Edit `src/`, install packages |
| Docs updater | Read files, write to `README.md` and `docs/` | Shell access, network access |
| Reviewer | Read files, read diffs, run linters | Write access to the code under review |
A reviewer that can edit the code it reviews will tend to fix problems silently. You then lose the independent check that made the review worth running.
Scope more than the tool list
Allowing a tool is only the first layer. Narrow each one further.
- Paths. Allow writes only to the files named in the brief's scope. A test writer gets
tests/test_cli.py, not the whole repository. - Commands. Allow specific commands, such as
pytest tests/test_cli.py, rather than an open shell. - Network. Turn it off unless the task needs it. The example brief in this tutorial says tests must run offline, so the subagent needs no network.
- Credentials. Do not pass secrets the task does not use. Treat anything a subagent can read as something it can leak into its summary.
The scope in the brief and the permissions in the runtime should agree. The brief tells the subagent what is expected. The permissions enforce it.
Show the scope in the orchestrator
The orchestrator can keep each task's allowed tools next to its brief. The sketch below extends the earlier Task class with an explicit allow-list and rejects any tool call outside it.
from dataclasses import dataclass, field
@dataclass
class Task:
name: str
brief: str
allowed_tools: frozenset = field(default_factory=frozenset)
writable_paths: tuple = ()
def check_tool_call(task: Task, tool: str, path: str | None = None) -> None:
if tool not in task.allowed_tools:
raise PermissionError(f"{task.name}: tool '{tool}' is not allowed")
if tool == "write_file":
if path is None or not path.startswith(task.writable_paths):
raise PermissionError(f"{task.name}: cannot write to '{path}'")
tests_task = Task(
"add-tests",
"Add unit tests for parse_args in tests/test_cli.py.",
allowed_tools=frozenset({"read_file", "write_file", "run_tests"}),
writable_paths=("tests/",),
)
check_tool_call(tests_task, "write_file", "tests/test_cli.py") # passes
try:
check_tool_call(tests_task, "write_file", "src/cli.py")
except PermissionError as err:
print(err) # add-tests: cannot write to 'src/cli.py'In a real system the agent runtime performs this check before it executes each call. Keep the check outside the model, so the model cannot talk its way past it.
Common mistakes
- Copying the parent's full tool set to every subagent because it is easier.
- Giving write access to a read-only task such as research or review.
- Granting an open shell when one test command would do.
- Scoping tools but leaving parallel subagents in the same checkout. Tool limits do not prevent two writers from overwriting each other, so pair them with separate branches or worktrees.
Exercise
You are delegating two tasks: one subagent finds every caller of a deprecated function, and a second subagent rewrites those call sites in the files the first one found. What tools and write access should each subagent get?
Show answer
The first subagent only searches, so give it read and search tools and no write access or shell. The second needs to read and write, limited to the files the first subagent listed, plus the command that checks its changes, such as the project's test command. The tasks are also sequential, because the second depends on the first one's output.