Sub topic 2
Giving Each Agent Only the Tools Its Task Needs
Assume you understand basic LLM tool calling, APIs, authentication tokens, and access control. This chapter focuses on four controls that limit an agent’s blast radius.
Start with a minimal tool set
An agent should receive only the tools required for its current task. A documentation agent may need search_docs and read_file, but it probably does not need send_email, delete_record, or unrestricted shell access.
Fewer tools reduce both accidental misuse and the effect of a prompt injection. Review each tool’s purpose, data access, and side effects before adding it.
TOOLS = {
"search_docs": lambda query: f"results for {query}",
"read_file": lambda path: f"contents of {path}",
}
ALLOWED_TOOLS = {"search_docs", "read_file"}
def run_tool_call(name: str, args: dict) -> str:
if name not in ALLOWED_TOOLS:
raise PermissionError(f"{name} is not allowed for this task")
return TOOLS[name](**args)Enforce authorization in code
The runtime, rather than the model, must decide whether a call is permitted. Check the tool name, the caller, the task, and the requested operation in deterministic code. Apply the same checks whether the request came from the model, a user interface, or a retry mechanism.
Constrain tool arguments
A permitted tool can still be dangerous with unrestricted arguments. Validate paths, identifiers, destinations, and operation types. Prefer constrained values over free-form strings.
For example, a file reader can resolve a path and reject anything outside its workspace:
from pathlib import Path
ROOT = Path("/srv/agent/workspace").resolve()
def read_file(path: str) -> str:
target = (ROOT / path).resolve()
if not target.is_relative_to(ROOT):
raise PermissionError("path is outside the workspace")
return target.read_text()Use scoped, short-lived credentials
Give each task the smallest credential scope it needs. Prefer a token that can read one bucket prefix for one user and expires soon, rather than a long-lived account-wide key. Per-user credentials also preserve ownership and make audit records meaningful.
Exercise
Why should authorization be enforced outside the prompt?
Show answer
Model output is not a security boundary. Deterministic runtime checks can reject unlisted tools and invalid arguments regardless of what the model was instructed to do.