Getting started¶
Who this page is for: a Python developer who has never used Castellan. After reading it you will have installed Castellan, written a policy, checked calls from the command line, authorized calls from Python, and delegated work to a sub-agent whose power is capped.
Every command and snippet below was run, and the output shown is what it printed. Random ids and timestamps will differ on your machine.
1. Install¶
Castellan needs Python 3.11 or newer.
Check that the command line tool works:
Usage: castellan [OPTIONS] COMMAND [ARGS]...
Least-privilege permissions for multi-agent AI systems.
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --help <bool> Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────╮
│ check Dry-run one tool call against a policy and print the decision. │
│ explain Show every rule considered for a call and why it matched or not. │
│ test Run policy test files (YAML with a policy path and cases) and │
│ report pass/fail. │
│ lint Find shadowed, duplicate, over-broad and unreachable rules │
│ (checks L001-L007). │
│ mcp-proxy Run an MCP server behind a policy-enforcing stdio proxy: │
│ castellan mcp-proxy --policy team.yaml --role coder -- <server │
│ command...> │
│ audit Verify and tail audit logs. │
│ approve List, accept and reject approvals. │
╰──────────────────────────────────────────────────────────────────────────────╯
Extras
The MCP proxy needs the mcp extra (pip install "agent-castellan[mcp]") and the LangGraph wrapper needs [langgraph]. Neither is needed on this page.
2. Write your first policy¶
Save this as first-policy.yaml. It has two roles and three rules.
version: 1
defaults:
effect: deny # anything no rule mentions is refused
agents:
planner:
can_delegate_to: [coder]
coder: {}
rules:
- id: no-secrets
description: Secret files are off limits to everyone
tools: ["*"]
when:
any:
- args.path: { glob: "**/.env*" }
- args.path: { glob: "**/*.pem" }
effect: deny
reason: Secret files are off limits
- id: read-workspace
agents: [planner, coder]
tools: ["fs.read", "fs.list"]
when:
args.path: { glob: "/workspace/**" }
effect: allow
- id: write-needs-approval
agents: [coder]
tools: ["fs.write"]
when:
args.path: { glob: "/workspace/**" }
effect: ask
approver: human
reason: Writes need a human OK
audit:
path: ./castellan-audit.jsonl
Read it top to bottom:
defaults.effect: denymeans a call that no rule mentions is refused. This is the safe default.no-secretsdenies any tool whosepathargument looks like a secret file. Because deny always wins, no other rule can undo it.read-workspacelets theplannerandcoderroles read inside/workspace.write-needs-approvalmakes everycoderwrite wait for a human.
3. Check calls from the command line¶
castellan check asks the policy about one call without running anything. The exit code is the answer: 0 allow, 1 deny, 2 ask.
castellan check first-policy.yaml --role coder --tool fs.write --args '{"path":"/workspace/app.py","content":"x"}'
A tool that no rule mentions falls to the default:
Castellan cleans up paths before matching, so .. tricks do not work. This read tries to climb out of /workspace, and no rule allows the cleaned path:
castellan check first-policy.yaml --role coder --tool fs.read --args '{"path":"/workspace/../etc/passwd"}'
4. Authorize calls from Python¶
In a real agent you call the guard instead of the CLI. The guard runs the whole pipeline: policy, delegation grants, approvals, budgets and the audit log. This script does everything in the rest of this page:
import secrets
from agent_castellan import Guard, ToolCall
# One key per deployment. Grants signed with it only verify with the same key.
guard = Guard.from_file("first-policy.yaml", key=secrets.token_bytes(32))
# --- A root agent: the planner starts a run on behalf of a user -------------
planner = guard.start_run("planner", on_behalf_of="alice")
print("principal:", planner.role, "depth", planner.depth, "run", planner.run_id[:8] + "...")
# --- Allow ------------------------------------------------------------------
read = ToolCall(tool="fs.read", args={"path": "/workspace/app.py"})
decision = guard.authorize(planner, read)
print(decision.effect.value, decision.rule_id, "-", decision.reason)
# --- Deny -------------------------------------------------------------------
secret = ToolCall(tool="fs.read", args={"path": "/workspace/.env"})
decision = guard.authorize(planner, secret)
print(decision.effect.value, decision.rule_id, "-", decision.reason)
# --- Delegation: the coder gets a grant that can only read ------------------
coder, grant = guard.delegate(planner, "coder", tools=["fs.read"], ttl_seconds=600)
print("coder depth:", coder.depth, "| grant tools:", list(grant.tools))
write = ToolCall(tool="fs.write", args={"path": "/workspace/app.py", "content": "print('hi')"})
decision = guard.authorize(coder, write)
print(decision.effect.value, decision.source, "-", decision.reason)
# --- Ask: a coder with a wider grant must get approval ----------------------
coder2, _ = guard.delegate(planner, "coder", tools=["fs.*"], ttl_seconds=600)
decision = guard.authorize(coder2, write)
print(decision.effect.value, decision.rule_id, "approval_id set:", decision.approval_id is not None)
status = guard.approve(decision.approval_id, approver_id="alice")
print("approval status:", status.value)
decision = guard.authorize(coder2, write) # retry the exact same call
print(decision.effect.value, decision.source, "-", decision.reason)
decision = guard.authorize(coder2, write) # approvals are single-use
print(decision.effect.value, "(a second retry asks again)")
Run it from the folder that holds first-policy.yaml:
principal: planner depth 0 run run-0d96...
allow read-workspace - rule 'read-workspace' matched
deny no-secrets - Secret files are off limits
coder depth: 1 | grant tools: ['fs.read']
deny grant - tool 'fs.write' is not covered by grant 0 (grant-9fba4e4c663090fb)
ask write-needs-approval approval_id set: True
approval status: approved
allow approval - approved request for rule 'write-needs-approval'
ask (a second retry asks again)
Here is what each part showed:
- Allow and deny.
guard.authorizereturned aDecision. Itseffect,rule_idandreasonsay what happened and why.start_run("planner", on_behalf_of="alice")created the root agent of a new run. - Delegation.
guard.delegatecreated a childcoderand a grant that lets it use onlyfs.read. When the coder triedfs.write, the policy saidask, but the grant said no, and the result was adenyfrom sourcegrant. A grant can only turn an allow or ask into a deny, never the other way. - Approval. A second coder with a wider grant (
fs.*) hit theaskrule. The decision carried anapproval_id. Afterguard.approve(...), retrying the same call succeeded with sourceapproval. - Single use. An approval unlocks exactly one call. The next identical call asked again.
Keep the root key stable
Guard.from_file(..., key=...) signs grants with a key. The script uses a random one, which is fine inside one process. To pass grants between processes, give every process the same key (at least 32 bytes), either with key= or the hex-encoded CASTELLAN_KEY environment variable. Without either, Castellan generates a random key and emits a RuntimeWarning.
5. Look at the audit log¶
authorize wrote six entries to castellan-audit.jsonl, each carrying the hash of the one before it. Verify the chain:
Pitfalls¶
checkis a dry run. It never creates approvals or counts budgets. UseGuard.authorizein your agents.- Roles are strings. A typo such as
coderin a rule silently matches nothing. Runcastellan lint(see testing and linting). askneeds a retry. Your tool wrapper must catch theask, wait for the approval, and call again.
Where to go next¶
- Concepts: principals, grants, runs and the exact decision algorithm.
- Writing policies: grow this policy into a multi-agent one.
- Multi-agent delegation and Approvals: the two features used in step 4, in depth.