ai agents llm cost observability production ai cost attribution langsmith

The Hidden Cost of AI Agents in Production (And How to Control It)

Agentic Runbook ·

You approved the budget for the AI initiative. The team built something. It’s running in production.

Then the cloud bill arrives.

This is not a hypothetical scenario. It’s the conversation we’re having with engineering leaders at mid-market SaaS and fintech companies right now. Not because they did anything wrong — but because AI agents have a cost structure that’s fundamentally different from traditional software, and most teams don’t build cost visibility in until they’ve already been surprised by it.

This post is for CTOs, VPs of Engineering, and engineering managers who are operating — or about to operate — AI agents in production. We’ll cover how agent costs compound, where the hidden expenses actually live, and the architectural patterns that give you control without slowing your team down.


Why Agent Costs Are Different

Traditional software has predictable compute costs. You provision infrastructure, it runs, you pay for the instances. Spikes are visible in your cloud dashboard.

Agent costs don’t work that way. Here’s what makes them different:

LLM API costs are per-token and highly variable. The same workflow can cost $0.002 in one invocation and $0.40 in another, depending on context length, model selection, and how many tool calls the agent makes before reaching a decision. There’s no flat pricing.

Agents make multiple LLM calls per workflow. A single “trigger → classify → route → respond” agent might make 3–5 LLM calls per run. If your multi-agent system has a supervisor coordinating 4 workers, a single user request might generate 15–25 LLM calls. At low volume this is fine. At scale it compounds fast.

Runaway loops are expensive. Unlike infinite loops in traditional code (which consume CPU and are usually caught quickly), an agentic loop that calls an LLM 40 times before hitting a max-iteration guard has already spent real money. The failure mode is financially costly, not just computationally.

Multiple agents share a billing surface. If you have 8 agents running across different workflows, their costs are often aggregated under a single OpenAI or Anthropic API key. Without cost attribution tagging, you can’t tell which agent or which client engagement is driving the bill.

Tool calls add up. Agents that call external APIs, run database queries, or invoke other agents each time they run add latency and cost beyond the LLM calls themselves — especially if those tools are priced per-call.


Where the Hidden Costs Actually Live

Based on what we see in practice, here are the most common sources of unexpected agent spend:

1. Oversized context windows

The most common and most invisible cost driver. Agents that include full conversation history, large document chunks, or unfiltered tool outputs in every LLM call pay for context they don’t need.

A retrieval agent that appends 8,000 tokens of retrieved chunks to every classifier call when 2,000 tokens would suffice is spending 4x on that LLM call. Multiply by invocation volume and this adds up fast.

What to do: Audit your context construction. Are you appending raw tool outputs or summarized outputs? Are you trimming message history or carrying it indefinitely? Set explicit context budgets per node.

2. Model selection mismatches

Not every task needs your most capable (and most expensive) model. A routing/classification node that runs on GPT-4o when GPT-4o-mini would perform identically is paying a 10–15x price premium per call for no benefit.

We see this consistently when teams prototype with a powerful model, then ship to production without reviewing the model-to-task fit.

What to do: Implement a model routing table keyed to task type. Classification, routing, summarization, and simple extraction can almost always run on smaller models. Reserve your frontier models for complex reasoning, multi-step planning, and high-stakes decisions where capability differences are measurable.

3. Missing max-iteration guards

LangGraph and similar frameworks have recursion_limit settings. Teams often leave them at defaults (25–50) without thinking through the cost implications. A workflow that enters a bad state and runs 50 LLM calls before hitting the recursion limit has already spent the equivalent of hundreds of normal invocations.

What to do: Set explicit recursion_limit values per graph, with tight limits for production agents. Add iteration counters to your state and fail fast (with a Slack alert) at a low threshold. Log every invocation that hits the limit — they should be investigated, not silently discarded.

4. Untracked invocations

If your agents aren’t tagged with metadata — which agent, which client, which trigger type, which environment — your billing dashboard is a single undifferentiated number. You can’t optimize what you can’t attribute.

What to do: Every LangSmith run (or equivalent) should carry mandatory metadata tags: agent_slug, client_slug (if applicable), trigger_type, environment. This is the foundation for per-engagement cost attribution and for identifying which agents or workflows are driving spend.

5. Development and eval spend bleeding into production reporting

If your dev and eval invocations share a billing key with production, your cost reporting is polluted. A synthetic dataset eval that runs 500 LLM calls during a PR gate looks like a production cost spike if you’re not separating environments.

What to do: Use separate LangSmith projects per environment (ar-{slug}-prod vs. ar-{slug}-evals). Use separate API keys or at least separate tagging to allow environment-level cost filtering.


The Cost Attribution Stack You Actually Need

Here’s the minimum viable cost visibility architecture for teams operating agents in production:

Layer 1: Per-run metadata tagging

Every LLM invocation gets tagged with:

  • agent_slug — which agent made this call
  • trigger_type — slack_mention, webhook, cron
  • environment — production, staging, dev
  • client_slug — which client engagement (for consulting/multi-client contexts)

This is a one-time implementation per agent and gives you the foundation for everything else.

Layer 2: LangSmith project isolation

One LangSmith project per agent per environment:

  • ar-inbound-triage-production
  • ar-inbound-triage-evals
  • ar-inbound-triage-staging

