How do I trace LangChain in TypeScript?
Rius traces LangChain in TypeScript when you install the LangChain instrumentation package and call init(), with no changes to your chains. Rius hooks LangChain’s callback system, so chains, tools, retrievers and model calls arrive as a nested trace that mirrors your graph. Each model call carries its model, token usage and messages, and Rius computes the cost.
How it works
init()finds@langchain/corein your app, resolves its callback manager and patches it. You pass nothing toinit()for this.- Anything that runs through LangChain’s callbacks is traced, which includes LCEL chains, agents and LangGraph nodes.
- The integration does not use OpenTelemetry’s CommonJS module hook, so it works the same in a pure-ESM application as in CommonJS.
init()is synchronous, but auto-instrumentation attaches asynchronously.await rius.readyresolves with the names of the integrations that attached, so you can check thatlangchainis among them. It never rejects.- With a global
init(), every installed integration turns on automatically. From@glassflow-ai/rius1.8.0,init({ instruments: ['langchain'] })limits it to LangChain, andinstruments: []turns them all off.
The LangChain integration page covers this integration in full. The Integrations overview lists the other supported libraries and frameworks.
Set it up
- Install the SDK, its OpenTelemetry peer dependency and the LangChain instrumentation. The SDK needs Node 18 or newer.
@langchain/coreis already in any LangChain.js application, and@langchain/core1.x needs Node 20 or newer. The full SDK install and every option are on the installation page.
npm install @glassflow-ai/rius @opentelemetry/api
npm install @arizeai/openinference-instrumentation-langchain- Call
init()once, as early as possible, before you build or run your chains. Then run your chains exactly as before. A short script also needsawait rius.shutdown()at the end, because the TypeScript SDK does not flush spans when the process exits.
import { init } from '@glassflow-ai/rius'
const rius = init({ apiKey: 'ri_...', serviceName: 'my-agent' })
const attached = await rius.ready // includes 'langchain' when the integration attached
// build and run your chains exactly as before
await rius.shutdown() // export queued spans before the process exits- Run one chain that calls a model, then open the trace in the console. You should see the chain’s structure as nested spans, with the model call at the leaf.
If langchain is missing from attached, check that @arizeai/openinference-instrumentation-langchain and @langchain/core are both installed in the project that runs your code.
What you get
- One span per step, typed by what the step is: chains, tool invocations, retrievers and model calls.
- Model-call spans with the model, token usage and messages. Rius computes the cost from the model and token counts, as a list-price estimate in US dollars.
- Nesting that follows your chain structure, so the trace reads like your execution graph.
- Step inputs, outputs and messages as content. Set
RIUS_CAPTURE_CONTENTtofalseto strip prompt and response content at export while the structure, tokens and timing still flow. - Correct totals when you also use a client library such as OpenAI. LangChain’s chat model span wraps the client span, and with
@glassflow-ai/rius1.2.0 or newer each model call is counted once in tokens and cost. The LangChain span still shows the run’s total.
When something else fits
- If you want only model-call detail and no chain structure, instrument the client library directly, such as OpenAI or Anthropic. Each has its own page in Integrations.
- If your app uses a framework with no TypeScript integration, such as LlamaIndex or LiteLLM, trace those calls with manual generations instead.
Related
Last updated on