Observability

What Is LLM Cost?

The per-trace dollar spend created by LLM inference, tokens, retries, routing, tool calls, and evaluator calls.

What Is LLM Cost?

LLM cost is the dollar spend created by model inference in production LLM and agent systems. It is an observability metric because the useful view is not just the provider invoice; it is cost per trace, route, user, feature, retry chain, and successful task. LLM cost shows up on production traces and gateway routing decisions through token counts, cache discounts, model prices, and retries. FutureAGI measures it with traceAI token attributes and Agent Command Center routing data.

Why LLM Cost Matters in Production LLM and Agent Systems

Cost becomes a reliability issue when one user request fans out into planner calls, retrieval calls, tool calls, critic calls, retries, and post-run evaluation. A single chat completion may be cheap; a 12-step agent trace using a reasoning model, a large context window, and two judge checks can burn the same budget as hundreds of simple turns. Ignore that, and the failure mode is not only a larger invoice. You get hidden margin loss, throttled traffic, and budget alarms that trigger after the production incident is already expensive.

The pain lands in different places. Developers see slower traces but cannot tell whether latency came from input-token growth, retries, or a model route change. SREs see provider rate-limit errors because tokens per minute spiked, not request count. Product teams lose feature-level unit economics. Finance sees an API bill with no link back to prompt version, customer tier, or route.

Common symptoms are gen_ai.usage.input_tokens jumping after a retrieval change, gen_ai.cost.total clustering around one agent step, cache-read tokens falling after a prompt edit, and p95 cost per trace rising while task-completion rate stays flat. In 2026 multi-step pipelines, cost also compounds across hidden work: routing fallback, semantic-cache misses, tool retries, and evaluator calls. That is why LLM cost needs trace-level observability instead of monthly spreadsheet review.

How FutureAGI Handles LLM Cost

FutureAGI’s approach is to put cost next to the work that caused it. traceAI integrations such as traceAI-litellm, traceAI-openai, and traceAI-langchain capture token fields on each LLM span, including llm.token_count.prompt, llm.token_count.completion, gen_ai.usage.input_tokens, and gen_ai.usage.output_tokens. The platform then computes gen_ai.cost.total from the routed model, current provider price, cache discount, and retry count.

In Agent Command Center, the same trace data feeds gateway routing. A production support agent might have three routes: a low-cost model for simple account questions, a stronger model for policy exceptions, and a fallback route for tool errors. The engineer can watch cost per successful task by route, then update routing policy: cost-optimized only when quality stays above the eval threshold. If a semantic-cache miss rate rises after a prompt template edit, the gateway can restore the prior prompt version or send high-cost traffic through a cheaper route while the regression is investigated.

Unlike OpenAI’s usage dashboard, which is useful for account-level spend, FutureAGI ties spend to the trace, route, prompt version, session, and agent step. That matters during incidents: a dashboard can show that one agent.trajectory.step is creating most of the spend, while the route view shows whether model fallback or retries amplified it. The next action is concrete: set a cost-per-trace alert, cap max retries, tune context size, or run a regression eval before shipping the prompt again.

How to Measure or Detect LLM Cost

Track cost at span, trace, and route level. The minimum signals are:

  • Token inputs: gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, llm.token_count.prompt, and llm.token_count.completion.
  • Cost outputs: gen_ai.cost.total, cost per trace, cost per successful task, and cost per route.
  • Gateway context: gen_ai.request.model, route name, cache status, retry count, fallback count, and semantic-cache hit rate.
  • Cohort cuts: user tier, tenant, prompt version, feature flag, agent step, and eval-pass versus eval-fail cohorts.
  • Alert signals: token-cost-per-trace p95, p99 cost per user session, cache-read ratio, and cost growth after deploy.
from opentelemetry import trace

tracer = trace.get_tracer(__name__)

with tracer.start_as_current_span("agent_turn") as span:
    span.set_attribute("user.tier", "pro")
    span.set_attribute("gen_ai.request.model", "gpt-4o-mini")
    span.set_attribute("gen_ai.cost.budget_usd", 0.08)
    result = run_support_agent(question)

Cost is not a quality metric by itself. The useful dashboard pairs gen_ai.cost.total with task-completion rate, thumbs-down rate, and escalation rate. A cheaper route that doubles escalations is not cheaper in production.

Common Mistakes

  • Optimizing only the headline model price. A cheap model with more retries and longer outputs can cost more per completed task.
  • Ignoring hidden evaluator spend. Judge calls, regression evals, and safety checks need their own span costs, especially in online evaluation.
  • Averaging across all traffic. p50 cost hides runaway traces. Track p95 and p99 cost per trace by route and tenant.
  • Treating cache misses as random noise. Prompt edits can destroy prefix-cache hit rate and raise cost without changing request volume.
  • Separating gateway and trace data. Routing decisions need the same cost, token, and quality fields engineers use during incident review.

Frequently Asked Questions

What is LLM cost?

LLM cost is the dollar spend created by model inference, including prompt tokens, output tokens, cached tokens, reasoning tokens, retries, tool calls, and evaluator calls. The operational view is cost per trace, route, user, feature, and successful task.

How is LLM cost different from token usage?

Token usage is the raw count of input and output tokens. LLM cost applies provider prices, cache discounts, model routing, retries, and tool-call fanout to convert those counts into spend.

How do you measure LLM cost?

Measure it with traceAI span fields such as gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, and gen_ai.cost.total. In Agent Command Center, compare cost per successful task by route before changing routing policy: cost-optimized.