Trace LangChain LLM calls, tool invocations, chain executions, and retriever queries with a single callback handler.
npm install @openlantern-ai/sdk langchain @langchain/coreNo additional integration package is needed — the LangChain collector is included in @openlantern-ai/sdk.
import { LanternTracer, createLanternCallbackHandler } from "@openlantern-ai/sdk";
const tracer = new LanternTracer({
apiKey: process.env.LANTERN_API_KEY,
baseUrl: process.env.LANTERN_BASE_URL,
});
const handler = createLanternCallbackHandler(tracer, {
agentName: "my-langchain-agent", // optional, defaults to "langchain-agent"
});import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
modelName: "gpt-4",
callbacks: [handler],
});
const response = await model.invoke("Explain quantum computing");import { RetrievalQAChain } from "langchain/chains";
const chain = RetrievalQAChain.fromLLM(model, retriever, {
callbacks: [handler],
});
const result = await chain.invoke({ query: "What is Lantern?" });If you already have an active trace (e.g., from a parent request), pass its ID to avoid creating a new one:
const handler = createLanternCallbackHandler(tracer, {
traceId: existingTraceId,
agentName: "sub-chain",
});| LangChain Event | Lantern Span Type | Captured Data |
|---|---|---|
handleLLMStart / handleLLMEnd |
llm_call |
Model name, prompt text, response text, token usage |
handleToolStart / handleToolEnd |
tool_call |
Tool name, input arguments, output |
handleChainStart / handleChainEnd |
custom |
Chain inputs and outputs |
handleRetrieverStart / handleRetrieverEnd |
retrieval |
Query text, document count |
Any *Error handler |
Sets error on span | Error message |
The handler automatically manages trace lifecycle — if no traceId is provided, it creates a trace on the first event and ends it when all active spans complete.
Parent-child relationships are preserved. When LangChain reports a parentRunId, the corresponding Lantern span is nested under the parent span, giving you a full execution tree in the dashboard.
Spans not appearing
- Verify the handler is passed in the
callbacksarray, not as a single object. - Check that
LANTERN_API_KEYandLANTERN_BASE_URLare set correctly.
Missing token counts
- Token usage depends on the LLM provider returning
tokenUsagein its response. Not all providers include this data.
Duplicate traces
- If you pass the handler to multiple components in the same request, each will create its own trace unless you provide a shared
traceId.
function createLanternCallbackHandler(
tracer: LanternTracer,
opts?: {
traceId?: string;
agentName?: string;
}
): LangChainCallbackHandler;| Parameter | Type | Description |
|---|---|---|
tracer |
LanternTracer |
A configured Lantern tracer instance |
opts.traceId |
string |
Optional — attach spans to an existing trace |
opts.agentName |
string |
Optional — name shown in dashboard (default: "langchain-agent") |