Skip to Content

How do I see token counts for each call to LiteLLM?

Rius records token counts for every call you route through LiteLLM in Python, with no code beyond rius.init(). Each completion becomes its own span with the model, input and output tokens and a computed cost. You read the counts inline in the trace waterfall, in the span’s Model section, and in the Tokens column of the Spans tab. You only add your own span if you want several calls grouped under one run.

How it works

The litellm integration is turned on automatically by a global init() once the extra is installed. It is Python only, because the LiteLLM library is Python only.

For each routed completion, the integration records:

  • the model, stored without LiteLLM’s routing prefix when the prefix names the provider, so openai/gpt-4o-mini lands as gpt-4o-mini with the provider openai
  • the token usage
  • the messages

Cost is computed by Rius from the model and the tokens. You never send it.

LiteLLM calls OpenAI and OpenAI-compatible providers through the openai client library. If the openai extra is installed too, that client call appears as a second LLM span under the LiteLLM span. The call is still counted once in token and cost totals, and the LiteLLM span shows the run’s total. A few setups, such as streamed calls on some instrumentation versions, are still counted twice. See Still counted twice for the cases and which layer to keep.

From TypeScript there is no LiteLLM integration. If you call a LiteLLM proxy over its OpenAI-compatible endpoint with the OpenAI client, the OpenAI integration traces those calls. The model recorded is the one you ask the proxy for, not the provider it routes to. If you run LiteLLM as a proxy server, the proxy is a separate process, so instrument the proxy itself or send its OpenTelemetry traces to Rius.

Set it up

  1. Install the instrumentation and LiteLLM. The extra installs the instrumentation, not litellm itself. Without litellm in the environment, init() logs a DependencyConflict and the integration stays off.
pip install "glassflow-rius[litellm]" litellm
  1. Call init() and make a completion. Every routed call is traced from here on. Full setup is on the LiteLLM integration page.
import rius import litellm rius.init(api_key="ri_...", service_name="my-agent") response = litellm.completion( model="gpt-4o-mini", # or any provider LiteLLM routes to messages=[{"role": "user", "content": "hi"}], )
  1. Add your own span only when you want structure. The integration records model calls and nothing around them. If your agent makes several completions in one run, wrap the function with @rius.observe so the calls sit under one parent span in the waterfall. A bare observe makes a chain span named after the function. Each completion inside it keeps its own token counts.

  2. Open the trace in the console and read the counts where you need them:

    • Waterfall: in the trace’s tree view, LLM spans show their token counts inline, and the header shows the total tokens and cost for the whole trace.
    • Model section: select an LLM span to see the requested and response model, input and output tokens, and cost.
    • Spans tab: pick LLM in the All kinds menu to list only model calls, and click the Tokens header to sort by count.

For a trace with several LLM calls, a strip above the tree plots input tokens per call in order. Click a bar to select that call. The trace exploration guide covers every view.

What you get

  • Input and output token counts on the span for each LiteLLM completion, whichever provider LiteLLM routes to.
  • A cost for each call and a total on the trace. Costs are list-price estimates in US dollars.
  • One integration for every provider behind the router, so switching providers keeps your tracing unchanged.
  • Token totals that count each call once, even when the openai client call underneath is traced too.
  • A blank cost, not $0, on a call that reported no token usage, so it does not look free.
  • A per-call input token strip that shows where a prompt grows across a run.

To see how these counts fit into watching a whole agent in production, read Monitor AI agents in production.

When something else fits

  • If you call providers directly rather than through LiteLLM, use the integration for that client library, such as OpenAI or Anthropic, listed in the integrations overview.
  • If you run LiteLLM as a proxy server, instrument the proxy process or send its OpenTelemetry traces to Rius, because the Python integration only covers the library in your own process.

Still stuck? Get help

Last updated on