Sub topic 8
Giving Each Subagent Only the Tools It Needs
This chapter assumes you have used a coding agent that calls tools, and that you know what a subagent is and how an orchestrator hands it a brief. It covers how to decide which tools and permissions each subagent receives, and what goes wrong when you skip that decision.
Why scoping matters
A subagent's brief says what it should do. Its tool list decides what it can do. These are different controls, and only the second one is enforced by the runtime. A brief that says "do not change files under src/" is a request. A subagent that has no write access to src/ cannot break that rule, even if it misreads the brief or follows a bad instruction from a file it read.
Scoping tools gives you four benefits:
- Smaller blast radius. A mistake or a prompt injection (text in a file or web page that tries to steer the agent) can only use the tools the subagent holds.
- Better focus. A model choosing among five tools makes fewer odd choices than one choosing among fifty.
- Less context used. Every tool definition takes space in the context window. Fewer tools leave more room for the task.
- Easier review. If a subagent was read-only, you know it changed nothing, and you can skip checking its diff.
The guiding rule is least privilege: give a subagent the minimum access its task needs, and nothing more.
The dimensions you can restrict
Tool scoping is more than a list of tool names. Think about four dimensions.
| Dimension | Question | Example restriction |
|---|---|---|
| Tool set | Which tools exist for this subagent? | Read, search, and list only. No shell. |
| Paths | Where may it read or write? | Write only to `tests/`. |
| Commands | If it has a shell, which commands may run? | `pytest` and `git diff` only. |
| Network and credentials | What can it reach, and with what identity? | No network. No cloud credentials. |
Your runtime may support only some of these. Many agent runtimes let you list allowed tools per subagent, and some add path or command rules. Check what yours enforces. Where it enforces nothing, a restriction exists only as text in the brief, and you should treat it as a request.
Three common roles
Most coding subagents fit one of three profiles.
- Researcher. Reads code and answers questions. Tools: file read, search, directory listing. No write, no shell. A researcher can run in parallel with anything, because it changes nothing.
- Implementer. Edits a defined set of files. Tools: file read, file write limited to specific paths, and a shell limited to the checks for its task, such as a test runner.
- Reviewer. Inspects another agent's output. Tools: read, search, and
git diff. No write access, so it cannot quietly fix what it is judging.
The reviewer profile deserves attention. If a reviewer can edit files, it may "fix" a problem instead of reporting it, and the orchestrator then receives work that no one independent has checked.
A worked example
Suppose the orchestrator splits a change into two tasks: update the README, and add tests for parse_args. The scoping below is expressed as plain Python data so you can see the decision clearly. The format is illustrative, so adapt the field names to your runtime.
from dataclasses import dataclass, field
@dataclass(frozen=True)
class Scope:
tools: frozenset[str]
write_paths: tuple[str, ...] = ()
shell_allow: tuple[str, ...] = ()
network: bool = False
SCOPES = {
"update-docs": Scope(
tools=frozenset({"read", "search", "write"}),
write_paths=("README.md",),
),
"add-tests": Scope(
tools=frozenset({"read", "search", "write", "shell"}),
write_paths=("tests/test_cli.py",),
shell_allow=("pytest tests/test_cli.py",),
),
}
def check_write(task: str, path: str) -> bool:
scope = SCOPES[task]
if "write" not in scope.tools:
return False
return path in scope.write_paths
def check_shell(task: str, command: str) -> bool:
scope = SCOPES[task]
return "shell" in scope.tools and command in scope.shell_allow
if __name__ == "__main__":
print(check_write("update-docs", "README.md")) # True
print(check_write("update-docs", "src/cli.py")) # False
print(check_shell("add-tests", "pytest tests/test_cli.py")) # True
print(check_shell("add-tests", "rm -rf build")) # FalseThe docs subagent has no shell at all, since editing a README does not require running anything. The test subagent has a shell, but only for the one command that proves its work. The checks run outside the model, in the orchestrator or runtime. A check that the model itself performs is not a control.
Exact-match command lists are the strictest option and the easiest to reason about. Prefix or pattern matching is more flexible but riskier. An allowance such as pytest * also permits pytest --rootdir=/ ; ... if the runtime passes the string to a shell without parsing it. Prefer exact commands, or run the command yourself and give the subagent only the result.
How scope connects to the brief and the workspace
Scope works together with two other controls from earlier chapters.
- The brief states the same boundaries in words. The brief's "Scope" line (create or edit only
tests/test_cli.py) should match thewrite_pathssetting. When they disagree, the subagent receives contradictory signals and may stall or work around the limit. - The workspace adds a physical boundary. A subagent in its own git worktree can only damage its own branch, even if its path rules are loose. Path rules and worktrees protect against different failures, so use both for parallel implementers.
Edge cases and common errors
Hidden write access through the shell. A subagent with no write tool but with an unrestricted shell can still write files with echo > or sed -i. A shell is a general-purpose tool. If you want a subagent to be read-only, remove the shell or restrict it to specific read-only commands.
Secrets in the environment. A subagent that can run commands inherits the environment variables of the process, which often include API keys. Start subagent shells with a cleaned environment, and pass only the variables the task needs.
Reading is not always safe. Read access to the whole repository includes .env files, private keys, and other secrets. If the task does not need them, exclude those paths. The content a subagent reads can also end up in its summary, which flows back into the parent's context.
Over-restriction. A subagent that lacks a tool it needs will burn turns trying alternatives, or return a weak result and report success. Signs of this are repeated rejected calls and summaries that say "I was unable to...". Look at the rejection log, then widen the scope by the smallest amount that unblocks the task.
Tools that spawn agents. If a subagent can start its own subagents, your scoping applies only to the first level. Unless you want nested delegation, remove the delegation tool from workers. This also prevents runaway fan-out, where agents keep creating more agents and cost grows without bound.
Silent rejection. When the runtime blocks a call, return a clear error that names the rule, such as "write to src/cli.py is outside allowed paths". A vague failure leads the model to retry the same call. A clear one lets it stop or report the blocker.
A decision procedure
For each subagent, work through these steps when you write its brief.
- List what the task must read, write, and run to reach its "Done when" condition.
- Start from no tools. Add the smallest set that covers the list.
- Narrow writes to named files or directories, not the whole repository.
- Replace open shell access with exact commands, or do the command yourself.
- Remove network access and credentials unless the task states a need.
- Remove delegation tools from workers.
- Run the task. If rejections show a real need, widen one item at a time.
Summary of the key points
- The tool list is the enforced control. The brief is the advisory one. Use both, and keep them consistent.
- Restrict four things: tools, paths, shell commands, and network or credentials.
- Researchers and reviewers should be read-only. Implementers get narrow write paths.
- A broad shell undoes most other restrictions.
- Widen scope only in response to evidence from a rejected call.
Exercise
You give a "reviewer" subagent the read, search, and shell tools, with the shell unrestricted, because it needs to run git diff. Name two ways this breaks the read-only intent, and give a safer setup.
Show answer
First, an unrestricted shell can write or delete files (for example with sed -i or rm), so the reviewer is not truly read-only and might fix code instead of reporting problems. Second, the shell inherits environment variables, so it can reach API keys and other secrets, and it can use the network. A safer setup removes the general shell and allows only the exact commands git diff and git log (or the orchestrator runs git diff and passes the output in the brief), with a cleaned environment and no network.
Exercise
A subagent assigned to add tests keeps returning "unable to complete" and the runtime log shows many rejected calls to pytest. What is the likely cause, and how should you fix it?
Show answer
The scope is too tight: the subagent has no permission to run its test command, so it cannot reach its "Done when" condition. Add the exact command pytest tests/test_cli.py to its shell allowlist, and make sure the rejection message names the rule. Widen only that one item, then rerun.