Skip to Content
Rius

How do I trace LangChain in Python?

Rius traces LangChain in Python with one extra install and one init() call. Install glassflow-rius[langchain], call rius.init() at the start of your process, and run your chains as before. Each chain, tool call, retriever and model call arrives as a nested span, and Rius computes the cost of each model call from its token counts.

How it works

The LangChain integration hooks LangChain’s callback system. Anything that runs through callbacks is traced, including LCEL chains, agents and LangGraph nodes. You do not wrap your chains or add decorators.

With a default (global) init(), every installed integration turns on automatically, so installing the extra is all the setup the integration needs. The extra installs the instrumentation for LangChain, never langchain itself. A project that already calls LangChain has it, and pip leaves it untouched. If the extra is installed without langchain, init() logs a DependencyConflict and the integration stays off.

The Python SDK requires Python 3.10 or newer. The full list of init() arguments and environment variables is on Installation and configuration, and the support matrix for all frameworks is on Integrations.

Set it up

  1. Install the extra, and name langchain as well if you are starting from a fresh environment:
pip install "glassflow-rius[langchain]" langchain
  1. Call init() once, as early as possible in your process. Set service_name explicitly, because it is how you find your agent in trace filters and cost breakdowns later. The API key starts with ri_.
import rius rius.init(api_key="ri_...", service_name="my-agent") # build and run your chains exactly as before
  1. Run one chain that calls a model, using your own code unchanged. That single invocation is your minimal traced call. A chain, a LangGraph graph or an agent all work the same way.

  2. Open the trace in the console. You should see the chain’s structure as nested spans, with the model call at the leaf carrying the model, token counts and cost.

If you also install a client extra such as openai, LangChain’s chat model span wraps the client library’s span, so you get framework context and client-level detail, nested. With glassflow-rius 1.1.0 or later, each model call is counted once in token and cost totals, and the LangChain span still shows the run’s total. The page for this integration, LangChain, covers the combined setup in more detail.

What you get

  • One span per step, typed by what the step is: chains as CHAIN spans, tool invocations as TOOL spans, retrievers as RETRIEVER spans and model calls as LLM spans.
  • Model details on each LLM span: the model, token usage and messages. Rius computes the cost on its side from the model and token counts, so you never send a cost value. Costs are list-price estimates in US dollars.
  • A trace that reads like your graph, because the nesting follows your chain structure.
  • Content on every step: step inputs, outputs and messages are content attributes. The privacy controls apply to the whole tree, so you can strip or mask content at export.

One known gap: text-completion models, meaning BaseLLM subclasses such as langchain_openai.OpenAI, are still counted twice when a client extra is also installed. The LangChain page links to the cases and which layer to keep.

When something else fits

  • If you use LangChain.js, the TypeScript SDK has its own integration, installed with an npm package instead of a Python extra.
  • If part of your run does not go through LangChain callbacks, add your own spans with observe around that code.
  • If you only need model-call tracing with no framework context, a client extra such as openai or anthropic alone is enough.

Still stuck? Get help

Last updated on