Skip to main content

Castellan

CI PyPI Python versions License: Apache-2.0

A permission layer for multi-agent AI systems: you write YAML rules that allow, deny or require approval for every tool call, and when one agent hands work to another, the second agent can never get more power than the first.

Castellan (noun): the keeper of a castle, who decides who may enter and what they may do.

Who this is for: developers building agents that call tools (files, shell, databases, web, payments, GitHub) and who need to say, in a reviewable file, what each agent may do. After this page you will know what Castellan does, have run it, and know where to go next.

Why

One agent with a few tools is easy to reason about. Real systems are now teams: a planner hands work to a researcher, which hands work to a browser agent. That creates problems a tool-call allow-list does not solve:

  • A sub-agent can end up with more power than the agent that created it. Nothing stops a planner that may only read from spawning a helper that can write.
  • One runaway sub-agent can burn the whole budget of calls for everyone.
  • "Ask a human" does not scale, and an approval for one call must not be reusable for another.
  • Afterwards, nobody can tell which agent did what, on whose behalf, or whether the log was edited.
  • Policies grow until nobody trusts them. There is no way to test them or to see why a call was refused.

Castellan treats each of these as a first-class feature, not an afterthought.

flowchart TD
    U["user (alice)"] --> P["planner<br/>tools: fs.read, web.*, fs.write"]
    P -- "delegate: web.search only" --> R["researcher<br/>tools: web.search"]
    P -- "delegate: fs.write in /workspace/drafts" --> W["writer<br/>tools: fs.write (drafts)"]
    R -. "asks for fs.write" .-> X(["denied: not in its grant"])
    W -. "asks for web.search" .-> Y(["denied: not in its grant"])
    R -- "web.search" --> G{{"Guard.authorize<br/>budget, policy, grants,<br/>approval, audit"}}
    W -- "fs.write" --> G

A grant can only shrink what its parent holds. It is signed with a chained HMAC, so it cannot be widened even by code that holds it.

Quickstart

Install (Python 3.11+). Until 0.1.0 is on PyPI, install from the repository:

pip install "git+https://github.com/KachhadiyaHiren/agent-castellan.git"   # or: pip install agent-castellan

Write a policy, policy.yaml:

version: 1
defaults:
  effect: deny                      # anything not allowed below is refused
rules:
  - id: read-workspace
    tools: ["fs.read"]
    when:
      args.path: { glob: "/workspace/**" }
    effect: allow
  - id: write-needs-approval
    tools: ["fs.write"]
    effect: ask                     # a human must approve each exact call

Ask the policy about three calls, demo.py:

from agent_castellan import Guard, ToolCall

guard = Guard.from_file("policy.yaml", key=b"k" * 32)  # use a real secret, see Security below
agent = guard.start_run("coder", on_behalf_of="alice")

for tool, args in [
    ("fs.read", {"path": "/workspace/app.py"}),
    ("fs.read", {"path": "/etc/passwd"}),
    ("fs.write", {"path": "/workspace/app.py", "content": "x = 1"}),
]:
    d = guard.authorize(agent, ToolCall(tool=tool, args=args))
    print(f"{tool:9} -> {d.effect.value:5} {d.rule_id} {d.reason}")
$ python demo.py
fs.read   -> allow read-workspace rule 'read-workspace' matched
fs.read   -> deny  None no rule matched; default is deny
fs.write  -> ask   write-needs-approval rule 'write-needs-approval' matched

The same questions from the command line, without writing any Python. castellan check exits 0 for allow, 1 for deny, 2 for ask:

$ castellan explain policy.yaml --role coder --tool fs.write --args '{"path":"/workspace/app.py"}'
ASK rule=write-needs-approval source=policy reason=rule 'write-needs-approval' matched
                            Rule trace
┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Rule                 ┃ Matched ┃ Detail                         ┃
┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ read-workspace       │ no      │ tools: no match for 'fs.write' │
│ write-needs-approval │ yes     │ matched                        │
└──────────────────────┴─────────┴────────────────────────────────┘

