# 28.7 Guardrails in Claude Code settings, the Agent SDK and Managed Agents (/building-ai-workers/guardrails-and-agent-security/guardrails-in-each-place)

---
type: Document
title: "28.7 Guardrails in Claude Code settings, the Agent SDK and Managed Agents"
description: "Which tiers Claude Code settings, the Agent SDK and Managed Agents each keep, how a guard reaches a subagent, and what the builder's own session needs."
status: stable
order: 328.07
ksor:
  owner: team:panaversity
  audience:
    - public
  approval:
    by: process:panaversity
    at: 2026-11-30T10:59:25Z
chapter: '28'
part: III
expert_status: builder-only
concepts: [ "28.7" ]
last_verified: '2026-11-30'

sources:
  - id: anthropic-how-we-contain-claude
    title: How we contain Claude across products (Anthropic, accessed 2026-11-30)
    resource: https://www.anthropic.com/research/how-we-contain-claude
  - id: claude-code-hooks-guide
    title: Automate workflows with hooks (Anthropic, accessed 2026-11-30)
    resource: https://code.claude.com/docs/en/hooks-guide
  - id: claude-code-hooks
    title: Hooks reference (Claude Code, Anthropic, accessed 2026-11-30)
    resource: https://code.claude.com/docs/en/hooks
  - id: claude-agent-sdk-hooks
    title: Control execution with hooks (Claude Agent SDK, Anthropic, accessed 2026-11-30)
    resource: https://code.claude.com/docs/en/agent-sdk/hooks
  - id: claude-agent-sdk-permissions
    title: Configure permissions (Claude Agent SDK, Anthropic, accessed 2026-11-30)
    resource: https://code.claude.com/docs/en/agent-sdk/permissions
  - id: claude-code-permissions
    title: Configure permissions (Claude Code, Anthropic, accessed 2026-11-30)
    resource: https://code.claude.com/docs/en/permissions
  - id: claude-code-memory
    title: Manage Claude's memory (Claude Code, Anthropic, accessed 2026-11-30)
    resource: https://code.claude.com/docs/en/memory
  - id: claude-code-settings
    title: Claude Code settings (Anthropic, accessed 2026-11-30)
    resource: https://code.claude.com/docs/en/settings
  - id: claude-code-headless
    title: Run Claude Code programmatically (Anthropic, accessed 2026-11-30)
    resource: https://code.claude.com/docs/en/headless
  - id: claude-code-security
    title: Security (Claude Code, Anthropic, accessed 2026-11-30)
    resource: https://code.claude.com/docs/en/security
  - id: claude-code-sandboxing
    title: Sandboxing (Claude Code, Anthropic, accessed 2026-11-30)
    resource: https://code.claude.com/docs/en/sandboxing
  - id: anthropic-managed-agents-permissions
    title: Permission policies (Claude Managed Agents, beta, Anthropic, accessed 2026-11-30)
    resource: https://platform.claude.com/docs/en/managed-agents/permission-policies
  - id: anthropic-managed-agents-multiagent
    title: Multiagent orchestration (Claude Managed Agents, beta, Anthropic, accessed 2026-11-30)
    resource: https://platform.claude.com/docs/en/managed-agents/multiagent-orchestration
generated:
  at: 2026-11-30T10:59:25Z
  by: esl-rewrite/1.2.0+ksor.1
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

**In everyday life.** A company has offices in three cities. Each office has its own door lock and its own receptionist, set up by whoever runs that office. Move a team from one office to another, and the door they walk through is a different door, set up by someone else. Nobody checks that the new door is as strong as the old one unless someone looks.

**What you will learn.** You practice this in Lab 28, Task 3, and check it in Task 4.

- Which tiers does each place keep?
- How does a guard reach a subagent?
- How does Claude Code decide a call, and where do its hooks live?
- What does the builder's own session need?

**Why it matters.** A guard set in one place does not travel to another. Move a worker from the Agent SDK to a hosted runtime, and its hook is gone without an error ([23.6](../hosted-agents/what-travels.md)). Only the third tier is kept everywhere, because it lives in the connector.

**At work.** The builder works on Lab 28 in Claude Code. The repository keeps the builder's keys in `.secrets/`, such as `.secrets/anthropic.env`, which Claude must never read.

