LangChain / LangGraph
Add Trappr security to any LangChain or LangGraph agent in two lines of Python or TypeScript. Route all LLM calls through the Trappr Gateway — no changes to your chains, agents, or tools.
How it works
LangChain's ChatOpenAI and ChatAnthropic classes (and their equivalents) accept a custom base_url. Point that at the Trappr Gateway and swap your provider key for your Trappr Gateway key — the rest of your agent code is untouched.
Python — LangChain / LangGraph
ChatOpenAI
from langchain_openai import ChatOpenAI
# Before
llm = ChatOpenAI(model="gpt-4o", openai_api_key="sk-...")
# After — only openai_api_base and openai_api_key change
llm = ChatOpenAI(
model="gpt-4o",
openai_api_key="<your-trappr-gateway-key>",
openai_api_base="https://gateway.trappr.net/v1",
)ChatAnthropic
from langchain_anthropic import ChatAnthropic
# Use the OpenAI-compatible shim for Anthropic via Trappr
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="claude-sonnet-4-6",
openai_api_key="<your-trappr-gateway-key>",
openai_api_base="https://gateway.trappr.net/v1",
)Full LangGraph agent example
import os
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
import operator
TRAPPR_KEY = os.environ["TRAPPR_GATEWAY_KEY"]
llm = ChatOpenAI(
model="gpt-4o",
openai_api_key=TRAPPR_KEY,
openai_api_base="https://gateway.trappr.net/v1",
)
class AgentState(TypedDict):
messages: Annotated[list, operator.add]
def call_model(state: AgentState):
response = llm.invoke(state["messages"])
return {"messages": [response]}
graph = StateGraph(AgentState)
graph.add_node("agent", call_model)
graph.set_entry_point("agent")
graph.add_edge("agent", END)
app = graph.compile()
result = app.invoke({"messages": [("user", "Summarize this report.")]})TypeScript / JavaScript
import { ChatOpenAI } from "@langchain/openai";
// Before
const llm = new ChatOpenAI({ model: "gpt-4o", openAIApiKey: "sk-..." });
// After
const llm = new ChatOpenAI({
model: "gpt-4o",
openAIApiKey: process.env.TRAPPR_GATEWAY_KEY,
configuration: {
baseURL: "https://gateway.trappr.net/v1",
},
});Execution tracking with LangChain
For multi-step agents, wrap the execution with a Trappr execution token so all LLM calls from one agent invocation are grouped together in the dashboard.
import httpx
def start_execution(agent_key: str, agent_id: str) -> str:
resp = httpx.post(
"https://api.trappr.net/v1/agents/executions",
headers={"Authorization": f"Bearer {agent_key}"},
json={"agentId": agent_id},
)
return resp.json()["token"]
def end_execution(agent_key: str, token: str, status: str):
httpx.patch(
f"https://api.trappr.net/v1/agents/executions/{token}",
headers={"Authorization": f"Bearer {agent_key}"},
json={"status": status},
)
# Usage
token = start_execution(AGENT_KEY, "cht_agent_XXXX")
llm = ChatOpenAI(
model="gpt-4o",
openai_api_key=AGENT_KEY,
openai_api_base="https://gateway.trappr.net/v1",
default_headers={"X-Trappr-Execution": token},
)
try:
result = app.invoke({"messages": [("user", "Process this data.")]})
end_execution(AGENT_KEY, token, "completed")
except Exception as e:
end_execution(AGENT_KEY, token, "failed")
raiseEnvironment variable setup
# .env TRAPPR_GATEWAY_KEY=cht_gw_XXXX TRAPPR_AGENT_KEY=cht_ak_XXXX TRAPPR_AGENT_ID=cht_agent_XXXX # Remove or comment out your direct provider keys # OPENAI_API_KEY=sk-... <- no longer needed
OPENAI_API_KEY from the environment automatically, set it to your Trappr gateway key and set OPENAI_API_BASE tohttps://gateway.trappr.net/v1. LangChain will pick both up without any code change.LangChain callbacks
Trappr is fully compatible with LangChain callbacks. You can continue using LangSmith, Lunary, or any other callback handler alongside Trappr monitoring — they operate independently.
from langchain.callbacks import LangChainTracer
llm = ChatOpenAI(
model="gpt-4o",
openai_api_key=TRAPPR_KEY,
openai_api_base="https://gateway.trappr.net/v1",
callbacks=[LangChainTracer()], # works alongside Trappr
)