Skip to Content

How do I see token counts for each call in an agent built with LangChain?

Rius shows the token counts for each model call in a LangChain agent, because the LangChain integration records every call as its own span with the model and token usage. You add no spans or attributes yourself. You read the counts in the trace waterfall, in the span detail and on the Spans tab. For the wider picture, see Monitor AI agents in production.

How it works

The LangChain integration hooks LangChain’s callback system. Anything that runs through it is traced: LCEL chains, agents and LangGraph nodes. Each step becomes one span, typed by what the step is:

  • Chains are CHAIN spans.
  • Tool invocations are TOOL spans.
  • Retrievers are RETRIEVER spans.
  • Model calls are LLM spans with the model, token usage and messages.

The spans nest the way your chain is built, so the trace reads like your execution graph. Rius computes the cost on the server from the model and the token counts, so you never send a cost value.

You can combine LangChain with a client integration such as OpenAI. LangChain’s chat model span then wraps the client library’s span, and Rius counts each model call once in token and cost totals. The LangChain span still shows the run’s total. Text-completion models (BaseLLM subclasses, such as langchain_openai.OpenAI) are still counted twice. Still counted twice covers that case and which layer to keep.

Set it up

  1. Install the instrumentation and call init() once, before your chains run. In Python, run pip install "glassflow-rius[langchain]" langchain. The extra installs the instrumentation, not LangChain itself. If LangChain is missing, init() logs a DependencyConflict and the integration stays off.
import rius rius.init(api_key="ri_...", service_name="my-agent") # build and run your chains exactly as before
  1. In TypeScript, run npm install @arizeai/openinference-instrumentation-langchain. The integration also needs @langchain/core, which a LangChain.js application already has. Call const rius = init({ apiKey: 'ri_...', serviceName: 'my-agent' }) and run your chains as before. await rius.ready resolves with the integrations that attached, so it tells you whether langchain is among them.

  2. Run one chain that calls a model, then open the trace in the console. You should see the chain as nested spans, with the LLM span at the leaf carrying model, tokens and cost.

  3. Find the counts. Where they appear is covered in the next section. The Exploring traces page covers each view in full.

There is nothing to add for token counts. The integration records the usage that LangChain reports for each call. You only write your own spans for steps that do not run through LangChain’s callbacks.

What you get

Each view shows the counts at a different level:

  • Waterfall view. Open a trace and the LLM spans show their token counts inline, next to spans badged by kind. The header sums up the trace: duration, span and error counts, total tokens and cost.
  • Span detail. Select an LLM span and its Model section shows the provider, the requested and response model, input and output tokens, and cost.
  • Trace list. Each row has a token total and a computed cost, so you can spot a heavy run before you open it.
  • Spans tab. It lists individual spans. Filter with the All kinds menu set to LLM, then click the Tokens header to sort, which puts the most expensive model calls first. You can also filter by model.
  • Context growth. For a trace with several LLM calls, a strip above the tree plots input tokens per call in order. A runaway prompt or an ever-growing history shows up as rising bars, and you click a bar to select that call.
  • Map view. The Map tab has a tokens lens that colors each node, so you can see where the tokens went across a deep graph.

Costs are list-price estimates in US dollars, not billed amounts. If a call reports no token usage, its cost is blank rather than $0, so it does not look free. Prompts and completions appear on the Input / Output tab. If privacy controls strip content, the tab says so and the tokens and cost still show.

When something else fits

  • If a model call runs outside LangChain’s callback system, this integration does not record it. Check that your calls go through LangChain, or use another integration from the integrations overview.
  • For aggregate token and cost trends over days rather than per-call counts, the usage and cost views give the totals across all traces.

Still stuck? Get help

Last updated on