How do I see token counts for each call in an agent built with the OpenAI Agents SDK?
Rius shows the token counts of every model call an OpenAI Agents SDK agent makes, with no framework-specific code. The framework drives the OpenAI client underneath, so the Rius openai integration records each call as a generation with model, input and output tokens, cost and time to first token. Open the trace and the span for each model call shows its tokens inline. You add your own wrappers only for the run and tool structure around those calls.
How it works
- Model calls need nothing of your own. The
openaiintegration captures each call the framework makes, so every one arrives as anLLMgeneration with model, tokens, cost and time to first token. The token counts do not depend on any wrapper you write. - Run structure needs one line each. The agent loop, tool executions and handoffs run inside the framework, so auto-instrumentation only sees the client calls that come out of it. Wrap the entrypoint around
Runner.run()withobserveand kind AGENT to get a root span. Wrap each tool body withobserveand kind TOOL to get a named tool span. - The framework’s own tracing is separate. It uploads to the OpenAI platform and is independent of OpenTelemetry. Call
set_tracing_disabled(True)if Rius should be your only telemetry path. Leaving it on does not interfere with Rius. - Handoffs have no span of their own. A handoff shows only as consecutive generations that belong to different agents.
- Streamed runs need usage on the client. A streamed call carries token counts only when usage is enabled on the underlying OpenAI client (
include_usage). Without it the call has no tokens and so no cost.
Set it up
- Install the SDK with the
openaiextra, plus the framework. The extra installs the instrumentation, not the OpenAI library itself.
pip install "glassflow-rius[openai]" openai-agents- Call
rius.initbefore you run the agent, turn off the framework’s own tracing if you want a single path, and wrap the entrypoint and the tool bodies. Put@function_tooloutermost andobserveon the function body, because the framework reads the function signature for its tool schema.
import rius
from agents import Agent, Runner, function_tool, set_tracing_disabled
from rius import SpanKind
rius.init(api_key="ri_...", service_name="my-agent")
set_tracing_disabled(True)
def lookup(email: str) -> str:
return "active" # replace with your own account lookup
@function_tool
@rius.observe(kind=SpanKind.TOOL)
def account_status(email: str) -> str:
"""Fetch a customer's account standing by email."""
return lookup(email)
support_agent = Agent(
name="support_agent",
instructions="Answer support questions; check accounts with the tool.",
tools=[account_status],
model="gpt-5-mini",
)
@rius.observe(name="handle-ticket", kind=SpanKind.AGENT)
def handle_ticket(ticket: str) -> str:
result = Runner.run_sync(support_agent, ticket)
return str(result.final_output)- Run one request that calls a tool.
- Open the trace in the console. You should see the root
AGENTspan, theLLMgenerations with model, tokens and cost, and your namedTOOLspans.
The other integrations follow the same pattern, and the OpenAI Agents SDK page covers sessions and the blind spots in full.
What you get
Token counts show in several places in the console, all described in Exploring traces:
- Waterfall. Open a trace and the spans appear as a tree on a timeline. Each
LLMspan shows its token counts inline, and the header sums the whole trace: duration, total tokens and cost. - Span detail. Select a model-call span and its Model section shows provider, requested and response model, input and output tokens, and cost. Cost is a list-price estimate in US dollars.
- Context growth. For a trace with several model calls, a strip above the tree plots input tokens per call in order. A prompt that keeps growing across the agent loop shows up here first. Click a bar to select that call.
- Spans tab. Pick LLM in the All kinds menu to list only model calls, then click the Tokens header to sort by token count.
- Keyboard. Press
]lto jump to the next LLM span.
A call that reported no token usage shows a blank cost rather than $0, so it never looks free. In the run structure, each model call is named Response, so the span name does not say which agent spoke. Wrapping each agent’s entry in observe adds that context. For the wider picture across runs, see Monitor AI agents in production.
When something else fits
- If a streamed run shows no tokens, enable usage on the underlying OpenAI client. Without it the call has no token counts and no cost.
- If handoff boundaries matter to you, add an
observewrapper for each agent entry, or use the framework’sRunHooksto open spans at agent boundaries. - This integration covers Python. Rius has no integration for this framework in TypeScript today.