LangChain Integration

Secure your LangChain agents with AIM in 2 lines of code. Get complete audit trails, trust scoring, and security alerts for all tool invocations - without refactoring.

What You'll Get

  • Secure existing LangChain agents (zero refactoring)
  • Complete audit trail of all tool uses
  • Real-time trust scoring
  • Security alerts for anomalous behavior
  • Automatic action verification before tool execution

Integration Time: 5 minutes

Code Changes: 2-3 lines

Difficulty: Beginner

Quick Start (5 Minutes)

Step 1: Download SDK from AIM Dashboard

Important: There is NO pip package for the AIM SDK. You must download it from your AIM dashboard with pre-configured credentials.

  1. 1. Login to AIM Dashboard → Settings → SDK Download
  2. 2. Click "Download Python SDK" (contains your credentials)
  3. 3. Extract the downloaded ZIP file
  4. 4. Install dependencies: pip install langchain langchain-openai keyring PyNaCl

Step 2: Register Agent

In AIM Dashboard (running on http://localhost:8080):

  1. Navigate to Agents → Register New Agent
  2. Name: langchain-assistant
  3. Type: AI Agent
  4. Copy the generated credentials
export AIM_URL="http://localhost:8080"
export AIM_AGENT_ID="your-agent-id"
export AIM_PRIVATE_KEY="your-private-key"
export OPENAI_API_KEY="your-openai-key"

Step 3: Add AIM to Your LangChain Agent

Before (Unsecured)

from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI
from langchain.tools import Tool

# Your existing LangChain agent
llm = ChatOpenAI(model="gpt-4")
tools = [search_tool, calculator_tool]
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)

# Run agent (no security, no audit trail)
result = agent_executor.run("What's the weather in SF?")

After (Secured with AIM) - Just Add 3 Lines

from aim_sdk import secure  # ← Line 1: Import AIM
from aim_sdk.integrations.langchain import AIMCallbackHandler
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI

# Register with AIM
aim_agent = secure("langchain-assistant")  # ← Line 2: Secure your agent

# Your existing LangChain agent (unchanged)
llm = ChatOpenAI(model="gpt-4")
agent = create_openai_functions_agent(llm, tools, prompt)

# Add AIM callback
agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    callbacks=[AIMCallbackHandler(aim_agent=aim_agent)]  # ← Line 3: Add callback
)

# Run agent - now secured with full audit trail!
result = agent_executor.run("What's the weather in SF?")

That's it! Your LangChain agent is now secured with:

  • Complete audit trail of every tool invocation
  • Real-time trust scoring (0-100%)
  • Security alerts for anomalous behavior
  • Automatic compliance reporting (SOC 2, HIPAA, GDPR)

What Gets Logged

Every tool invocation is logged to AIM with:

  • Tool Name: Which tool was called (e.g., "search_database")
  • Arguments: What parameters were passed
  • Result: What the tool returned
  • Timestamp: When the action occurred
  • Agent ID: Which agent performed the action
  • Trust Score: Agent's trust score at execution time
  • Verification Status: Whether action was approved/denied

View in AIM Dashboard

After running your LangChain agent, visit the AIM Dashboard to see:

  • Activity Feed: Real-time stream of all tool invocations
  • Trust Score Timeline: How your agent's trust evolves over time
  • Security Alerts: Any anomalous behavior detected
  • Audit Logs: Complete compliance-ready audit trail
  • Performance Metrics: Tool success rates, latency, errors

Advanced: Explicit Verification

For sensitive operations, you can require explicit verification before tool execution:

from aim_sdk.integrations.langchain import AIMVerifiedTool

# Wrap sensitive tools with verification
verified_db_tool = AIMVerifiedTool(
    aim_agent=aim_agent,
    tool=database_tool,
    require_verification=True  # Blocks until verified in AIM dashboard
)

# Add to your agent
tools = [search_tool, calculator_tool, verified_db_tool]

Next Steps

Need Help?

Questions about integrating AIM with LangChain? We're here to help!