How do I see token counts for each call in an agent built with the Claude Agent SDK?
Rius shows token counts for a Claude Agent SDK task on a generation span that you fill from the run’s ResultMessage. Each task gets input, output and cache tokens plus a computed cost. You see them inline in the trace waterfall and in the span’s Model section.
How it works
The Claude Agent SDK drives the Claude Code runtime in a separate process, so no model client runs in your process and auto-instrumentation has nothing to hook. Rius covers this framework with two pieces you add yourself:
- An
observewrapper withkind=SpanKind.AGENTmakes each task the root span. - A manual generation wraps the
query()loop. When the run’sResultMessagearrives, you copy its token usage into the generation withset_usage, set the real model id withset_response_model, and set the collected answer as the output.
Rius computes the cost on its side from the model and the token counts. You never send a cost value. Costs are list-price estimates in US dollars.
The generation is one span for the whole runtime turn. It records that turn’s totals, not a count for each step inside the runtime. Tool calls, sub-turns and file operations happen inside the Claude Code process and produce no spans. So the unit you see is one generation per query() run, and one trace per task.
Two details keep the numbers right:
- Anthropic reports an
input_tokensthat excludes cache reads and writes. Because the generation’s provider isanthropic,set_usage()adds the two cache counts toinput_tokensfor you. Pass the ResultMessage’s cache counts as they are and keepprovider="anthropic". Without the cache fields, a cache-heavy run would undercount badly: a one-turn run can report 10 input tokens next to 24,000 cache tokens. - Use a full model id such as
claude-haiku-4-5, not an alias such ashaiku. Cost is computed from the requested model, and the pricing table matches real model ids only, so an alias gives a generation with no cost.
Set it up
- Install the SDK next to the Claude Agent SDK. The base package is enough, and no extra applies. The
claudeCLI must be installed and authenticated. The full install options are on the Claude Agent SDK integration page.
pip install glassflow-rius claude-agent-sdk- Initialize Rius, wrap each task in
observeas an AGENT span, and wrap thequery()loop in a generation. Fill the usage from theResultMessage:
import rius
from claude_agent_sdk import (
AssistantMessage, ClaudeAgentOptions, ResultMessage, TextBlock, query,
)
from rius import SpanKind
rius.init(api_key="ri_...", service_name="my-agent")
MODEL = "claude-haiku-4-5"
@rius.observe(name="handle-task", kind=SpanKind.AGENT)
async def handle_task(task: str) -> str:
options = ClaudeAgentOptions(model=MODEL, max_turns=1)
parts = []
with rius.start_as_current_generation(
"claude_agent_turn", model=MODEL, provider="anthropic", input=task
) as gen:
async for message in query(prompt=task, options=options):
if isinstance(message, AssistantMessage):
parts.extend(
b.text for b in message.content if isinstance(b, TextBlock)
)
elif isinstance(message, ResultMessage):
usage = message.usage or {}
gen.set_usage(
input_tokens=usage.get("input_tokens"),
output_tokens=usage.get("output_tokens"),
cache_read_input_tokens=usage.get("cache_read_input_tokens"),
# Anthropic's payload still calls the write count "cache creation"
cache_write_input_tokens=usage.get("cache_creation_input_tokens"),
)
models = list((message.model_usage or {}).keys())
if models:
gen.set_response_model(models[0])
answer = "".join(parts)
gen.set_output(answer)
return answer-
Run one task, then open the trace in the console. You should see the root AGENT span and the generation with a full model id, tokens and cost. A generation without cost means an alias was used as the model id.
-
To group a conversation, scope each task with
rius.session(). The Claude Agent SDK’s ownsession_idarrives on messages only after the task’s root span has started, so use your own conversation id, or reuse thesession_idfrom an earlierResultMessagewhen you resume that conversation.
What you get
A task traces like this:
handle-task AGENT 5.0s
└─ claude_agent_turn LLM 5.0s claude-haiku-4-5, tokens + costThe Traces page shows the token counts in several places:
- Trace list. Each row carries a token total and a computed cost, and the strip above the list sums tokens and cost for the traces that match your filters.
- Waterfall. The generation span is badged as an LLM span and shows its token counts inline. The trace header shows total tokens and cost.
- Span detail. Select the generation to open its Model section: provider, requested and response model, input and output tokens, and cost. If the model’s context window is known, the span also shows how much of it was used.
- Spans tab. Filter with the All kinds menu set to LLM and sort by the Tokens or Cost header to find your heaviest runs.
If the run reports no token usage, the cost shows blank rather than $0, so it does not look free. For monitoring these runs across a whole agent, see Monitor AI agents in production.
When something else fits
- If you need a span for each tool call or sub-turn inside the Claude Code runtime, this setup does not give it. The runtime’s inner work produces no spans, and you see the turn’s totals only.
- If you want a token count for every individual model call, call the model through a client that has an integration, such as Anthropic or OpenAI. Each call then arrives as its own generation with tokens and cost. See the integrations overview.