My Claude Agent SDK agent makes more LLM calls than usual. Why?
In Rius, a Claude Agent SDK task shows one LLM span for each turn your code wraps, so more LLM calls means your code opens more turns per task, or more tasks run. The runtime’s own steps are not visible as separate spans, so extra work inside one turn shows up as larger tokens, cost and duration on that span. Start in the Spans tab filtered to LLM, then add a custom alert so the next jump is caught early.
How it works
The Claude Agent SDK drives the Claude Code runtime in a separate process, so no model client runs in your process. Rius coverage is manual: an observe wrapper makes each task the root AGENT span, and a manual generation wraps the query() loop and is filled from the run’s ResultMessage with tokens, the real model id and the answer. Cost is then computed by Rius, as for any generation. A task looks like this:
handle-task AGENT 5.0s
└─ claude_agent_turn LLM 5.0s claude-haiku-4-5, tokens + costThat shapes how you read the symptom:
- Count of LLM spans. Each generation is one LLM call in Rius. If the count per task rises, your code is wrapping more
query()loops per task, retrying, or looping over the task more often. - Size of one LLM span. Tool calls, sub-turns and file operations happen inside the Claude Code process and produce no spans. If the runtime makes more model calls inside one turn, you do not see more spans. You see higher tokens, cost and duration on the turn, because the generation records the turn’s totals.
- Other model calls. A call made through a model client in your own process is traced by its integration as its own LLM span.
Set it up
- Make sure each task is traced as in the Claude Agent SDK integration: an
observeroot with kindAGENT, and one generation perquery()loop with a full model id such asclaude-haiku-4-5. Scope each conversation withrius.session()so its traces group under one session. - Open Traces in the console, set the time range to cover the change, and open a slow or costly trace. In the waterfall, count the LLM spans under the root. Several
claude_agent_turnrows under one task means your code opened several turns. - Switch to the Spans tab and pick LLM in the All kinds menu. Click the Tokens, Cost or Duration header to sort, and find the largest turns. Pasting a session ID into the search box lists every span in that session.
- In a trace with several LLM calls, read the context growth strip above the tree. It plots input tokens per call in order, so a growing history or a runaway prompt shows up first. On a call’s Input / Output tab, Since previous llm call shows what each step added to the prompt.
- Use Compare runs on the open trace against a normal run of the same agent. It shows which steps were added or dropped and where cost or latency moved. See Exploring traces for all of these views.
- Create a custom alert on the LLM calls metric, filtered to your service. Pick a threshold from what a normal window looks like for you:
FILTERS Service = my-agent
RULES count(LLM calls) > 50
WINDOW 15 minutesBecause a Claude Agent SDK turn is one LLM span, this rule counts turns your code opens, not model calls inside the runtime. To catch the runtime doing more work inside a turn, add a second alert on a numeric metric such as Cost, Input tokens or Duration. Rules in one alert combine with AND, so create it as its own alert. For more on spotting problems like this across agents, see Monitor AI agents in production.
What you get
- A clear split between two causes: more LLM spans per task (your code), or heavier single turns (the runtime).
- A sortable list of LLM spans by tokens, cost and duration, so the biggest turns stand out.
- Input tokens per call in order, and a diff of what each step added to the prompt.
- Cost per turn, computed by Rius as a list-price estimate in US dollars from the model and token counts.
- A custom alert that tells you when LLM calls, cost or tokens cross a threshold you chose.
When something else fits
- To cap the runtime’s own loop, set
max_turnsonClaudeAgentOptions. A run that hits the cap ends with aResultMessagewhoseis_erroris set rather than an exception, so raise on it inside the generation if you want those runs to read as failed. - If you need to see which tools or sub-turns the runtime ran, Rius cannot show them for this framework today, because that work produces no spans. Use the
claudeCLI’s own output for that detail. - If you want a coding session traced rather than your own agent, the Claude Code plugin records tool calls and model calls inside a session. See Integrations for the other frameworks Rius covers with automatic tracing.