LangChain
Connect Akto with LangChain
Overview
LangChain is a framework for developing applications powered by language models. Akto provides two ways to connect with your LangChain applications:
LangChain Hooks (Recommended) — A Python middleware that plugs directly into your LangChain agent via the
AgentMiddlewareinterface. It validates prompts and responses against Akto guardrails in real time.LangSmith Connector — A cron-based connector that pulls execution traces from LangSmith for monitoring.
The Akto LangChain integration automatically:
Validates AI requests and responses against security policies
Detects PII, prompt injection, and policy violations
Blocks malicious requests (sync mode) or logs violations (async mode)
Ingests traffic into Akto for monitoring and analysis
Prerequisites
Before integrating Akto with LangChain, ensure you have:
A LangChain application using
langchainandlanggraphPython 3.9+
httpxpackage installedAkto guardrails service endpoint (your
AKTO_DATA_INGESTION_URL)
Option 1: LangChain Hooks (Recommended)
This approach uses Akto's AktoGuardrailsMiddleware — a class-based AgentMiddleware that intercepts model calls to enforce Akto guardrails before and after each LLM invocation.
How It Works
The middleware hooks into two points of the LangChain agent lifecycle:
before_model— Validates the prompt against Akto guardrails before the LLM is called. In sync mode, a policy violation blocks the request immediately.after_model— Ingests the completed interaction (prompt + response) into Akto for audit and dashboard visibility.
Both synchronous and asynchronous agent execution modes are supported.
Request Flow (AKTO_SYNC_MODE=true)
Request Flow (AKTO_SYNC_MODE=false)
Steps to Connect
Install Dependencies
Ensure the required packages are installed:
Download the Middleware
Download the akto_middleware.py file into your project:
Configure Environment Variables
Set the following environment variables in your shell or .env file:
Note
AKTO_SYNC_MODE determines behavior:
AKTO_SYNC_MODE=true: Prompts are validated before being sent to the LLM. Policy violations raise aValueErrorand block the request.AKTO_SYNC_MODE=false: All requests proceed immediately. Interactions are ingested after the fact for logging and audit only.
Integrate the Middleware into Your Agent
Import AktoGuardrailsMiddleware and pass it to your LangChain agent's middleware list:
The middleware automatically handles both sync and async execution paths — no additional configuration is needed.
Verify Integration
Run your agent and check the logs for middleware initialization:
Then verify in the Akto dashboard:
Log into your Akto dashboard
Navigate to the Collections section
Verify you see requests from your LangChain application appearing
Configuration Reference
AKTO_DATA_INGESTION_URL
Yes
Akto service base URL
AKTO_SYNC_MODE
No
true
true to block on violation, false for log-only
AKTO_TIMEOUT
No
5
HTTP timeout in seconds
LOG_LEVEL
No
INFO
Logging level
LOG_PAYLOADS
No
false
Log full request/response payloads (privacy-sensitive)
LANGCHAIN_API_HOST
No
api.langchain.com
Host header used in the proxy payload
LANGCHAIN_API_PATH
No
/langchain/chat
Path used in the proxy payload
Handling Blocked Requests
When AKTO_SYNC_MODE=true and a request is blocked by guardrails, the middleware raises a ValueError:
You can catch this in your application to handle blocked requests gracefully:
Option 2: LangSmith Connector
This approach uses a cron-based connector that pulls execution traces from LangSmith for monitoring. Use this if you are already using LangSmith and want to monitor traffic without modifying your application code.
Steps to Connect
Configure Akto Traffic Processor
Set up and configure your Traffic Processor. The steps are mentioned here.
Download Configuration Files
Update Environment Variables
Update the following variables in the langchain-cron.env file:
Start the LangChain Traffic Connector
Run the following command to start the LangChain traffic connector:
This will start monitoring your LangChain applications and send API traffic data to Akto for analysis.
What Data is Collected?
Application Metadata
All LangChain applications and traces
Execution Data
Recent execution traces
Input and output data
Get Support for your Akto setup
There are multiple ways to request support from Akto. We are 24X7 available on the following:
In-app
intercomsupport. Message us with your query on intercom in Akto dashboard and someone will reply.Join our discord channel for community support.
Contact
[email protected]for email support.Contact us here.
Last updated