LangSmith’s cost reporting is at the project level. Without this isolation, you’re reading tea leaves.

Layer 3: Weekly cost roll-up report

An automated script (or a finance-bot agent) that queries LangSmith’s run API weekly, groups by agent_slug and client_slug, applies your model pricing table, and posts a cost summary to your #exec Slack channel.

The report should answer three questions:

  1. Total LLM spend this week, broken down by agent
  2. WoW delta — is any agent’s cost trending up unexpectedly?
  3. Per-client attribution — for consulting firms or multi-tenant SaaS, which engagement is driving what spend?

Layer 4: Cost anomaly alerts

Set hard thresholds and alert immediately when crossed:

  • Single invocation > $2 → immediate Slack alert to #exec
  • Weekly per-client spend > $100 → DM to CFO/founder
  • WoW spend increase > 50% for any agent → flag for review

These thresholds are conservative starting points. Tune them based on your actual baseline after 2–4 weeks of data.


Operational Patterns That Actually Help

Beyond the attribution stack, these operational patterns make a meaningful difference in cost control:

Cache aggressively

Many agent workflows make redundant LLM calls. If your agent classifies the same customer question pattern dozens of times a day, a simple semantic cache (embedding the query, storing the classification result) can eliminate 60–80% of the LLM calls for high-frequency patterns. LangChain’s InMemoryCache and Redis-backed caches are straightforward to implement.

Build cost into your eval gate

Before any agent ships a new version, run your eval dataset and compare the cost per invocation against the prior version. If a “fix” doubled the average cost per run, that’s a signal — either the fix added unnecessary complexity or there’s a model selection regression.

Make cost-per-invocation a first-class metric in your eval pipeline, not an afterthought.

Human-in-the-loop reduces expensive edge case handling

This sounds counterintuitive, but agents that try to handle every edge case autonomously often end up making more LLM calls (retry loops, multi-step recovery, fallback chains) than agents that escalate to a human at a lower confidence threshold. An HITL escalation costs you a human’s time but saves you the LLM cost of 5–10 additional recovery calls.

Design your confidence thresholds with cost in mind.

Review your tool call frequency

If your agent calls an external API (a CRM, a database, a search service) on every invocation regardless of whether the data has changed, you’re paying for tool calls that aren’t adding value. Add caching at the tool layer for data that changes slowly. Review which tool calls are actually influencing the agent’s output and which are vestigial from earlier iterations.


Common Mistakes We See

Treating LLM cost like infrastructure cost. You can’t right-size an LLM API bill the same way you right-size an EC2 instance. The optimization levers are different (context, model selection, caching, iteration limits) and require understanding the agent’s actual runtime behavior.

Waiting for a surprise bill to build visibility. The time to instrument cost attribution is before you have cost data to analyze, not after. Retrofitting tags and project isolation into production agents is painful; building it in takes an afternoon.

Optimizing for token count without measuring quality impact. Some cost reduction measures (truncating context, downgrading models, caching aggressively) can degrade agent output quality in ways that aren’t immediately obvious. Always measure quality alongside cost — the cheapest agent that gives wrong answers is the most expensive one.

Conflating API spend with total cost. LLM API cost is visible. The human review time triggered by low-confidence escalations, the engineering hours spent debugging runaway loops, the customer impact of an agent that hallucinated — these are real costs that don’t show up in your OpenAI invoice. Cost optimization that ignores these dimensions is incomplete.


A Realistic Cost Profile

To give you a benchmark: a well-instrumented, well-optimized production agent handling a classification-and-routing workflow at moderate volume (500–2,000 invocations/week) should cost between $5–$50/week in LLM API costs. That’s a very wide range — the difference is almost entirely model selection and context length.

The same agent with a frontier model, unoptimized context, and no caching might run $200–$800/week at the same volume. Before you’ve even validated that the workflow justifies that spend.

The optimization leverage is real. But it requires visibility first.


Where to Start

If you’re operating agents in production today and don’t have cost attribution in place:

  1. Add metadata tags to your LangSmith runs this week. Four mandatory fields: agent_slug, trigger_type, environment, client_slug. One-time change per agent.

  2. Create per-environment LangSmith projects. Isolate prod from evals from staging. This takes an afternoon.

  3. Set a weekly cost review cadence. Even a manual query of your LangSmith dashboard weekly catches surprises before they compound. Automate it once you have the baseline.

  4. Audit context construction on your highest-volume agent. What’s in the context window? Is all of it actually used by the LLM to make better decisions? Trim what isn’t.

The companies building sustainable agentic systems aren’t the ones who built the most agents or moved the fastest. They’re the ones who built cost visibility in early enough to make confident decisions — about which agents to scale, which models to use, and where to invest in optimization.

Know What Your Agents Are Actually Costing You

The Diagnostic Sprint includes a full review of your agent architecture, cost attribution, and operational runbook — then hands you the tools to run it yourself. 4–6 weeks. Full knowledge transfer.

Learn About the Diagnostic Sprint

Ready to build your agentic team?

Start with a Diagnostic Sprint — a 2–4 week structured audit that produces your prioritized Agentic Roadmap.

Start with a Diagnostic →