Skip to main content

air-langchain-trust

PyPI Python 3.10+ License: MIT LangChain

AIR Trust Layer for LangChain / LangGraph — Drop-in security, audit, and compliance for your AI agents.

Part of the AIR Blackbox ecosystem. Adds tamper-proof audit trails, sensitive data tokenization, consent gates for destructive tools, and prompt injection detection to any LangChain or LangGraph project.

Quick Start

pip install air-langchain-trust
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_openai_tools_agent
from air_langchain_trust import AirTrustCallbackHandler

# Create the trust handler
handler = AirTrustCallbackHandler()

# Use it with any LangChain component via config
llm = ChatOpenAI(model="gpt-4")
result = llm.invoke(
    "What is the capital of France?",
    config={"callbacks": [handler]}
)

# Or with agents — callbacks propagate to all child components
result = agent_executor.invoke(
    {"input": "Search for AI safety papers"},
    config={"callbacks": [handler]}
)

# Check what happened
print(handler.get_audit_stats())
print(handler.verify_chain())

What It Does

Tamper-Proof Audit Trail

Every tool call, LLM invocation, and chain execution is logged to an HMAC-SHA256 signed chain. Each entry references the previous entry's hash — modify any record and the chain breaks.

Sensitive Data Tokenization

API keys, credentials, PII (emails, SSNs, phone numbers, credit cards) are automatically detected in tool inputs and LLM prompts, logged as tokenized versions. 14 built-in patterns covering API keys, credentials, and PII.

Consent Gate

Destructive tools are blocked until the user explicitly approves them. Unlike CrewAI (where hooks return False), LangChain callbacks block by raising exceptions:

from air_langchain_trust import ConsentDeniedError, InjectionBlockedError

try:
    result = agent.invoke(input, config={"callbacks": [handler]})
except ConsentDeniedError as e:
    print(f"Tool '{e.tool_name}' blocked (risk: {e.risk_level})")
except InjectionBlockedError as e:
    print(f"Injection detected (score: {e.score}, patterns: {e.patterns})")
Risk Level Tools Action
Critical exec, spawn, shell Always requires consent
High fs_write, deploy, git_push Requires consent (default)
Medium send_email, http_request Configurable
Low fs_read, search, query Auto-approved

Prompt Injection Detection

15+ weighted patterns detect prompt injection attempts including role overrides, jailbreaks, delimiter injection, privilege escalation, and data exfiltration.

Configuration

from air_langchain_trust import AirTrustCallbackHandler, AirTrustConfig

config = AirTrustConfig(
    consent_gate={
        "enabled": True,
        "always_require": ["exec", "spawn", "shell", "deploy"],
        "risk_threshold": "high",
    },
    vault={
        "enabled": True,
        "categories": ["api_key", "credential", "pii"],
    },
    injection_detection={
        "enabled": True,
        "sensitivity": "medium",
        "block_threshold": 0.8,
    },
    audit_ledger={
        "enabled": True,
        "max_entries": 10000,
    },
    # Optional: forward to AIR Blackbox gateway
    gateway_url="https://your-gateway.example.com",
    gateway_key="your-api-key",
)

handler = AirTrustCallbackHandler(config)

LangChain Callback Mapping

LangChain Callback Trust Components
on_tool_start ConsentGate → DataVault → AuditLedger
on_tool_end AuditLedger
on_tool_error AuditLedger
on_llm_start InjectionDetector → DataVault → AuditLedger
on_llm_end AuditLedger
on_chain_start AuditLedger
on_chain_end AuditLedger

Works with LangGraph Too

from langgraph.graph import StateGraph
from air_langchain_trust import AirTrustCallbackHandler

handler = AirTrustCallbackHandler()
graph = StateGraph(...)
app = graph.compile()

# Callbacks propagate through the entire graph
result = app.invoke(input, config={"callbacks": [handler]})

API Reference

from air_langchain_trust import AirTrustCallbackHandler

handler = AirTrustCallbackHandler(config=None)

# Inspection methods
handler.get_audit_stats()   # → {"total_entries": 42, "chain_valid": True, ...}
handler.verify_chain()      # → {"valid": True, "total_entries": 42}
handler.export_audit()      # → [{"id": "...", "action": "tool_call", ...}, ...]
handler.get_vault_stats()   # → {"total_tokens": 5, "by_category": {"api_key": 3}}

AIR Blackbox Ecosystem

Repository Purpose
gateway Go reverse proxy gateway
air-blackbox-mcp MCP server for Claude Desktop
air-crewai-trust Trust layer for CrewAI
air-anthropic-trust Trust layer for Anthropic SDK
air-langchain-trust Trust layer for LangChain (this repo)

Development

git clone https://github.com/airblackbox/air-langchain-trust.git
cd air-langchain-trust
pip install -e ".[dev]"
pytest tests/ -v

License

MIT

Metadata

Release files for air-langchain-trust 0.2.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 air-langchain-trust 0.2.0
File Size Uploaded Provenance
air_langchain_trust-0.2.0.tar.gz 30.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for air-langchain-trust 0.2.0
File Interpreter ABI Platform
air_langchain_trust-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 56.6 kB

Release files / air_langchain_trust-0.2.0.tar.gz

Download URL air_langchain_trust-0.2.0.tar.gz
Size 30.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a003bbaf736d660d0f6b413144e92a098f8d96815dcd8c9919fa7c36beac8cd9
BLAKE2b-256 checksum
How to use checksums
b53cfb335b20c3b2412b936a91841c3be27d72951230fec03c2f60d239b12720
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release files / air_langchain_trust-0.2.0-py3-none-any.whl

Download URL air_langchain_trust-0.2.0-py3-none-any.whl
Size 26.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc9c534e14b8188ca530002bc90e54dd9619aa72751d10aab15e42e63ea382f4
BLAKE2b-256 checksum
How to use checksums
ba4ae4980e69bc7788ce145f90d3f87ba2d1db96489ed52276164a19b29c86cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Security for air-langchain-trust 0.2.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.2.0 This release

2 release files No 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