### Which tiers does each place keep?

**You set the first and second tiers yourself, in each place, in that place's own way.** **Managed settings**, in the table, are Claude Code settings that an organization deploys to every computer.

| | Claude Code | Claude Agent SDK | Managed Agents | Your own loop |
| --- | --- | --- | --- | --- |
| Lowers the odds: instructions, tool text as data | `CLAUDE.md` and rules files, read as context | The system prompt and how your tools return text | The agent's instructions and its skills | Your prompt and how you build tool results |
| One route: which tools exist | Permission rules. A bare tool name removes the tool | `tools` and `disallowedTools` in your code | The tools you turn on by name | The tools your code sends |
| One route: a hook before each call | Hooks in settings files, plugins and managed settings | Callbacks in your code. File hooks only if `settingSources` loads them | Not there. No hooks travel | Your own code, between any two calls |
| One route: a person asks | Ask rules and the permission prompt | Ask rules and your permission callback | `always_ask`, which waits for a person | Your own code |
| One route: a sandbox | A sandbox setting, off by default | Its sandbox option, or the container you run it in | The environment's list of allowed hosts | Yours to build |
| Every route: the connector's checks | Kept | Kept | Kept | Kept |

Three cells need a sentence each.

- *Claude Code's sandbox.* "The sandbox is off by default."[^claude-code-sandboxing] A guard that exists only when someone turns it on is "set by you", not "kept".
- *Claude Code's Bash rules.* "A Bash rule matches the command text Claude writes", so a deny rule "isn't a security boundary around the program".[^claude-code-permissions] It is code outside the model, so it decides, but only on the text it matches. `Bash(rm *)` does not stop `bash -c 'rm -rf build/'`. Count it as a narrow guard on one route, never as a boundary.
- *Custom tools on Managed Agents.* A **custom tool** is one your product runs when the hosted session hands it the call ([23.6](../hosted-agents/what-travels.md)). "Custom tools run in your application, under your control, so permission policies don't apply to custom tools."[^anthropic-managed-agents-permissions] Its only guard is your product's code. So when the hosted worker gets Lab 22's `flag_invoice` back, as a custom tool, its handler checks `approvals` itself, as the connector does.

<!-- EDITOR: the spec's raised point 13 recommends bringing `flag_invoice` back to the hosted worker as a custom tool whose handler checks `approvals`. The other option leaves the hosted worker read-only and drops this sentence. The 7:10 triage gets no `schedule_payment` either way, because an `always_ask` call in an unattended session waits indefinitely. -->

