Skip to Content

My LangChain agent suddenly costs more. Why?

Rius shows why a LangChain agent got more expensive by pricing every model call and rolling the cost up per trace, per agent and per model. Start in the Spans tab, sort by Cost, and compare a recent expensive trace with an older cheap one to see whether calls got bigger, more numerous or moved to a pricier model. A custom cost alert then tells you the next time it happens.

How it works

The LangChain integration hooks LangChain’s callback system, so chains, agents and LangGraph nodes arrive as one nested trace. Each step is one span: chains as CHAIN, tools as TOOL, retrievers as RETRIEVER, and model calls as LLM spans with the model, token usage and messages. Rius computes cost from the requested model and the token counts, so every model call carries its own cost. Cost is then summed per trace, per agent and per workspace. Read the spans against these causes:

  • Bigger calls. A prompt or conversation history that grows raises input tokens on every call.
  • More calls. A loop, retry or extra tool round adds LLM spans to each run.
  • A different model. Each model has its own price, so a switch moves the cost even when token counts stay flat.
  • Newly priced calls. A call whose model is not in the price catalog has an empty cost, not zero. When the catalog gains that model, new calls start adding cost to your totals.
  • Calls counted twice. Model calls are counted once in tokens and cost, but text-completion models (BaseLLM subclasses, such as langchain_openai.OpenAI) are still counted twice. See Still counted twice for that case and which layer to keep.

Costs are list-price estimates in US dollars, so use them to see the change, not to match an invoice. The LangChain integration page lists what the integration records, and Track LLM and agent cost explains how a call is priced.

Set it up

  1. Check that the LangChain integration is on, so every step and model call is recorded. The integration page covers the full setup.

    import rius rius.init(api_key="ri_...", service_name="my-agent") # build and run your chains exactly as before

    In TypeScript, await rius.ready resolves with the integrations that attached, so you can confirm langchain is among them. All integrations are listed under Integrations.

  2. Open Traces and set the time range so it covers the jump, for example 7d. The strip above the list sums count, tokens and cost for the traces that match your filters, and each row shows its own cost. Filter to your agent and compare an expensive trace with an older cheap one.

  3. Switch to the Spans tab, choose LLM in the All kinds menu and click the Cost header to sort. This shows which model and which calls cost the most. Select a span to see its provider, model, input and output tokens and cost. See Exploring traces for the filters and keys.

  4. Open the expensive trace. On a trace with several LLM calls, the context growth strip plots input tokens per call in order. A climbing bar means history or retrieved text keeps piling into the prompt. On a captured LLM span, Since previous llm call on the Input / Output tab marks what each step added to the prompt.

  5. Check for retries. Back-to-back calls of the same tool or step with the same input collapse into one row such as fetch_article · 3 attempts · recovered, so a retry loop reads as one event. The Map tab has a cost lens that shows where the money went in one run.

  6. Use Compare runs on the open trace to see which steps changed, which were added or dropped, and where cost moved against another run of the same agent. Version Compare and Drift on the Agents page show cost per trace over time and across versions.

  7. Add a custom alert on cost so you hear about the next jump. Cost is measured per trace, and you can scope it by service, agent or model:

    FILTERS Service = my-agent RULES p95(Cost) > $2.00 WINDOW 1 hour

    Replace the service name and threshold with your own. This catches runs that got expensive, such as a prompt that grew or an agent stuck retrying.

What you get

  • A cost on every model call in the trace, so you can point at the span where the spend went.
  • A per-trace, per-agent and per-model view of the change, with cost by model on the Usage and cost page showing whether a model switch is behind it.
  • Context growth and retry chains in the trace that separate “prompts got bigger” from “the agent called more”.
  • A cost alert scoped to your service or agent that fires when p95 cost per trace crosses your threshold. The Tool Loop alert, one of the pre-defined alerts, also watches TOOL spans, which the LangChain integration records for tool invocations.
  • The same questions answerable in plain language once you connect an MCP client, as described in Monitor AI agents in production.

When something else fits

  • If a call shows an empty cost, its model is not in the price catalog or the call reported no token usage, so look at tokens and timings instead of cost.
  • If you need a total-spend or budget threshold for the whole workspace, watch the Usage and cost page, because workspace-wide spend is not an alert metric.
  • If you need the cost to match your invoice, compare against your provider’s billing, since Rius uses standard list prices and ignores negotiated discounts.

Still stuck? Get help

Last updated on