demo.py also wrote castellan-audit.jsonl, a hash-chained log of every decision; castellan audit verify castellan-audit.jsonl checks that nobody edited it.

A multi-agent example

The planner delegates to a researcher with a grant for one tool. The policy (examples/policies/research-team.yaml) would also let the researcher use web.fetch, but the grant is narrower, and the narrower side wins.

from agent_castellan import Guard, PermissionDenied, ToolCall

guard = Guard.from_file("research-team.yaml", key=b"k" * 32)
planner = guard.start_run("planner", on_behalf_of="alice")

# The planner hands the researcher a narrow grant: one tool, ten minutes.
researcher, grant = guard.delegate(planner, "researcher", tools=["web.search"], ttl_seconds=600)


def attempt(who, tool, **args):
    d = guard.authorize(who, ToolCall(tool=tool, args=args))
    print(f"{who.role:10} {tool:10} -> {d.effect.value:5} ({d.source}: {d.reason})")


attempt(researcher, "web.search", query="macaroons")  # inside the grant
attempt(researcher, "web.fetch", url="https://example.com")  # policy allows, grant does not

try:  # a researcher cannot spawn helpers
    guard.delegate(researcher, "writer", tools=["fs.write"])
except PermissionDenied as exc:
    print("delegate    ->", exc.decision.reason)
researcher web.search -> allow (policy: rule 'researcher-web' matched)
researcher web.fetch  -> deny  (grant: tool 'web.fetch' is not covered by grant 0 (grant-bfe373cb376bb9df))
delegate    -> role 'researcher' may not delegate to 'writer' at depth 1

The complete, runnable team is in examples/multi_agent_team/.

Features

Feature What you get
YAML policy language allow, deny and ask rules over tool name globs, agent roles and a data-only condition DSL (glob, regex, in, lt/gte, exists, all/any/not, ...). Nothing is ever passed to eval. Deny always overrides.
Delegation that only shrinks Guard.delegate mints a signed grant for a child. Chained HMAC signatures make widening impossible; grants expire and can carry extra conditions.
Approvals ask rules route to a human, a role or a named agent. Approvals are bound to the exact call (tool plus canonical arguments), single use, and expire. An agent never approves its own request.
Run-tree budgets Call and cost limits shared by the whole agent tree, plus per-role limits.
Tamper-evident audit log Append-only JSONL where each entry carries the hash of the previous one; castellan audit verify detects edits and deletions.
Policy tooling castellan test (unit tests for policies, JUnit output), castellan explain (why a call got its decision), castellan lint (shadowed rules, duplicate ids, undefined roles, over-broad allows).
Input normalization Paths, URLs and tool names are normalized before matching, so /workspace/../etc/passwd and FS.Read do not slip past a rule.
MCP proxy castellan mcp-proxy sits between any MCP client and any MCP server and enforces the policy on tools/call, with no change to the agent.
Python integrations @guarded decorator for plain functions and guard_tools for LangGraph.

Integrations in one minute

# Put any MCP server behind a policy (needs the `mcp` extra: pip install "agent-castellan[mcp]")
castellan mcp-proxy --policy team.yaml --role coder -- npx -y @modelcontextprotocol/server-filesystem /workspace
from agent_castellan import Guard
from agent_castellan.integrations.decorator import guard_context, guarded

guard = Guard.from_file("policy.yaml", key=b"k" * 32)


@guarded(guard, tool="fs.read")
def read_file(path: str) -> str: ...


with guard_context(guard.start_run("coder")):
    read_file(
        path="/workspace/app.py"
    )  # raises PermissionDenied or ApprovalRequired if not allowed

How this compares

Several small projects already gate tool calls with declarative rules. They do that job well, and if you have a single agent with a flat list of tools one of them may be all you need. Castellan is for the case where agents create other agents. The descriptions below are deliberately general, because these projects move quickly; read their own pages for the current state.

