LangChain Integration
TraceAgent provides a native callback handler for LangChain that automatically captures chain steps, LLM calls, tool usage, and retriever activity.
Installation
pip
pip install trace-agent-sdk trace-agent-langchainuv
uv add trace-agent-sdk trace-agent-langchainBasic Usage
from trace_agent_sdk import TraceAgentClient
from trace_agent_langchain import TraceAgentLangChainCallback
client = TraceAgentClient("http://localhost:8000")
run = client.start_run("langchain-agent", "Answer user question")
callback = TraceAgentLangChainCallback(run)
agent.invoke({"input": "What's the weather?"}, callbacks=[callback])
run.finish({"status": "success"})
Captured Events
| Event | Captured Data |
|---|---|
| Chain start/end | Chain type, input, output, duration |
| LLM call/response | Prompt, response, token counts |
| Tool use/result | Tool name, parameters, output |
| Retriever query | Query, retrieved documents |
| Error | Error type, message, traceback |
How Tools and Models Are Recorded
The callback reports each tool through a single call to the server, so a tool run appears once in the timeline with its real name and duration, and it is counted once in the run's tool and error totals. A failed tool counts as one error.
For every model call, the callback also remembers the provider and model, so the run shows them (and their token usage when the model reports it).
The callback never slows down or breaks your chain: if the TraceAgent server is down, the chain keeps running.
With Local Models
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="http://localhost:5000/v1",
api_key="not-needed",
model="local-model"
)
callback = TraceAgentLangChainCallback(run)
agent.invoke({"input": "..."}, callbacks=[callback])
tip
Set OPENAI_BASE_URL environment variable to avoid hardcoding the endpoint.
Environment Variables
| Variable | Description |
|---|---|
TRACE_AGENT_BASE_URL | Server URL for the callback handler |
OPENAI_BASE_URL | OpenAI-compatible endpoint for local models |
OPENAI_API_KEY | API key (use any value for local models) |
What's Next?
- LangChain Example — Full walkthrough
- Pydantic AI Integration — The equivalent one-line integration for Pydantic AI agents
- OpenTelemetry Ingestion — Trace without TraceAgent code in your agent
- SDK Reference — Manual instrumentation