Integrations
LangChain Integration
AgentTrustCallback and LangChain agent governance integration.
LangChain Integration
Overview
AgentTrust integrates with LangChain via auto_instrument() (patches LCEL and legacy chains) and @harness on chain invocation wrappers. No dedicated adapter module — integration uses core SDK hooks.
Tier: OSS+ (auto-instrument) · Team for advanced graph node patterns
Why It Matters
LangChain is one of the most common agent frameworks. Governance must work with LCEL pipelines, tool calls, and chain callbacks without rewriting chain definitions.
Prerequisites
pip install agentrust-sdk langchain langchain-openaiStep-by-Step Guide
Pattern A — Auto-instrument
from agentrust_sdk import auto_instrument, embed_gateway
embed_gateway()
auto_instrument()
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template("Answer: {input}")
chain = prompt | ChatOpenAI(model="gpt-4o-mini")
result = chain.invoke({"input": "What is AI governance?"})Pattern B — @harness wrapper
from agentrust_sdk import harness
@harness(agent_id="langchain-agent", framework="LangChain")
def run_chain(user: str, input: str) -> dict:
result = chain.invoke({"input": input})
return {"answer": result.content}Pattern C — Direct client
from agentrust_sdk import AgentTrustClient
with AgentTrustClient() as client:
output = chain.invoke({"input": "..."})
result = client.validate(
agent_id="langchain-agent",
user="alice",
input="...",
output={"answer": output.content},
framework="LangChain",
)Examples
See examples/langchain_agent.py for all three patterns.
Best Practices
- Call
auto_instrument()before LangChain imports - Include tool call metadata in validation for tool trust checks
- Use
@harnesswith explicitagent_idin production - For LangGraph graphs, use the LangGraph adapter instead
Common Mistakes
- Confusing LangChain hooks with LangGraph
AgentTrustNode(different adapters) - Not returning structured dict from
@harnesswrapper - Import order: auto_instrument must precede langchain imports
Troubleshooting
| Issue | Fix |
|---|---|
| Chain calls not validated | Check import order and AGENTRUST_AUTO_INSTRUMENT |
| Tool trust failures | Pass tool_calls in direct client validation |
| LangGraph in same app | Use LangGraph adapter for graph nodes |