Skip to Content

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

Rius shows token counts for each AutoGen model call on the ChatCompletion span, which the openai integration records with model, tokens and cost. Install the integration, call rius.init(), and open the trace to read the counts inline in the waterfall and in the span’s Model section. You write no token or cost values yourself.

How it works

AutoGen agents reach the model through the OpenAI client (autogen-ext[openai]), and the openai integration captures each of those calls as a generation. Rius computes cost from the model and the token counts, so you never send a cost value.

The framework adds its own structure. AutoGen emits create_agent, invoke_agent and execute_tool spans through the global tracer provider that rius.init() installs, so agents and tools appear with no wrappers.

The only code of your own is one observe wrapper on the entrypoint. It becomes the root AGENT span and groups agent creation and invocation into a single trace per unit of work.

Do not wrap AutoGen tool functions with observe. The framework runs tools in a detached context, so the wrapper’s span cannot find the run and splits off as its own single-span trace. The execute_tool spans already cover tools.

This is how a run that calls a tool looks in the trace:

handle-ticket AGENT 2.8s ├─ create_agent support_agent 0ms └─ invoke_agent support_agent 2.8s ├─ ChatCompletion LLM 2.6s └─ execute_tool account_status 0ms

The model call is the ChatCompletion span, kind LLM, nested under invoke_agent. That is the span to read for tokens.

Set it up

  1. Install the integration together with AutoGen. The extra installs the instrumentation, not the openai library itself, so openai must already be in your environment.
pip install "glassflow-rius[openai]" "autogen-agentchat" "autogen-ext[openai]"
  1. Call rius.init() before you run the agent, and put one observe wrapper on the entrypoint. The rest of the setup is the SDK’s usual one, covered on the AutoGen integration page.
import rius from autogen_agentchat.agents import AssistantAgent from autogen_ext.models.openai import OpenAIChatCompletionClient from rius import SpanKind rius.init(api_key="ri_...", service_name="my-agent") def lookup(email: str) -> str: return "active" # replace with your own account lookup def account_status(email: str) -> str: """Fetch a customer's account standing by email.""" return lookup(email) @rius.observe(name="handle-ticket", kind=SpanKind.AGENT) async def handle_ticket(ticket: str) -> str: agent = AssistantAgent( name="support_agent", model_client=OpenAIChatCompletionClient(model="gpt-5-mini"), tools=[account_status], system_message="Answer support questions; check accounts with the tool.", ) result = await agent.run(task=ticket) return str(result.messages[-1].content)
  1. Run one request that calls a tool, then open Traces in the console and select the trace. Follow Exploring traces for where each view lives.

  2. Read the counts. In the waterfall, the LLM spans show their token counts inline. Select the ChatCompletion span and the Model section lists provider, requested and response model, input and output tokens, and cost.

  3. To compare calls across runs, open the Spans tab, choose LLM in the All kinds menu, and click the Tokens header to sort.

What you get

  • Tokens on every model call. Each ChatCompletion span carries the input and output tokens for that call, with the model and cost beside them.
  • Totals for the whole trace. The trace header shows total tokens and cost, and each row in the trace list shows the token total and computed cost.
  • Context growth across calls. For a trace with several LLM calls, a strip above the tree plots input tokens per call in order. A prompt that keeps growing shows up there, and clicking a bar selects that call.
  • What changed between calls. On an LLM span with captured messages, Since previous llm call on the Input / Output tab shows what each step added to the prompt.
  • Cost as an estimate. Cost is a list-price estimate in US dollars. A call that reported no token usage shows a blank cost rather than $0, so it does not look free.
  • Nothing double counted. Your observe root carries no usage of its own, so each call’s tokens and cost are counted once, on its ChatCompletion span. Do not add a manual generation around a call the integration already records, or it is counted twice.

For how these per-call numbers fit with alerts, agents and the other views, see Monitor AI agents in production.

When something else fits

  • The openai integration captures calls made through the OpenAI client. If your AutoGen agent uses a different model client, check the Integrations page for the integration that matches the library you call.
  • The AutoGen integration is Python only today. For a streamed OpenAI call, token usage appears only when stream_options sets include_usage; the Integrations page covers this and the other known limitations.

Still stuck? Get help

Last updated on