LangChain, CrewAI and LlamaIndex
Drop-in zn guards for popular Python agent frameworks.
The adapters ship with the Python package (pip install zn-gate) under zn_gate.integrations. They always run locally and never use the network. They check what goes into a tool and what comes out of it, and mask_secrets=True hides secrets in tool output.
LangChain and LangGraph
A callback handler checks prompts, tool calls and tool outputs as the agent runs:
from zn_gate.integrations import ZnGuardCallbackHandler
handler = ZnGuardCallbackHandler(on_block="raise")
# pass callbacks=[handler] to your agent, chain or tool
With on_block="raise" (default) a blocked prompt, tool call or tool output raises GuardBlockError out of the agent: the tool does not run and the output does not reach the model. on_block="ignore" turns this off: nothing is stopped, but each blocked prompt, tool input, tool output, tool error or secret in a tool output that is let through is logged as a warning (logger zn_gate) with the rule and where it happened, never the text. "return" is not accepted (it raises ValueError), because LangChain ignores what a callback returns.
A callback cannot change what a tool returns. So with mask_secrets=True (default) a tool output that contains a secret also stops the run. To hide the secret and keep going, or to give the model a block message instead of the output, wrap the tool (needs langchain-core):
from langchain_core.tools import tool
from zn_gate.integrations import guard_langchain_tool
@tool
def read_notes(topic: str) -> str:
"""Read the team notes on a topic."""
return "Deploy key: AKIAIOSFODNN7EXAMPLE"
safe_read_notes = guard_langchain_tool(read_notes, on_block="return")
print(safe_read_notes.invoke({"topic": "deploy"}))
# Deploy key: [REDACTED_AWS_KEY]
guard_langchain_tool returns a tool with the same name, description and arguments. It checks the arguments before the tool runs, checks the output for prompt injection (check_result=True) and hides secrets in it (mask_secrets=True) before the model reads it. With on_block="return" the model gets fallback, or a short block message, instead; with "raise" (default) it raises GuardBlockError. Pass the wrapped tools to your agent, and use it on every tool that reads outside data or can reach files, shells or the network.
CrewAI
Wrap a tool function so blocked calls return a fallback instead of executing:
from zn_gate.integrations import guarded_tool
@guarded_tool(on_block="return", fallback="BLOCKED_BY_GUARD", mask_secrets=True)
def run_search(query: str):
return search_api(query)
Without fallback=, a blocked call returns zn's default message, which gives the reason and one next step. Pass fallback= to keep a fixed string.
LlamaIndex
Check a query before it reaches the index, and clean the response:
from zn_gate.integrations import ZnLlamaGuard
guard = ZnLlamaGuard(on_block="return")
safe_query = guard.on_query_start(user_query)
clean_response = guard.sanitize_response(query_engine.query(safe_query))
With on_block="return", on_query_start returns fallback, or a short block message, in place of the query, so the engine never sees the blocked text. sanitize_response hides secrets in text, lists, dicts and in the text of a LlamaIndex Response (on a copy).
For LlamaIndex tools, wrap the function with guarded_tool before FunctionTool.from_defaults(fn=...): the arguments are checked before it runs, and its output is checked and masked before the agent reads it:
from llama_index.core.tools import FunctionTool
from zn_gate.integrations import guarded_tool
@guarded_tool(on_block="return", mask_secrets=True, check_result=True)
def read_notes(topic: str) -> str:
return "Notes on " + topic
tool = FunctionTool.from_defaults(fn=read_notes)
Not using a framework? See the SDK.