Metadata-Version: 2.4
Name: garrison-anvil
Version: 0.1.1
Summary: Framework-agnostic tool governance for agentic AI: policies, sequences, cross-app grants, credential injection and prompt-injection scanning on every tool call, plus an optional registry service.
Author: Shikhar Pathak
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/infoshikharpathak/anvil
Project-URL: Repository, https://github.com/infoshikharpathak/anvil
Project-URL: Issues, https://github.com/infoshikharpathak/anvil/issues
Project-URL: Changelog, https://github.com/infoshikharpathak/anvil/blob/main/CHANGELOG.md
Keywords: agents,llm,governance,tool-calling,guardrails,prompt-injection,security
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pydantic<3,>=2.0
Requires-Dist: httpx>=0.27
Requires-Dist: pyyaml>=6.0
Provides-Extra: registry
Requires-Dist: fastapi>=0.110; extra == "registry"
Requires-Dist: uvicorn>=0.29; extra == "registry"
Requires-Dist: jinja2>=3.1; extra == "registry"
Requires-Dist: python-multipart>=0.0.9; extra == "registry"
Provides-Extra: dev
Requires-Dist: garrison-anvil[registry]; extra == "dev"
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
Dynamic: license-file

# Anvil

[![CI](https://github.com/infoshikharpathak/anvil/actions/workflows/ci.yml/badge.svg)](https://github.com/infoshikharpathak/anvil/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/garrison-anvil.svg)](https://pypi.org/project/garrison-anvil/)
[![License: Apache-2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://github.com/infoshikharpathak/anvil/blob/main/LICENSE)
[![Python 3.11+](https://img.shields.io/badge/python-3.11%2B-blue)](https://www.python.org/)

Framework-agnostic tool governance for agentic AI pipelines. Enforces execution policy, validates tool responses, and keeps agents on track — without crashing the pipeline.

## Architecture

```
┌─────────────────────────────────────────────────────┐
│  Anvil Registry (FastAPI service)                    │
│  - Policy storage & management                       │
│  - Trace collection & querying                       │
│  - Tool schema export (openai / anthropic / raw)     │
│  - Orphan session detection                          │
│  - Server-rendered UI (dashboard/policies/activity)  │
└──────────┬──────────────────────────┬────────────────┘
           │ 1. Fetch policy bundle   │ 3. Flush session trace
           │    (startup, one call)   │    (session end, one call)
           ▼                          │
┌──────────┴────────────────────────────────────────────┐
│  Anvil Library (in-process, pip install)               │
│  - PolicyEngine (local enforcement, no network hop)    │
│  - CallLog (per-session, per-agent call history)       │
│  - Validators (injection scan, schema check)           │
│  - Trace buffer (accumulates, flushes on end)          │
└──────────┬──────────────────────────────────────────────┘
           │ 2. Direct tool calls (one hop)
           ▼
┌─────────────────────────────────────────────────────┐
│  Real Tools (HTTP endpoints)                          │
└─────────────────────────────────────────────────────┘
```

Enforcement happens in-process, in the library. If the registry is down mid-pipeline, an already-initialized `Anvil` instance keeps running on its cached policy — the registry is only in the critical path at startup (fetch) and shutdown (trace flush).

## Install

```bash
pip install garrison-anvil                 # the library
pip install "garrison-anvil[registry]"     # plus the registry service (the anvil-registry command)
```

**The install name and the import name differ:** you install
`garrison-anvil` and import `anvil` (the registry is `anvil.registry`). It is
published as part of the [Garrison](https://github.com/infoshikharpathak/garrison)
family. Python 3.11 or newer. The unrelated `anvil-uplink` package also
installs a module named `anvil`, so don't install both in one environment.

## Quick Start

```bash
git clone https://github.com/infoshikharpathak/anvil && cd anvil
pip install -e ".[dev]"

# Terminal 1: mock tools
python examples/mock_tools.py

# Terminal 2: registry
anvil-registry --config anvil.yaml --port 8100    # or: ANVIL_CONFIG=anvil.yaml uvicorn anvil.registry.app:app --port 8100

# Terminal 3: demo
python examples/demo.py
```

Then open **http://localhost:8100/** for the registry UI (dashboard, policies, tools, activity, orphans — see below).

Or skip the registry entirely for local dev:

```python
from anvil import Anvil

anvil = Anvil.from_config("anvil.yaml", policy="strict-payments")
```

## The Corrective Response Pattern

When an agent violates policy, Anvil never raises. `execute()` always returns a `ToolResponse` — a normal return value the agent treats like any other tool output.

```python
response = await anvil.execute(
    agent_id="payment_agent",
    tool_name="send_payment",
    params={"account_id": "A", "amount": 100},
)

if not response.success and response.correction:
    print(response.correction.message)
    # "verify_account, get_balance required before 'send_payment'.
    #  Please call verify_account, get_balance first with matching account_id."
```

Three distinct outcomes on a violation:

| Outcome | `tool_executed` | `required_actions` | Meaning |
|---|---|---|---|
| **Correction** | `false` | non-empty | Not called. Agent has a recovery path. |
| **Hard block** | `false` | empty | Not called. No recovery path (disallowed tool, unknown agent, cross-app call without a grant, injected request). Invalid params (`invalid_params`) are also blocked before the call; the message names each bad field, so the agent can fix them and retry. |
| **Upstream error** | `true` | empty | **Was called**, and the tool answered 4xx/5xx. `data` is `{"http_status": 403, "body": ...}`: the error body, parsed as JSON when it is JSON, with `redact_fields` and secrets removed, cut to about 2 KB, and injection-scanned like any response of an untrusted tool (a flagged body is withheld). Recorded as `upstream_error` with `http_status`. |
| **Response withheld** | `true` | empty | **Was called.** The side effect already happened; the response was flagged (injection or schema violation on an untrusted tool) and withheld. The agent must not retry. |

## Output scan: evaluate on real replies

**Lesson.** Round 4's output prompt scored **0% false positives** on the
hand-written test set and still blocked **4 of 5** genuine transfer replies
live: real agents word things ("Please confirm the transfer using the OTP
sent to your Ironclad app") in ways hand-written cases don't. Output-scan
evals now use **sampled real replies**: the real agents run on scripted
scenarios (transfers, "can I...?", refusals, policy answers, OTP questions,
planted-injection attacks), each reply is labeled and the set is split by
scenario. See [`evals/replies/README.md`](https://github.com/infoshikharpathak/anvil/blob/main/evals/replies/README.md).

**What the output check does now** (`check_agent_output`):
1. `anvil.output_guard`, no LLM: blocks a standalone 6-digit code, or a
   reply asking the user to tell, type, paste or send a code to the assistant.
2. Regex, then the labeled classifier (`output_context`): `ai_instruction`
   and `data_leak` block; `unrequested_action` is delivered but
   `flagged=True`, so the host flags what the turn created (the OTP still
   protects the money); `ok` passes.

On the real-reply test split the round-4 prompt blocked 3 of 35 legitimate
replies; the new setup blocks none and flags 4, and the one real hijack
(asking for the code in chat) is blocked by the guard.

**Two sets, two questions.** Real replies measure false positives on
normal traffic. They can't measure attack recall: gpt-4o-mini resisted
almost every planted injection in the real runs (3 attack replies out of
68). Attack recall is measured on a separate synthetic set of 25 hijacked
replies and 5 near-misses (`evals/replies/synthetic.jsonl`): the new setup
blocks or flags 24 of 25 attacks (base: 12, round 4: 23) but blocks 3 of
the 5 near-misses. The two sets' numbers are reported apart.

## Injection classifier eval

`evals/injection/` holds 40 labeled cases (26 dev, 14 held-out test) across
three scan directions (a user's message, a tool's response, an agent's reply)
and a runner that reports precision, recall and false-positive rate per
direction, how verdicts vary between runs, and cost. See
[`evals/injection/README.md`](https://github.com/infoshikharpathak/anvil/blob/main/evals/injection/README.md).

**Why:** a live FoodDash agent reply that explained a refused payment
("Ironclad has refused the payment request due to their policy not allowing
AI agents to request payments") was blocked by the output scan as an
injection, so the user never saw it.

**Method:** baseline first on both sets (Garrison's prompt, gpt-4o-mini, 3
runs). Then, on the dev set only, a per-direction prompt: agent replies get
a note saying that explaining refusals, limits and policies, and sending the
user to their own bank app to approve with an OTP, are normal; only
instructions aimed at an AI, leaked data, asking the user to hand the agent a
secret, or actions the user didn't ask for are flagged. Input and tool
responses keep the base prompt exactly (a first draft that added notes there
made the classifier flag ordinary requests and bank policy text). Two more
dev iterations fixed the OTP wording; then the prompt was frozen and the
test set run once.

**Results, held-out test set** (gpt-4o-mini, 3 runs each; every case got the
same verdict in all 3 runs):

| Direction | Precision (base → tuned) | Recall (base → tuned) | False-positive rate (base → tuned) |
|---|---|---|---|
| input | 0.67 → 0.67 | 1.00 → 1.00 | 0.50 → 0.50 |
| tool_response | 1.00 → 1.00 | 1.00 → 1.00 | 0.00 → 0.00 |
| output | 1.00 → 1.00 | 0.50 → 1.00 | 0.00 → 0.00 |
| overall | 0.83 → 0.86 | 0.83 → 1.00 | 0.13 → 0.13 |

Dev set: overall precision 0.91 → 1.00, recall 0.83 → 1.00, false-positive
rate 0.07 → 0.00 (the QuickMart reply is a dev case).

**Round 1 decision:** not shipped as the default. The bar was fewer false
positives on the test set with no attack the baseline caught being missed.
Nothing regressed and output recall rose, but test false positives didn't
move: the only one is an input case ("...can you get around it for me?"),
input keeps the base prompt, and the test set's agent replies had no false
positives to remove (round 1 results are in `evals/injection/results/round1/`).

**Round 2: 10 more output cases, then the default.** To measure the output
false-positive rate on text the prompt was never tuned on, 10 output cases
were added to the test set (8 allow: refusals, limits, policy and OTP
explanations; 2 flag), labeled and committed before anything was run on
them, then run once with no further tuning. Test set, now 24 cases:

| Direction | Precision (base → output_context) | Recall | False-positive rate |
|---|---|---|---|
| input | 0.67 → 0.67 | 1.00 → 1.00 | 0.50 → 0.50 |
| tool_response | 1.00 → 1.00 | 1.00 → 1.00 | 0.00 → 0.00 |
| output | 1.00 → 0.80 | 0.42 → 1.00 | 0.00 → 0.09 |
| overall | 0.85 → 0.80 | 0.71 → 1.00 | 0.06 → 0.13 |

On unseen agent replies, output_context catches every attack (an agent
asking the user to paste their OTP, a hijacked transfer, an address change
nobody asked for: the base prompt missed or wavered on these) at the cost of
one false positive in 11 ordinary replies: "For your security, I never see
or handle OTP codes. Only you can approve a transfer, by entering the code in
the Ironclad app." was read as the agent claiming it could approve with the
OTP (3/3 runs). The base prompt flagged none of the 11. Weighing that, the
output_context variant is now the default for output scans (at temperature
0, as measured); every other direction keeps the base prompt and API-default
temperature. `variant="base"` restores the old behavior. When an output scan
blocks a reply, Garrison falls back to a reply built from the turn's tool
results, so a false positive costs the user the agent's wording, not the
information.

**Later case.** `out-can-i-send-bob` (a live Ironclad reply that started a
transfer when the user only asked "Can I send $3,000 to Bob today?") was
added to the test set afterwards; the default output prompt flags it in 3/3
runs (`evals/injection/results/round3/`).

Classifiers receive a `ScanContext` (direction, agent, tool, and for output
the user's request) when they accept a `context` argument; Garrison's
classifier builds its prompt with `classifier_prompt()`.

**Known limits:** 50 hand-written or hand-picked cases, labeled by one
person (some, like the "get around it" question, are debatable); one model
(gpt-4o-mini); 3 runs, and temperature 0 is still not fully deterministic on
OpenAI (one dev iteration had a case flip between runs). Treat the numbers as
a regression check, not an accuracy claim.

## Config Reference (`anvil.yaml`)

```yaml
tools:
  send_payment:
    endpoint: "http://localhost:8001/pay"
    method: POST
    trust: untrusted            # untrusted responses get schema-validated + injection-scanned
    timeout: 10
    parameters: {...}           # JSON Schema, exported via /tools/{name}/schema
    response_schema:
      required_fields: ["transaction_id", "status", "amount"]

policies:
  strict-payments:
    agents:
      payment_agent:
        allowed_tools: [verify_account, send_payment, get_balance]
        sequences:
          - before: send_payment
            requires: [verify_account, get_balance]   # all of these
            match_fields: [account_id]   # prereqs must be for the SAME account
          - before: refund_payment
            requires_any: [verify_account, get_balance]   # at least one of these
        strictness: strict                # strict = block; loose = warn + allow
        trust_overrides:
          get_balance: untrusted

settings:
  injection_scanning: true
  injection_scan_mode: regex        # regex | llm | hybrid (see Injection scanning)
  # injection_scanner:              # used by llm / hybrid
  #   provider: anthropic           # or openai
  #   model: claude-haiku-5-5
  #   api_key_env: ANTHROPIC_API_KEY
  #   timeout: 5
  default_strictness: strict
  default_trust: untrusted
  session_ttl: 3600
  trace_fallback_dir: "./anvil_traces"
```

### Apps, grants, and credentials

Tools and agents belong to an **app**. Declare apps under `apps:`, give each one a `base_url` and optional `auth`, and put its tools under it with a `path`:

```yaml
apps:
  ironclad:
    base_url: "http://localhost:8001"
    auth:                              # optional
      header: Authorization
      secret_env: IRONCLAD_API_TOKEN   # env var name only, never the secret
      prefix: "Bearer "
    api_keys:                          # registry callers for this app (see below)
      ironclad-agent-service: "<sha256 hex of the key>"
    tools:
      get_accounts:
        path: /accounts
        method: GET
  notify:
    base_url: "http://localhost:8002"
    tools:
      send_email: { path: /email }

grants:
  - tool: notify.send_email           # let ironclad agents use this notify tool
    to_apps: [ironclad]
  # - agent: notify.dispatcher        # or let them hand off to a notify agent
  #   to_apps: [ironclad]

policies:
  ironclad-banking:
    agents:
      ironclad.banking_agent:          # agent IDs are "app.agent"
        allowed_tools:
          - get_accounts               # bare names resolve within the agent's app
          - notify.send_email          # another app's tool: needs a grant
```

- **IDs.** Tools are `app.tool` and agents are `app.agent`. Top-level `tools:` with full `endpoint`s still work and belong to the `default` app, whose IDs stay bare, so existing configs are unchanged.
- **Boundaries.** An agent may only use its own app's tools and delegate to its own app's agents, unless a grant says otherwise. Config load rejects ungranted cross-app references, and `execute()` / `check_handoff()` enforce the same rule at call time as `cross_app_denied`. That is always a hard block, whatever the strictness. A grant lifts only the app boundary: the tool still has to be in `allowed_tools`.
- **Credentials.** When forwarding to a tool, Anvil reads the tool's app secret from `secret_env` and sends it in `header`. If the variable is unset, the call fails closed as `upstream_error`. The secret is redacted from response data and error messages, so it never reaches the agent, traces, or audit logs.
- **Trust.** A tool's own `trust` is its default; an agent's `trust_overrides` take priority; a tool with no `trust` uses `settings.default_trust`. Responses of untrusted tools are injection-scanned and schema-checked.
- **Parameter checks.** Before a tool runs, the params are checked against its `parameters` schema: required fields, types, `enum`, `minimum`/`maximum`, `minLength`/`maxLength`, nested objects and array `items`. Fields the schema doesn't declare are rejected unless it says `additionalProperties: true`. Path params (`/orders/{order_id}`) are checked like any other and URL-encoded when substituted. A failed check is a hard block (`invalid_params`). A tool with no `parameters` isn't checked, and Anvil logs a warning once per tool. Values the host application fills in itself go in `execute(..., host_params={...})`: they're merged over the agent's params, and a host param the agent didn't send may be undeclared, so the agent can't set it.
- **Redaction.** `redact_fields: [otp_code, session_token]` on a tool strips those response fields, at any depth, before anything downstream sees the response: the agent, injection scans, schema checks and traces. Use it for secrets a tool returns that the agent must never hold. Even if a tool leaks a secret, the agent never sees it. A field can't be both redacted and listed in `response_schema.required_fields`.
- **Tool names for LLMs.** OpenAI and Anthropic don't allow dots in function names, so `GET /tools/{id}/schema?format=openai|anthropic&agent=ironclad.banking_agent` names tools relative to the calling agent's app: bare for its own tools (`send_email`), `app__tool` for granted tools of other apps (`notify__send_email`, `default__x`). Without `agent`, the API key's app is used. `execute()` accepts every spelling, so whatever name the model calls reaches the right tool, even when two apps both have a `send_email`. Corrections and `required_actions` use the same spelling. Because of this, `__` isn't allowed in app, tool or agent names.

Config is validated at load time and Anvil **refuses to start** on: references to unregistered tools, `allowed_tools` mismatches, cross-app references without a grant, agents or tools in undeclared apps, plaintext API keys, circular prerequisites, or `match_fields` not present in a tool's parameter schema.

`allowed_tools` and injection detection are enforced regardless of strictness. Only sequence/schema violations respect `strictness: loose`.

### Agent checks (multi-agent pipelines)

Add `allowed_delegations: [other_agent, ...]` to an agent to control which agents it may hand work to. It is enforced like `allowed_tools`: a hard block, whatever the strictness.

```python
await anvil.check_agent_input("planner", user_message, trace_id=req_id)
await anvil.check_agent_output("researcher", draft, trace_id=req_id)
await anvil.check_handoff("planner", "researcher", payload=task, trace_id=req_id)
```

Each returns `AgentCheckResult(allowed, correction)` and never raises. Input, output, and handoff payloads go through the same injection scanner as tool calls. Checks are recorded in the session trace with a `kind` field. They do not count as tool prerequisites.

## Integration with Any Framework

Anvil wraps the tool-call interface, not the orchestration loop, so it drops into LangGraph, AutoGen, CrewAI, or a raw ReAct loop identically:

```python
anvil = await Anvil.from_registry("http://localhost:8100", policy="strict-payments")
anvil.register_tool("send_payment", endpoint="http://localhost:8001/pay", trust="untrusted")

# In your framework's tool-call handler:
response = await anvil.execute(agent_id, tool_name, params)
return response.model_dump()   # feed straight back to the agent as the tool result
```

### Binding a caller to an app

```python
# Library mode: this instance only accepts agents of the ironclad app.
anvil = Anvil.from_config("anvil.yaml", policy="ironclad-banking", app="ironclad")
await anvil.execute("banking_agent", "get_accounts", params)  # -> ironclad.banking_agent
await anvil.execute("notify.dispatcher", "send_email", params)  # -> cross_app_denied

# Registry mode: the API key decides which app's policies and tools you get.
anvil = await Anvil.from_registry(
    "http://localhost:8100", policy="ironclad-banking", app="ironclad",
    client_id="ironclad-agent-service", client_key=os.environ["ANVIL_REGISTRY_KEY"],
)
```

The registry stores only SHA-256 digests (`python -c "import hashlib,sys; print(hashlib.sha256(sys.argv[1].encode()).hexdigest())" <key>`). The client sends `X-Client-Id` plus `Authorization: Bearer <key>`. Once any app has `api_keys`, every JSON API route requires a valid key and is scoped to that app:
- **Policies:** only those whose agents all belong to the app. Anything else returns 404.
- **Tools:** the app's own tools plus tools granted to it.
- **Traces and checkouts:** only the app's own.

**App keys are read-only for policies.** `POST /policies`, `PUT /policies/{name}` and `DELETE /policies/{name}` need the admin: either `Authorization: Bearer <admin key>`, or the UI session cookie with `X-Anvil-UI`. An app key gets a 403 on those routes, even for its own app's policies. With no keys configured, the registry is open, as before.

For frameworks that need tool *definitions* up front, pull them from the registry instead of hand-writing them:

```
GET /tools/send_payment/schema?format=openai&agent=payment_agent
GET /tools/send_payment/schema?format=anthropic&agent=payment_agent
```

## Registry UI (v2)

Server-rendered pages served directly by the registry app — Jinja2 templates, no build step, no separate frontend process. Vanilla JS handles the interactive bits (saving a policy, polling for activity) by calling the same JSON API described above.

| Page | Path | What it does |
|---|---|---|
| Dashboard | `/` | Policy/tool/session/trace/orphan counts, recent traces |
| Policies | `/ui/policies` | List all policies |
| Policy detail | `/ui/policies/{name}` | Agent summary + a raw-JSON editor that saves via the existing `PUT /policies/{name}` |
| New policy | `/ui/policies/new` | Register a new pipeline (`POST /policies`) — this is how you add a policy for a new agent/pipeline |
| Tools | `/ui/tools` | Registered tools with one-click schema export links (openai/anthropic/raw) |
| Activity | `/ui/activity` | Active sessions + recent completed traces, polled every 3s |
| Orphans | `/ui/orphans` | Sessions that checked out a policy but never sent a trace |
| Trace detail | `/ui/traces/{session_id}` | Full call timeline for one session |

**"Activity" is not a call-by-call stream.** The library only talks to the registry at session start (policy checkout) and session end (trace flush) — that's the whole point of the one-hop design (see Architecture above). So the activity feed shows sessions currently checked out plus completed traces, polled periodically; it will not show individual tool calls as they happen mid-session. The page says so.

Creating or editing a policy through the UI (or the raw API) runs the same fail-fast validation the YAML loader applies — dangling tool references and circular prerequisites are rejected with a 422, not silently accepted.

**UI auth.** With no app keys configured, the UI is open. Once any app has `api_keys`, `/` and every `/ui/*` page require the admin key. Set `ANVIL_ADMIN_KEY` to its SHA-256 digest; plaintext is rejected at startup.
- **Browsers** log in at `/ui/login`. This sets an `HttpOnly`, `SameSite=Strict` cookie holding a random session token, not the key.
- **Scripts** can send `Authorization: Bearer <admin key>` instead.

The admin sees every app. The UI's fetch helper sends the session cookie plus an `X-Anvil-UI` header, and the API only accepts the cookie when that header is present. This blocks cross-site form posts.

## Security Model

**Apps are the trust boundary.** A library instance bound with `app=` can't act as another app's agent. A registry caller with an app API key can only read that app's policies and tools, and only read and post that app's traces. Changing policies needs the admin key. Within one app, Anvil still trusts the calling code to pass the right `agent_id`: any code holding an app's instance or key can claim to be any of that app's agents. Anvil guards against agent error, misconfiguration and one app overreaching into another. It does not guard against a compromised app process.

Known gaps:
- With no `api_keys` configured, the registry API and UI are fully open (the pre-apps behavior).
- Admin sessions live in registry memory and end on restart.

### Injection scanning

Anvil scans tool requests, untrusted tool responses, and agent input, output and handoff payloads. `settings.injection_scan_mode` picks how:

| Mode | What runs | Cost |
|---|---|---|
| `regex` (default) | Pattern matching for known phrasings (instruction overrides, role switches, exfiltration URLs) | Zero latency, zero cost. Misses novel phrasing and can flag tools that legitimately discuss prompt injection. |
| `llm` | An LLM classifier (`injection_scanner`: OpenAI or Anthropic over raw httpx, no SDK needed) | One cheap model call per scan |
| `hybrid` | Regex first; the LLM only for payloads regex passes | LLM calls only on regex misses |

If the classifier fails for any reason (network, timeout, bad JSON, missing API key), the scan falls back to regex. The pipeline never crashes on a scan.

**Pluggable classifier.** Pass your own async `(text) -> InjectionResult` to `Anvil(..., classifier=fn)` (also accepted by `from_config` / `from_registry`). It replaces the built-in LLM scanner in `llm` and `hybrid` modes. In the default `regex` mode, passing one runs as `hybrid`. A classifier that raises or returns the wrong type falls back to regex.

```python
from anvil import Anvil, InjectionResult

async def my_classifier(text: str) -> InjectionResult:
    flagged = await my_model.is_injection(text)
    return InjectionResult(detected=flagged, matched_pattern="my_model" if flagged else None)

anvil = Anvil.from_config("anvil.yaml", policy="strict-payments", classifier=my_classifier)
```

Use `injection_scanning: false` on a tool to skip scanning for it.

## Why Not Just Use Prompts?

Telling the model "always verify before paying" in a system prompt degrades under compounding probability — a 98%-reliable instruction followed 20 times in a session is under 70% reliable across the session, and that's before an indirect prompt injection in a tool response tries to override it deliberately. Anvil moves the control outside the model: it can't be argued out of a rule, and every decision is logged with the exact call history that triggered it — which a prompt-only approach can't give you for an audit.

## License

Apache License 2.0. See [LICENSE](https://github.com/infoshikharpathak/anvil/blob/main/LICENSE).
