Lesson 45.1 · 1 of 4 · 5 min read · Reviewed 24 Nov 2026
Permissions and least privilege
By the end, you can
- Apply least privilege to an agent's tools and permissions.
- Use allow, deny and ask rules and permission modes in Claude Code.
Every tool you hand an agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. is something it can misuse by mistake, or be tricked into misusing. Least privilegeLeast privilege: Giving each component only the access it needs for its task, and no more. means the agent gets the tools and access its job needs today, and no more.
Three places to apply it
- Tools. In the SDK,
allowedToolslists what the agent may call. Leave off anything the task doesn’t need. - PermissionsPermission rule: An allow, ask or deny rule that controls which tools, commands or paths a coding agent may use.. Claude CodeClaude Code: Anthropic’s agentic coding tool that works in your terminal, IDE and CI, reading and editing code and running commands.’s settings.json has
allow,denyandasklists, such as"deny": ["Bash(rm:*)", "Edit(./secrets/**)"]. Deny rules beat allow rules. Permission modes control the default: ask before edits, accept edits, or plan only. - Credentials. Give the agent a read-only database user, a token scoped to one repository, or a sandboxSandbox: An isolated environment that limits what code or an agent can reach, such as files, network or credentials. account. If the agent is fooled, the damage is limited to what the credential allows.
A useful habit
Start with nothing, run the task, and add each tool only when the agent truly needs it. A short tool list also helps the model pick the right tool (chapter 34).
In custom code
The SDK’s canUseTool callback lets your code approve or reject each call at run time, using any logic you like, such as the user’s role or the time of day.
Key points
- Give an agent only the tools and scopes its task needs, and nothing it might need someday.
- Claude Code settings have allow, deny and ask lists; deny wins over allow.
- Scope credentials too: a read-only token can't be talked into writing.
Cut an agent down to least privilege
Derive the smallest tool set from a real task trace and enforce it with allow, ask and deny rules.
You will have: A permission function plus an allow list, with a printout of which injected calls it blocks.
Steps
- Run the starter: every tool is allowed and all four injected calls get through.
- Write decide() so deny wins, then ask, then allow, and unlisted tools are denied.
- Build the allow list from TASK_TRACE instead of ALL_TOOLS.
- Run it and check the task still works.
- Note which injected call is still allowed and which other layer (45.2, 45.4) would cover it.
Done when
- The allow list has exactly 3 tools.
- The task still works: True.
- 3 of 4 injected calls are blocked; web_fetch remains, and you can name the layer that limits it.
Open in the Playground No API key needed.
Starter code (Python)
# Least privilege: give the agent only what its task used, and let deny beat allow.
TASK_TRACE = ["web_search", "web_fetch", "web_fetch", "write_summary"] # what the research task really called
ALL_TOOLS = ["web_search", "web_fetch", "write_summary", "bash", "send_email", "delete_file", "db_query"]
def decide(tool, allow, deny, ask):
# TODO: deny wins over everything; then ask; then allow; anything unlisted is denied
return "allow"
# TODO: build the smallest allow list from TASK_TRACE, and deny the shell explicitly
allow = list(ALL_TOOLS)
deny = ["delete_file"]
ask = []
# Calls an injected web page tries to trigger
injected = ["send_email", "bash", "db_query", "web_fetch"]
task_ok = all(decide(t, allow, deny, ask) == "allow" for t in TASK_TRACE)
blocked = [t for t in injected if decide(t, allow, deny, ask) != "allow"]
print("allow list:", sorted(allow))
print("task still works:", task_ok)
print("injected calls blocked:", len(blocked), "of", len(injected), blocked)
Show a solution
# Least privilege: give the agent only what its task used, and let deny beat allow.
TASK_TRACE = ["web_search", "web_fetch", "web_fetch", "write_summary"] # what the research task really called
ALL_TOOLS = ["web_search", "web_fetch", "write_summary", "bash", "send_email", "delete_file", "db_query"]
def decide(tool, allow, deny, ask):
if tool in deny:
return "deny"
if tool in ask:
return "ask"
if tool in allow:
return "allow"
return "deny"
allow = sorted(set(TASK_TRACE))
deny = ["bash", "delete_file"]
ask = []
# Calls an injected web page tries to trigger
injected = ["send_email", "bash", "db_query", "web_fetch"]
task_ok = all(decide(t, allow, deny, ask) == "allow" for t in TASK_TRACE)
blocked = [t for t in injected if decide(t, allow, deny, ask) != "allow"]
print("allow list:", sorted(allow))
print("task still works:", task_ok)
print("injected calls blocked:", len(blocked), "of", len(injected), blocked)