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
  • Dynamic 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
  • Dynamic 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: 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? Support is available.