Sub topic 5
Giving Each Agent Only the Tools Its Task Needs
This chapter assumes you know how LLM tool calling works, can read short Python and JSON snippets, and understand roles and permissions. Tool scoping is the first practical control. It limits what the model can ask for before any argument or credential check happens.
Why the tool set is the first boundary
An agent can only misuse a tool it can call. If a prompt injection (hostile text in the agent's input) persuades the model to act, the damage is limited to the tools in scope. Every extra tool adds reachable behavior, and it also adds tool descriptions to the context, which gives the model more ways to choose badly.
Scoping is a design step you do per task, not per application. A documentation assistant and a support-ticket agent should not share one tool list.
Derive the tool set from the task
Start from the task and work down:
- Write the task as one sentence, such as "answer questions from the public docs."
- List the actions that sentence needs, such as search and read.
- Map each action to the narrowest tool that does it.
- Mark each tool as read-only, write, or irreversible.
- Remove anything you cannot justify in one line.
Prefer several narrow tools over one general tool. A run_sql(query) tool can do anything the database account allows. A get_order_status(order_id) tool can do one thing.
| Task | Tools granted | Tools withheld |
|---|---|---|
| Answer questions from docs | `search_docs`, `read_file` | `send_email`, `delete_record` |
| Summarize a ticket | `get_ticket` | `update_ticket`, `read_file` |
| Notify a customer | `get_ticket`, `send_email` (with approval) | `delete_record` |
Enforce the allowlist in code
Listing only the right tools in the prompt is not enough. The model might name a tool you did not offer, whether through injection or a hallucinated name. The runtime must check each requested call against a per-task allowlist.
from pathlib import Path
ALLOWED_ROOT = Path("/srv/agent/workspace").resolve()
def read_file(path: str) -> str:
target = (ALLOWED_ROOT / path).resolve()
if not target.is_relative_to(ALLOWED_ROOT):
raise PermissionError("path is outside the workspace")
return target.read_text()
def search_docs(query: str) -> str:
return f"results for {query}"
TOOLS = {
"read_file": read_file,
"search_docs": search_docs,
}
def run_tool_call(name: str, args: dict, allowed: set[str]):
if name not in allowed:
raise PermissionError(f"tool {name!r} is not allowed for this task")
return TOOLS[name](**args)
# A documentation-answering task gets read-only tools only.
result = run_tool_call(
"search_docs",
{"query": "rotate API keys"},
allowed={"search_docs", "read_file"},
)
print(result)The allowlist is ordinary code that the model cannot edit. If the model requests send_email here, run_tool_call raises PermissionError before anything runs. The next chapters cover how to enforce authorization more broadly and how to constrain arguments such as the path in read_file.
Common errors
- Sharing one tool list across tasks. Build the allowlist per task or per agent role, not once for the whole application.
- Granting a tool "just in case." Add tools when a task needs them, not when it might.
- Using one general tool. Shell, generic HTTP, and raw SQL tools put the whole boundary inside the model's judgment.
- Trusting the tool list sent to the model. Hiding a tool from the prompt does not stop a call to it. Check at execution time.
- Forgetting multi-agent setups. If a sub-agent has tools the parent lacks, the parent can reach them by delegating.
Exercise
An agent's task is "summarize the status of a customer's order." Its tools are get_order_status(order_id), update_order(order_id, fields), and run_sql(query). Which would you keep, and where do you enforce that choice?
Show answer
Keep only get_order_status. The task needs a read of one order, and the other two tools allow writes or arbitrary data access. Enforce it in the runtime with a per-task allowlist such as {"get_order_status"} that is checked in run_tool_call before any tool executes, not only by leaving the other tools out of the prompt.