Skip to main content

recusal, deterministic governance for AI agents

recusal-langchain

PyPI CI python license

The Recusal gate for a LangChain create_agent agent: every tool call the agent proposes is judged by your policy before the tool runs, and a refused call never runs.

New to Recusal? It checks each tool call an AI agent proposes against your policy and refuses the unsafe or unapproved ones before the tool runs. No model makes that decision. A policy is a plain Python function: it is given the tool's name and its arguments and returns a list of findings. An empty list lets the call run; a failing finding refuses it and gives the reason. Writing a policy covers the two postures, allowlist and deny-list.

Before the tool runs. An agent proposes a tool call. Recusal, your policy with no model in it, decides. With no objection the tool runs. On a failing finding, or when the policy itself raises, the call is refused: the tool never runs and the reason goes back to the model. Every decision can be written to a hash-chained audit log, if you pass one.

This is a separate distribution from recusal. The kernel has zero runtime dependencies and a frozen compatibility perimeter; this package carries the LangChain dependency and its own version, so a LangChain release never forces a release of the kernel.

A create_agent agent executes its tools through a LangGraph ToolNode, so this package adds no decision logic of its own. It wraps the gate of recusal-langgraph as LangChain middleware and depends on that package.

Install it from PyPI. recusal-langgraph, which this package requires, comes with it:

pip install recusal-langchain

Requires Python 3.10+, recusal>=0.14.0, recusal-langgraph>=0.2.0 and langchain>=1.4.10. It is tested on every pull request against exactly langchain==1.4.10 with langgraph==1.2.20 and langchain-core==1.7.7, and weekly against the newest releases.

The package ships inline types (py.typed) for your editor and type checker.

Use

from langchain.agents import create_agent
from langchain_core.tools import tool
from recusal import Finding
from recusal_langchain import gate_middleware


@tool
def run_sql(sql: str) -> str:
    """Run a SQL statement."""
    ...


def policy(tool_name, tool_input):
    sql = tool_input.get("sql", "")
    if tool_name == "run_sql" and sql.lstrip().upper().startswith("DELETE"):
        return [Finding.fail("no_deletes", severity="CRITICAL", message="DELETE is not allowed")]
    return []


agent = create_agent(model=model, tools=[run_sql], middleware=[gate_middleware(policy)])

A refused call is answered with an error ToolMessage carrying Recusal's reason, and its tool does not run. A policy that raises refuses the call (fail closed). For agent.ainvoke, use agate_middleware(policy); the sync gate alone raises there, and the async gate alone raises under agent.invoke, so neither runs a tool ungated.

List the middleware last. The first middleware in the list is the outermost, so anything listed after the gate runs between the gate and the tool.

Put every decision on the record

from recusal.audit import AuditLog
from recusal_langchain import audited_gate_middleware

log = AuditLog(path="decisions.jsonl")
agent = create_agent(
    model=model, tools=[run_sql], middleware=[audited_gate_middleware(policy, log)]
)

audited_agate_middleware is the async form. Each call the gate sees, allowed or refused, is one hash-chained entry labeled langchain.create_agent with LangChain's tool call id and a SHA-256 fingerprint of the arguments (never the arguments). If the log cannot be written the call is refused. Check the log with recusal audit verify decisions.jsonl.

What the gate does not see

The gate judges, and records, inside the tool's own lifecycle. A middleware listed after it that rewrites the arguments is still judged and recorded as rewritten. A middleware listed after it that swaps the tool object, or answers in the tool's place, removes the judging copy, and that call is neither judged nor recorded. That is why the gate goes last.

Everything recusal-langgraph refuses before the policy runs is refused here too: a tool that overrides the standard execution lifecycle, input that does not validate, and a callable with a parameter its schema omits.

Compatibility

This package is outside the kernel's frozen perimeter (STABILITY.md, Integrations). Its public names are the ones in recusal_langchain.__all__. It uses only the public names of recusal-langgraph.

A runnable offline demonstration, with a scripted model and no API key, is examples/langchain_agent_gate.py and examples/langchain_agent_audit.py.

More

One kernel, eight adapters. Each adapter package depends on recusal: recusal-crewai, recusal-google-adk, recusal-langchain, recusal-langgraph, recusal-openai, recusal-openai-agents, recusal-openai-agents-mcp and recusal-pydantic-ai. recusal-langchain also depends on recusal-langgraph, and recusal-openai-agents-mcp on recusal-openai-agents. The kernel depends on nothing.

An arrow means "depends on". No arrow leaves the kernel, so a framework's breaking change is answered by a release of that framework's adapter, not by a release of the kernel.

Licensed under Apache-2.0.

Metadata

Release files for recusal-langchain 0.1.3

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

Source distribution (sdist)

Source distribution for recusal-langchain 0.1.3
File Size Uploaded Provenance Actions
recusal_langchain-0.1.3.tar.gz 11.4 kB With provenance Details for recusal_langchain-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for recusal-langchain 0.1.3
File Interpreter ABI Platform Actions
recusal_langchain-0.1.3-py3-none-any.whl Python 3 none any Details for recusal_langchain-0.1.3-py3-none-any.whl

Total release size: 22.5 kB

Release files / recusal_langchain-0.1.3.tar.gz

Download URL recusal_langchain-0.1.3.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
33e33b30460e6d349f8fef42aa982a4c54385ee6c2530a34ec44eb13f12c7cb0
BLAKE2b-256 checksum
How to use checksums
a7ea541e0a516103fd6c5acabdab3c30bfe42fa890af4027fc7bf311ce3c7f59
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 20, 2026.

Transparency log

Release files / recusal_langchain-0.1.3-py3-none-any.whl

Download URL recusal_langchain-0.1.3-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9cb728abd02f458599eb6bc4569635686c4fb0538877f9a8d29f615fcee8d611
BLAKE2b-256 checksum
How to use checksums
b89ba6517ffbcbc66e4cb313def2f720940f889b0469583f0ef5ec814b401a09
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 20, 2026.

Transparency log

Security for recusal-langchain 0.1.3

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 recusal_langchain-0.1.3.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 20, 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.

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