← Back to Learn
sdktutorialguardrails

Adding Safety to Existing LangChain Agents

Authensor

LangChain and LangGraph agents call tools through a well-defined interface, which makes adding safety controls straightforward. Authensor's LangChain adapter wraps the tool execution layer, intercepting every tool call for policy evaluation before it reaches the actual tool.

Installation

npm install @authensor/sdk @authensor/langchain-adapter

Or if you are using the Python SDK:

pip install authensor authensor-langchain

Integration Point

The adapter wraps LangChain's tool execution. Every tool call passes through the Authensor policy engine before the underlying tool function is invoked.

For a LangGraph agent, the integration point is the tool node. The adapter wraps the tool node's execution function:

import { AuthensorToolGuard } from '@authensor/langchain-adapter';
import { createToolNode } from '@langchain/langgraph';

const guard = new AuthensorToolGuard({
  apiUrl: process.env.AUTHENSOR_API_URL,
  apiKey: process.env.AUTHENSOR_API_KEY,
  policyId: 'langchain-agent-policy',
});

const toolNode = createToolNode({
  tools: [searchTool, databaseTool, emailTool],
  guard: guard.wrap(),
});

What the Adapter Does

For every tool call, the adapter:

  1. Constructs an Authensor envelope from the LangChain tool call (tool name, parameters, metadata)
  2. Sends the envelope to the policy engine for evaluation
  3. If the policy allows, the tool executes normally
  4. If the policy denies, the adapter returns a denial message to the agent
  5. If the policy requires approval, the adapter waits for the approval response
  6. The result (allow, deny, or approval outcome) is recorded as an audit receipt

Handling Denials

When a tool call is denied, the agent receives a structured response indicating the denial and the reason. Well-designed agents use this feedback to adjust their approach. Poorly designed agents may retry the same call repeatedly. Configure a maximum retry count to prevent loops.

Preserving Existing Behavior

The adapter is transparent when actions are allowed. The tool receives the same parameters and returns the same result. The only difference is the addition of policy evaluation and audit logging in the execution path.

Start in audit-only mode to verify that the adapter does not interfere with existing functionality. Review the audit logs to confirm that tool calls are recorded correctly before enabling enforcement.

The integration requires minimal code changes because Authensor operates at the tool execution boundary, not inside the agent's reasoning loop.

Keep learning

Explore more guides on AI agent safety, prompt injection, and building secure systems.

View All Guides