Connect LangChain to Anoman AI
Route LangChain agents through Anoman using the OpenAI-compatible base URL. Works with LangChain Expression Language (LCEL), LangGraph, and any LCEL chain. No code changes to your chain definition required.
Prerequisites
- An Anoman AI account with an active API key
- Python 3.9+
- pip (Python package manager)
Step 1: Install dependencies
pip install langchain langchain-openaiStep 2: Configure Anoman as the LLM backend
Set the environment variables before initializing your LLM. The base URL points to Anoman's OpenAI-compatible endpoint.
import os
from langchain_openai import ChatOpenAI
# Set Anoman as the LLM backend
os.environ["OPENAI_API_KEY"] = "anm-sk-your-key-here"
os.environ["OPENAI_BASE_URL"] = "https://api.anoman.io/v1"
llm = ChatOpenAI(model="gpt-4o-mini")Step 3: Use in a chain
Your existing chains work without modification. Every call goes through Anoman's guardrail pipeline automatically.
from langchain_core.messages import HumanMessage
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant."),
("human", "{input}"),
])
chain = prompt | llm
response = chain.invoke({"input": "Summarize this document."})
print(response.content)Using with LangGraph
from langgraph.prebuilt import create_react_agent
agent = create_react_agent(llm, tools=[search_tool, calculator_tool])
result = agent.invoke({"messages": [HumanMessage(content="Find the population of Jakarta.")]})When a guardrail triggers
If a request is blocked by the guardrail pipeline, the API returns a 403 with a structured error. LangChain surfaces this as an exception.
{
"error": {
"code": "guardrail_triggered",
"message": "Request blocked by guardrail pipeline",
"guardrail": "injection",
"score": 0.94
}
}This example is illustrative. Check docs/openapi/ for the current error schema.
Guardrail transparency headers
Every response includes guardrail result headers. Check these in your API logs to verify the pipeline is running correctly.
X-Anoman-Guardrail-Injection: pass score=0.07
X-Anoman-Guardrail-Pii: pass
X-Anoman-Guardrail-Content: pass
X-Anoman-Cache: noneNeed help with your integration?
Join the Founding 50 for early access and direct support from the Anoman team.
Join Founding 50