> ## Documentation Index
> Fetch the complete documentation index at: https://developers.agentx.so/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain

> Evaluate a LangChain chain or agent

Install:

```bash theme={null}
pip install agentx-python langchain langchain-openai
```

## Usage

```python theme={null}
from agentx import AgentX
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

client = AgentX.from_env()

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful support agent."),
    ("human", "{question}"),
])
chain = prompt | llm | StrOutputParser()

def langchain_agent(case):
    output = chain.invoke({"question": case.query})
    return {"output": output, "metadata": {"model": "gpt-4o-mini"}}

run_context = (
    client.evaluations
    .run(dataset_id="...", subject={"kind": "custom_agent", "displayName": "Support Chain", "framework": "langchain"})
    .execute(langchain_agent)
    .finalize()
)
print(f"Average rating: {run_context.average_rating:.2f}")
```

Returning `metadata: {"model": ...}` records which model produced each response, powering the Sovereignty & Portability breakdown in the report. This pattern works the same way for `AgentExecutor`-style agents: call `agent.invoke(...)` instead of `chain.invoke(...)` inside `langchain_agent`.

## With tracing

Pass an `AgentXCallbackHandler` into `chain.invoke(..., config={"callbacks": [handler]})` to get a full Execution Timeline per result. Unlike the raw-tracer pattern used for OpenAI/Anthropic, the handler's traces are always sent async, so `trace_id` isn't available directly from it. Capture it by additionally wrapping the call in a `sync=True` span, the same way the [Anthropic example](/sdk/evaluations/examples/anthropic#with-tracing) does:

```python theme={null}
from agentx import AgentX
from agentx.integrations.langchain import AgentXCallbackHandler
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

client = AgentX.from_env()
handler = AgentXCallbackHandler(tracer=client.tracer, name="support-chain")

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful support agent."),
    ("human", "{question}"),
])
chain = prompt | llm | StrOutputParser()

def langchain_agent(case):
    with client.tracer.trace("support-chain-call", framework="langchain", sync=True, monitor=False) as span:
        output = chain.invoke({"question": case.query}, config={"callbacks": [handler]})
        span.output = output

    return {
        "output": output,
        "metadata": {"model": "gpt-4o-mini"},
        "trace_id": span.trace_id,
    }
```

<Tip>
  If you only want tracing (not evaluation), see the [LangChain tracing integration](/sdk/integrations/langchain): pass `AgentXCallbackHandler` on its own with no surrounding span, and every chain invocation is traced automatically.
</Tip>

A complete working example is available as [`langchain_eval.py`](https://github.com/AgentX-ai/AgentX-Python/blob/main/examples/evaluations/langchain_eval.py) in the AgentX-Python repository.
