My OpenAI Agents SDK agent suddenly costs more. Why?
Rius shows what drove a cost jump in your OpenAI Agents SDK agent on the spans it records: a model call span with model, tokens and cost, and tool spans from your own wrappers. Start with the most expensive traces, then check four things: prompt size per call, the number of model calls per run, the model used, and repeated tool calls. A cost alert on cost per trace then flags the next jump.
How it works
Rius prices each model call when it arrives, from the requested model and its token counts, and rolls the cost up per trace and per agent. The OpenAI Agents SDK drives the OpenAI client underneath, so the OpenAI integration records every model call as a generation with model, tokens and cost. Your observe wrappers add the run and its tools. A trace for one ticket looks like this:
- A root span of kind AGENT, from your wrapper around
Runner.run(). - One LLM span per model call, named
Response, with model, tokens and cost. - One TOOL span per function tool, from the
observewrapper on the tool body. - No span for a handoff. It shows only as consecutive model calls that belong to different agents.
A cost increase comes from one of a few causes, and each one leaves a different mark on those spans:
- Longer prompts. Input tokens per call climb as the conversation history grows.
- More model calls per run. The trace has more
Responsespans than before. - A different model. The span detail shows the requested model, and cost by model on the usage and cost page shows where spend moved.
- A tool that keeps retrying. The same TOOL span repeats, and each retry usually triggers another model call.
Costs are list-price estimates in US dollars from LiteLLM’s public model-price catalog. A call with an unknown model or no token usage has an empty cost, not zero, so totals add up only the calls that were priced. The track LLM and agent cost guide covers how pricing works.
Set it up
- Make sure the spans exist. Install the OpenAI integration and wrap the entrypoint and each tool, with the TOOL kind on tools. The kind matters later: Rius’s Tool Loop alert counts only TOOL spans, so a tool wrapped with a bare
observeis never counted. Put@function_tooloutermost andobserveon the function body. The OpenAI Agents SDK integration page has the full setup.
pip install "glassflow-rius[openai]" openai-agentsimport 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)- Open Traces and set the time range to cover the jump. Each row shows cost, token total and span count, and the strip above the list sums cost for the filters you set. Rows with unusually high cost or span count are your suspects. The exploring traces page covers the list and filters.
- Switch to the Spans tab, pick LLM in the All kinds menu and click the Cost header. The priciest single calls come first, with their model and tokens.
- Open the most expensive trace. Above the tree, the context growth strip plots input tokens per model call in order. A staircase means the history grows every turn. Select a call and use Since previous llm call on the Input / Output tab to see which messages were added.
- Open the Map tab and choose the cost lens to see which step holds the money. Back in the tree, a retry loop on a tool collapses into one row, such as
account_status · 3 attempts · recovered. - Compare the expensive trace against a normal run of the same agent with Compare runs, to see which steps were added and where cost moved.
- Set an alert so the next spike reaches you. A custom alert measures cost per trace over a rolling window:
FILTERS Service = my-agent
RULES p95(Cost) > $2.00
WINDOW 1 hourChoose your own threshold from a normal day’s cost per trace.
What you get
- A clear split between a prompt that grew, extra model calls, a model change and a looping tool, read from spans instead of guessed.
- Cost per call and per trace, plus cost by model over the period against the previous one.
- An alert on p95 cost per trace, scoped to your service, that fires when runs get expensive.
- The pre-defined Tool Loop alert, which sees your tools because they carry the TOOL kind.
- Cost per trace over time and side by side across versions on the Agents page, to tie a jump to a release.
For how this fits with the rest of production monitoring, see Monitor AI agents in production.
When something else fits
- If you need to know which agent in a handoff made an expensive call, add an
observewrapper per agent entry, or use the framework’sRunHooks.Responsespans are generic names, and handoffs have no span. - A total-spend or budget threshold for the whole workspace is not an alert metric. Watch that on the usage and cost page, and use cost per trace for alerts.
- Rius costs are list-price estimates, so an invoice with negotiated or batch pricing will differ. Use your provider’s invoice for exact billing. Other libraries are listed on the integrations page.