Project What it does well What Castellan adds
agent-action-gate Checks an agent action against configured rules before it runs. Delegation between agents, approvals bound to the exact call, a tamper-evident audit log.
toolpolicy Declarative allow/deny policy over tool calls. The same, plus grants that shrink down the agent tree, run budgets, and test/lint/explain tooling.
agent-policy / guard A policy file evaluated against tool calls, with Python, TypeScript and Go implementations. Identity per agent (role, depth, parent, on behalf of), so rules can say "only when started by the planner".
Cohorte guardrails A declarative engine over agent inputs, outputs, tool calls and approvals, part of a wider agent stack. A narrower focus on authorization of tool calls, with cryptographically chained delegation; the two are complementary.
agent-authz Authorization checks at the moment an agent action executes. Multi-agent delegation, run-tree budgets and an MCP proxy that works without changing the agent.

What these tools generally treat as one flat identity is the gap: a call is allowed or denied for "the agent", with no notion that the agent was created by another agent that holds less power. That is what Castellan models. It does not replace content guardrails (toxicity, prompt filters) and does not sandbox your tools; see the security model in the docs for exactly what it protects against and what it does not.

Security notes

  • Use a real root key. The examples pass key=b"k" * 32 so they run anywhere. In production set CASTELLAN_KEY (hex, at least 32 bytes) or pass key=; without one, Castellan generates a random key per process and warns, so grants will not verify in other processes.
  • Castellan decides; your code must obey. A process that never calls the guard, or a tool that ignores the decision, is outside its protection. The MCP proxy closes that gap for MCP tools.
  • Arguments chosen by a model are untrusted. A rule on args.env is only as strong as the code that sets env.

Documentation

  • Docs site: getting started, concepts, the policy language reference, delegation, approvals, budgets, audit, integrations, CLI reference, security model and FAQ.
  • Policy cookbook: ten tested policies (research, coding sandbox, payments, GitHub, SELECT-only database, support, office hours, two-person rule, staging vs prod, multi-agent team).
  • Multi-agent example: a runnable team.
  • Design plan: the specification this project was built from.

Roadmap

Planned after 0.1.0 (nothing here is promised for a date):

  • More integrations: CrewAI, AutoGen, PydanticAI, OpenAI Agents SDK.
  • HTTP sidecar mode (castellan serve) for non-Python agents, and a TypeScript client.
  • Approval channels: webhook, Slack, email.
  • Argument redaction in the audit log (allow plus redact: [args.token]).
  • Signed, versioned policy bundles and hot reload.
  • OpenTelemetry spans for decisions.

Contributing

Issues and pull requests are welcome. CONTRIBUTING.md explains setup, the code layout and how to add an operator, a lint rule or an integration step by step. Please follow the Code of Conduct. Report security problems privately as described in SECURITY.md.

License

Apache-2.0.

Metadata

Release files for agent-castellan 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agent-castellan 0.1.0
File Size Uploaded Provenance Actions
agent_castellan-0.1.0.tar.gz 412.4 kB With provenance Details for agent_castellan-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agent-castellan 0.1.0
File Interpreter ABI Platform Actions
agent_castellan-0.1.0-py3-none-any.whl Python 3 none any Details for agent_castellan-0.1.0-py3-none-any.whl

Total release size: 486.7 kB

Release files / agent_castellan-0.1.0.tar.gz

Download URL agent_castellan-0.1.0.tar.gz
Size 412.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2bd71d7bf315a0edbe3f0e8150a1b06c9d3ec5cf94ca93184f14c4ad1ce03777
BLAKE2b-256 checksum
How to use checksums
f595a124e69fe76ed03c89201d2a360559d8506a75baa3a62a97d0af88451b10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Release files / agent_castellan-0.1.0-py3-none-any.whl

Download URL agent_castellan-0.1.0-py3-none-any.whl
Size 74.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1f21a0cfbcd875add89cd2b6423661dc6d284a40c5320ab179ba260ea37067d1
BLAKE2b-256 checksum
How to use checksums
469c807cf78dd11db7415151c44ab7bbd8c7100fe153da0d42591386b3080264
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Security for agent-castellan 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for agent_castellan-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page