<!-- FIGURE 28.4: A grid. Columns, left to right: "Claude Code", "Claude Agent SDK", "Managed Agents", "Your own loop". Rows, top to bottom: "Lowers the odds", "Decides on one route: tools", "Decides on one route: hooks", "Decides on one route: ask a person", "Decides on one route: sandbox", "Subagents", "Decides on every route: the connector". Each cell holds one of three words, with a matching mark: "kept" (filled circle), "set by you" (outlined circle), "not there" (a dash). Lowers the odds: set by you in all four. Tools: set by you in all four. Hooks: set by you, set by you, not there, set by you. Ask a person: set by you in all four. Sandbox: set by you (off by default), set by you, set by you, set by you. Subagents: kept (file, managed and plugin hooks run in subagents), kept (callbacks run, permission rules inherited), set by you (each subagent's own tools), set by you. Connector: kept in all four, and this row is shaded. Alt text must give every cell, and must say that only the connector row reads "kept" in every column. -->

*Figure 28.4. Tiers by place. Only the connector's checks are kept everywhere without your work.*

<!-- EDITOR: Figure 28.4 repeats the table above and adds a "Subagents" row that the table answers in prose below. docs/authoring.md section 7 prefers one of the two. The other option: add the row to the table and drop the figure. -->

### How does a guard reach a subagent?

**In Claude Code and the Agent SDK, the parent's hooks and rules reach it. On Managed Agents, a subagent has the tools its own definition gives it.** This is the question 27.4 left open ([27.4](../agent-architecture/what-a-subagent-gets.md)).

- *Claude Code.* "Hooks from settings files, managed policy settings, and plugins also run inside subagents. When a subagent calls a tool, tool events such as `PreToolUse` and `PostToolUse` fire the same configured hooks as in the main conversation".[^claude-code-hooks]
- *The Agent SDK.* Your callbacks run for a subagent's calls too, and "`agent_id` and `agent_type` are populated when the hook fires inside a subagent", so the evidence line can say which agent proposed the payment. Permission rules are ones "which subagents inherit from the parent conversation".[^claude-agent-sdk-hooks] A main session in `bypassPermissions` passes that mode to its subagents, and the Agent SDK warns that "inheriting `bypassPermissions` grants them full, autonomous system access."[^claude-agent-sdk-permissions] In Lab 28, with `settingSources` empty, `vendor-lookup` gets the orchestrator's callbacks, and none from files.
- *Managed Agents.* A subagent you created "uses its own configuration: model, system prompt, tools, MCP servers, and skills". One defined inline uses the tools of the agent that the session runs.[^anthropic-managed-agents-multiagent] Each needs its own check of which tools it has.

Everywhere, a subagent's call to the connector still carries the run's token, so the connector checks it the same way.

### How does Claude Code decide a call, and where do its hooks live?

**Hooks first, then deny, ask and allow rules. All of them are second-tier guards that you set in the session's files.** Claude Code's rules follow 22.5's idea. "Rules are evaluated in order: deny, then ask, then allow. The first match in that order determines the outcome".[^claude-code-permissions] A hook's `deny` "blocks the tool even in `bypassPermissions` mode or with `--dangerously-skip-permissions`", and a hook's `allow` "doesn't bypass deny rules from settings".[^claude-code-hooks-guide]

One exception has conditions. A **mod**, an installed extension that can answer the permission check, answers `tool.check`, Claude Code's permission check, after the hooks have run. Some setups have no managed settings, no organization `managed-mcp.json` file, and no Team or Enterprise sign-in as the session's credential. In such a setup, "the mod can approve a call that a `PreToolUse` hook blocked, and some calls that a deny rule refuses." A session that runs on an API key or a long-lived `CLAUDE_CODE_OAUTH_TOKEN` "isn't using that login".[^claude-code-permissions]

Each part of a Claude Code hook is a place where it can miss its route. Its event, such as `PreToolUse`, sets when it runs, and its matcher works as in [28.5](hooks-as-deterministic-enforcement.md): "Matchers are case-sensitive".[^claude-code-hooks-guide] It is one of five handler types: command, HTTP, MCP tool, prompt and agent. A command hook that exits 2 blocks, and "The message that comes with the block is your hook's stderr", which Claude reads as the reason. Hooks live in your user settings, the project's `.claude/settings.json` and `.claude/settings.local.json`, managed policy settings, a plugin's `hooks/hooks.json`, and a skill's or subagent's front matter, and "Hook entries merge across settings levels rather than replacing each other".[^claude-code-hooks]

One trap gives no warning. "When your hook returns `permissionDecision` or `additionalContext` at the top level instead of inside `hookSpecificOutput`, the JSON still parses, and Claude Code ignores the misplaced fields without reporting an error."[^claude-code-hooks-guide] A deny in the wrong place is no deny at all.

### What does the builder's own session need?

**A deny rule for the secrets, the hook where the orchestrator loads it, and a known credential.** The builder's plan:

1. **A deny rule for `.secrets/`.** In `.claude/settings.json`, the rule `Read(./.secrets/**)` under `deny` blocks Claude's file tools from reading the key files.[^claude-code-permissions] It covers the tools, not every program: read and edit rules "don't apply to arbitrary subprocesses that read or write files indirectly, like a Python or Node script that opens files itself."[^claude-code-permissions]
2. **The payment hook in code, not in this file.** The orchestrator does not read `.claude/settings.json` ([28.5](hooks-as-deterministic-enforcement.md)). A hook written here guards the builder's own session, and nothing else.
3. **The mode, named.** "Auto mode is the built-in starting permission mode for interactive terminal and VS Code sessions."[^claude-code-security] It "delegates command approvals to a model-based classifier",[^anthropic-how-we-contain-claude] a guard that lowers the odds, never a person. The builder checks which mode the session started in.
4. **The credential, named.** The builder checks which credential the session runs on. On an API key or a long-lived `CLAUDE_CODE_OAUTH_TOKEN`, the mod exception above applies, so the builder installs no mod that answers `tool.check`.

**The operating rule:** before you trust a guard, name the place it lives in and whether the call's route passes through that place. Put the check that must hold everywhere in the connector, and let each place's own guards stop the model early.

**Do this now.** Ask your coding agent to fill the table for your own worker. Copy this line:

> Fill one table for the worker in this project. Columns: where it runs today, and every other place it could move to. Rows: instructions, the tool list, hooks, asking a person, a sandbox, subagents, and the connector's own checks. In each cell, write where that guard is set and the file and line, or "not there". Change no file.

Every "not there" in a column you plan to move to is a guard you lose on the move.

### For the Architect exam

*This part goes past the developer exam, into topics the Claude Certified Architect exam lists. The section above stands without it.*

**Rules that load by path.** "Rules can be scoped to specific files using YAML frontmatter with the `paths` field", for example `paths:` followed by `- "src/api/**/*.ts"`. "A path-scoped rule loads when Claude uses the Read, Write, or Edit tool on a matching file."[^claude-code-memory] It is still context, and enforces nothing ([28.5](hooks-as-deterministic-enforcement.md)).

**Which settings win.** "Settings precedence, highest first: managed settings, command line, project local, shared project, user." Managed settings (above) are highest, and "Nothing in your own settings files or `--settings` overrides a managed key".[^claude-code-settings]

Lists add up instead: "When you set the same list key, such as `permissions.allow`, in more than one file, Claude Code combines the lists".[^claude-code-settings] A deny cannot be undone lower down: "If a tool is denied at any level, no other level can allow it."[^claude-code-permissions]

An organization can go further. `allowManagedPermissionRulesOnly` "makes managed settings the only settings source of permission rules".[^claude-code-permissions] And `allowManagedHooksOnly` blocks "Your user, project, local, and plugin hooks".[^claude-code-hooks]

**Permissions for a run in CI.** The book already runs `claude -p` in a pipeline ([12.7](../agentic-coding/outside-the-app.md)). It starts in Manual mode "when nothing sets a permission mode", and "a call that needs approval is denied unless something approves it". `--allowedTools` approves named tools, and "The space before `*` is important: without it, `Bash(git diff*)` would also match `git diff-index`." And `dontAsk` mode "denies every call that would otherwise prompt, which is useful for locked-down CI runs, though a mod you installed can still approve some of those calls."[^claude-code-headless]

One CI risk is easy to miss. Hooks in a repository's settings files run under `claude -p` even when nobody ever trusted the folder, so a pull request that adds `.claude/settings.json` can add a hook that runs on your CI machine. To turn hooks off for one run, pass `--settings '{"disableAllHooks": true}'`, because "Setting it in your user settings alone isn't enough".[^claude-code-permissions] Managed hooks still run: `disableAllHooks` "can't disable managed hooks from outside managed settings."[^claude-code-hooks]

[^claude-code-sandboxing]: Sandboxing, Claude Code, Anthropic.
[^claude-code-permissions]: Configure permissions, Claude Code, Anthropic.
[^anthropic-managed-agents-permissions]: Permission policies, Claude Managed Agents, Anthropic.
[^claude-code-hooks]: Hooks reference, Claude Code, Anthropic.
[^claude-agent-sdk-hooks]: Control execution with hooks, Claude Agent SDK, Anthropic.
[^claude-agent-sdk-permissions]: Configure permissions, Claude Agent SDK, Anthropic.
[^anthropic-managed-agents-multiagent]: Multiagent orchestration, Claude Managed Agents, Anthropic.
[^claude-code-hooks-guide]: Automate workflows with hooks, Anthropic.
[^claude-code-security]: Security, Claude Code, Anthropic.
[^anthropic-how-we-contain-claude]: How we contain Claude across products, Anthropic.
[^claude-code-memory]: Manage Claude's memory, Claude Code, Anthropic.
[^claude-code-settings]: Claude Code settings, Anthropic.
[^claude-code-headless]: Run Claude Code programmatically, Anthropic.
