Observe for LangChain agents
Capture every LangChain run as it actually executes — via LangChain's own callback system, not a network proxy — with the full chain → tool → retriever call tree reconstructed from real run IDs.
Prefactor observes your LangChain agents in real time — every LLM call, tool invocation, and custom span captured as structured trace data the moment it happens, with cost attributed per call and a tamper-evident audit trail of every run.
How to add observe to LangChain
Install the package
Add prefactor-langchain to your project.
Create the middleware
Build it from your config — API URL, token, and agent.
Spans flow to Prefactor
LLM calls, tool invocations, and agent spans arrive as trace data.
# pip install prefactor-langchain
import os
from prefactor_langchain import PrefactorMiddleware
middleware = PrefactorMiddleware.from_config(
api_url="https://app.prefactorai.com",
api_token=os.environ["PREFACTOR_API_TOKEN"], # never hardcode tokens
agent_id="my-agent",
agent_name="My Agent",
)How the LangChain integration actually works
- Attaches via the prefactor-langchain middleware, which registers as a LangChain BaseCallbackHandler — no gateway in your request path, no change to how your chains run.
- LangChain fires every callback with a run_id and parent_run_id; Prefactor uses that parent/child relationship to reconstruct the full call tree — a chain's sub-chains, tool calls, and retriever lookups nested under the run that triggered them, not a flat list of events.
- Capture and sampling are controlled by PREFACTOR_CAPTURE_INPUTS / _OUTPUTS / PREFACTOR_SAMPLE_RATE.
- LangChain runs synchronous callback handlers inline and swallows any exception a handler raises unless that handler sets raise_error — the middleware sets it, so a policy violation raised from on_tool_start genuinely blocks the tool call before it executes, not just logs it afterward.
What Prefactor captures for LangChain agents
LLM calls
Model, prompt, completion, and token usage recorded for every call — not batched, not sampled after the fact.
Tool invocations
Arguments, results, and errors for each tool the agent calls.
Agent spans
The run captured as nested spans you can inspect step by step — plus custom spans for whatever's specific to your own domain.
Cost per call
Token usage rolled up into cost, attributed to the agent, team, and task that spent it — the same span data, not a separate metering system.
Immutable audit trail
Every span and run written once, tamper-evident, full-text searchable, and exportable for compliance review.
Trace anything — not just LLM calls
The SDK captures LLM, tool, and agent spans automatically. With withSpan you wrap any operation in your own span type — an API call, a database query, a quality check, a business action — each with its own payload and schema. It all flows into the same Observe, Evaluate, and cost views.
import { withSpan } from "@prefactor/core";
// Wrap ANY operation in a span you define — an API call, a quality
// check, a business action — with its own spanType, inputs and schema
await withSpan(
{
name: "research competitor",
spanType: "research_competitor",
inputs: { competitor },
},
async () => {
const results = await search(competitor); // your tool / API calls
return summarize(results); // captured as one span
},
);Nest spans to capture business-level actions, and start with permissive schemas you tighten over time. Instrumentation strategy →
An example run, span by span
Illustrative — a single LangChain run as nested spans.
Illustrative example.
Manual logging vs DIY vs Prefactor
| Capability | Manual | DIY OpenTelemetry | Prefactor |
|---|---|---|---|
| LLM / tool / agent spans | Hand-rolled | ✓ build it | ✓ via the SDK |
| Token usage captured per call | — | Build it | ✓ |
| Configurable capture & sampling | — | Partial | ✓ |
| Hosted Admin UI (agents, instances) | — | — | ✓ |
| Risk profiles & audit trail | — | — | ✓ |
LangChain observe — FAQ
What does Prefactor capture for LangChain?+
LLM calls, tool invocations, and agent spans — with inputs, outputs, and token usage — sent to the Prefactor API as structured trace data the moment they happen, not reconstructed from a log afterward.
Is Prefactor in my LangChain request path?+
No — the SDK instruments in-process and sends spans asynchronously. Your requests go straight to your model provider; Prefactor observes what happened alongside it.
How does cost tracking work for LangChain?+
Cost is derived from the token usage captured on each LLM span and rolled up by agent, team, and task — a view over the same trace data, not a separate metering system, so a cost spike traces back to the run that caused it.
Can audit records for LangChain runs be edited or deleted?+
No. Instances and spans are immutable once written, giving reviewers a real-time, queryable record of exactly what every agent did — the evidence a compliance review or an incident investigation actually needs.
How do I add Prefactor to a LangChain app?+
Install prefactor-langchain, create the middleware with PrefactorMiddleware.from_config(...) using your API URL, token, and agent, and add it to your LangChain agent — it registers as a standard LangChain callback handler.
What does Prefactor capture from LangChain?+
Prefactor hooks LangChain's own callback events — on_chain_start/end, on_tool_start/end, on_retriever_start/end, on_llm_start/on_chat_model_start/on_llm_end, and agent actions — and uses each event's run_id and parent_run_id to reconstruct the full call tree as structured, timestamped spans, so every LangChain run is captured as trace data you can reconstruct, search and export end to end.
Does Prefactor add latency or change how LangChain runs?+
No. Observability capture is designed to stay off your agent's critical path, so it doesn't alter your LangChain logic or your users' responses. The only part that acts inline is the optional runtime guardrails you enable per agent — by design, so a high-risk or low-confidence action can be held for human approval before it executes.
Can I evaluate agents built with LangChain and catch regressions?+
Yes. Once runs are captured, eval suites score quality and groundedness on real traffic, drift detection flags behaviour changes after deployment, and versioned eval history catches regressions before they ship — the observe → evaluate → improve loop applied to your LangChain agents.
Keep going
Observe for other frameworks
CrewAI →LangGraph →OpenAI Agents SDK →Claude Agent SDK →Microsoft AutoGen →LlamaIndex →See it on your LangChain agents
Book a 15-minute setup and our team gets you observe in production.