PromptABCD
FeaturesLearnHow it worksUse casesFAQGuideBlogContext Blocks
Sign inGet started free
Sign inSign up
PromptABCD

A calm home for your best AI prompts. Save them once, find them in seconds, reuse them forever.

Product

  • Features
  • Chrome Extension
  • Free Courses
  • How it works
  • Use cases
  • Blog
  • Context Blocks
  • Export Anywhere
  • FAQ

Resources

  • User guide
  • Learn prompting
  • Sign in
  • Get started free

© 2026 PromptABCD. All rights reserved.

Privacy PolicyTerms and Conditions
Home/Blog/AI Harness/Cost Tracking in an AI Agent Harness
AI Harness

Cost Tracking in an AI Agent Harness

Agent harness cost tracking shows why the bill tripled — usually context re-sending, not run count. Learn per-step tracking, ceilings, and cache-friendly prompts.

September 1, 2026·8 min read
ShareShare
⚡Featured Prompt— copy and use right now
PRICING = {  # dollars per 1M tokens (input, output)
    "model-a": (3.00, 15.00),
    "model-b": (0.50, 1.50),
}

def track_cost(model, usage):
    inp, out = PRICING[model]
    cost = usage.input_tokens / 1e6 * inp + usage.output_tokens / 1e6 * out
    return {"model": model, "input": usage.input_tokens,
            "output": usage.output_tokens, "cost_usd": round(cost, 6)}

Why did your agent's bill triple last month when your user count only grew 20%? If you can't answer that from your own data, you're missing agent harness cost tracking — and you're not alone, because most teams don't add it until a surprise invoice forces the question. The answer, when they finally look, is almost always the same and almost always surprising: it's not the number of runs that exploded the cost, it's the tokens inside each run. This guide adds cost tracking that shows you exactly where the money goes, in time to do something about it — not after the invoice arrives, but while the run is still happening.

Quick-Start: Track Cost Per Run

Add cost tracking at the model-call boundary, converting tokens to dollars on every call:

hljs python
PRICING = {  ,[object Object],
    ,[object Object],: (,[object Object],, ,[object Object],),
    ,[object Object],: (,[object Object],, ,[object Object],),
}

,[object Object], ,[object Object],(,[object Object],):
    inp, out = PRICING[model]
    cost = usage.input_tokens / ,[object Object], * inp + usage.output_tokens / ,[object Object], * out
    ,[object Object], {,[object Object],: model, ,[object Object],: usage.input_tokens,
            ,[object Object],: usage.output_tokens, ,[object Object],: ,[object Object],(cost, ,[object Object],)}

What this does: it takes the token usage from a model call and multiplies by the per-token price to get the dollar cost of that single call. Log this on every model call and you can suddenly see spend per call, per step, and per run — the visibility most agents lack entirely. That's the whole foundation; everything below builds on it.

Understanding the Variables

Three facts about agent cost shape where you should look, and the first one surprises almost everyone.

Context re-sending is usually the biggest cost, not step count. Here's the thing that catches teams off guard: at each step, the agent re-sends the entire growing conversation to the model. Step one sends a small prompt; step ten sends everything from steps one through nine plus the new content. Token cost per step grows as the conversation grows, so a ten-step agent doesn't cost ten times a one-step agent — it can cost far more, because the later steps each carry the accumulated weight of all the earlier ones. The cost curve bends upward across a run.

Output tokens cost more than input tokens. Most pricing charges several times more for generated tokens than for prompt tokens. An agent that generates verbose intermediate reasoning at every step spends disproportionately on output. Trimming what the model generates often saves more than trimming what you send.

Different models have wildly different prices. The same task on a frontier model versus a small one can differ by an order of magnitude. Routing easy steps to a cheap model is one of the largest cost levers you have.

Put the first two facts together and the shape of agent cost becomes clear: it's driven far more by how much context accumulates and how much the model generates than by how many runs you do. This is why doubling your users rarely doubles your bill, and why a small change in how the agent manages its context can move cost dramatically in either direction. The lever isn't "run fewer agents" — it's "make each run carry and generate less." That reframing is what separates teams who control agent cost from teams who just watch it climb.

⚡ Pro tip: Log input and output tokens separately, never just a combined total. Because output is priced higher and behaves differently, you can't diagnose or optimize cost without the split. A single "tokens used" number hides the exact information you need — an agent that's expensive because it generates too much needs a completely different fix from one that's expensive because it re-sends too much.

How to Add Agent Harness Cost Tracking Per Step

The useful version attributes cost to each step and each tool, so you can see where in a run the money goes:

hljs python
[object Object], ,[object Object],:
    ,[object Object], ,[object Object],(,[object Object],):
        ,[object Object],.events = []

    ,[object Object], ,[object Object],(,[object Object],):
        inp, out = PRICING[model]
        cost = usage.input_tokens / ,[object Object], * inp + usage.output_tokens / ,[object Object], * out
        ,[object Object],.events.append({,[object Object],: step, ,[object Object],: tool,
                            ,[object Object],: usage.input_tokens, ,[object Object],: usage.output_tokens,
                            ,[object Object],: cost})

    ,[object Object], ,[object Object],(,[object Object],):
        total = ,[object Object],(e[,[object Object],] ,[object Object], e ,[object Object], ,[object Object],.events)
        by_step = {}
        ,[object Object], e ,[object Object], ,[object Object],.events:
            by_step[e[,[object Object],]] = by_step.get(e[,[object Object],], ,[object Object],) + e[,[object Object],]
        ,[object Object], {,[object Object],: ,[object Object],(total, ,[object Object],), ,[object Object],: by_step,
                ,[object Object],: ,[object Object],(,[object Object],.events)}

What this does: it records the cost of every model call tagged with its step number, then summarizes total spend and cost-per-step. The

by_step
breakdown is where the insight lives — you'll usually see cost climbing across steps as context accumulates, which points you straight at the fix: trim the history the agent carries forward.

⚡ Pro tip: Use your per-step breakdown to find the exact step where cost spikes. It's often a single tool that dumps a huge result into the context, inflating every subsequent step's input cost. Capping that one tool's output can cut a run's total cost more than any other single change, because you're not just saving that step — you're saving every step after it that would have carried the bloat forward.

Pro-Level Variations

Three upgrades turn tracking into control:

Set a per-run cost ceiling. Give each run a dollar budget and stop it when it's exceeded. A single runaway agent looping expensively can otherwise cost more than thousands of normal runs, and a ceiling turns that catastrophe into a logged, bounded failure.

Attribute cost to users or tasks. Tag each run with who or what it served, so you can see which customers or features drive spend. This turns cost tracking from a number into a business input — you learn which use cases are worth their cost and which aren't.

Structure prompts for caching. Some providers cache a stable prompt prefix and charge far less for the cached portion on repeat calls. If your harness keeps the system prompt and tool definitions as a stable prefix — never reordering them — you can cut input cost dramatically on multi-step runs, because the unchanging prefix is billed at the cached rate every step after the first.

The caching point deserves emphasis because it interacts with the context-accumulation problem in a useful way. The part of the prompt that stays constant across a run — system instructions, tool schemas — is exactly the part caching rewards, and it's often a large share of each request. Keep that block byte-for-byte identical across steps and a provider that caches will charge you the full rate only once, then the reduced rate on every subsequent step. Reshuffle it, or inject a timestamp into it, and you break the cache and pay full price every step. A stable prefix is close to free money, and it's easy to forfeit by accident.

Three teams putting this to work:

  • A fintech engineer sets a per-run cost ceiling on a research agent, so a pathological loop that would have cost hundreds of dollars gets killed at a few dollars with a clear log.
  • A SaaS platform lead attributes agent cost per customer and discovers a single enterprise account driving most of the spend, informing their pricing.
  • A data team restructures their agent's prompt to keep a stable cacheable prefix and cuts input-token cost on long runs substantially, just by not reshuffling the system prompt each step.

Troubleshooting Common Issues

⚠️ Common mistake: Tracking only total cost per run and stopping there. A single total tells you spend went up but not why, so you can't act on it. The actionable signal is in the breakdown — cost per step, per tool, per model — because that's what points at the specific change that'll help. An aggregate number is an alarm; the breakdown is the diagnosis, and you need the diagnosis to fix anything.

Other issues you'll hit:

  • Pricing drifts. Provider prices change. Keep your pricing table in config, not scattered through code, so an update is one edit.
  • Estimates don't match the invoice. Small gaps are normal — providers may count tokens slightly differently than your estimate. Reconcile against the real invoice monthly and adjust.
  • Cached tokens counted at full price. If you use prompt caching, your tracker needs to know the cached rate, or it'll overstate cost and hide the savings you're actually getting.
  • Streaming responses skew the count. When you stream output, make sure you capture the final usage numbers from the end of the stream, not a partial count mid-stream. A tracker that reads usage too early undercounts output tokens and quietly understates your real spend.

Your Turn

Add per-call cost logging today — it's a dozen lines, and it converts "the bill tripled" from a mystery into a report. Then add the per-step breakdown and watch for the cost curve bending upward across a run, which is the fingerprint of context accumulation. Cap the runaway case with a per-run ceiling, and you've gone from blind to in-control.

⚡ Pro tip: Set an alert on cost-per-run, not just total daily spend. Total spend tells you the sum went up; cost-per-run tells you whether individual runs got more expensive, which is the earlier and more actionable signal. A rising per-run cost usually means a prompt change bloated the context or a tool started returning more — a specific, fixable cause you can catch the day it appears rather than at the end of a billing cycle.

Agent harness cost tracking turns spend from a monthly surprise into a real-time signal you can act on. The pricing tables, cost-ceiling settings, and cache-friendly prompt structures you build are reusable across every agent you run, and each one you add makes the next agent cheaper to run and easier to reason about. Keeping those configs and the lean, cache-structured prompts that lower cost organized in a library like PromptABCD means your next agent is cost-aware from its first run — and the surprise invoice becomes a thing that happened to you once, not every quarter.

agent harness cost trackingcost trackingtokensai agentsbudgetingprompt caching

Continue Reading

Rate Limiting Inside the Agent Harness
AI Harness

Rate Limiting Inside the Agent Harness

Agent harness rate limiting done reactively makes overload worse. Learn proactive, token-aware limiting that stops the 429 retry storm before it starts.

September 1, 2026·9 min read
Building a Harness That Swaps Models Easily
AI Harness

Building a Harness That Swaps Models Easily

A model agnostic agent harness turns a week-long provider rewrite into an afternoon. Learn the adapter pattern and why portable code doesn't mean portable behavior.

September 1, 2026·8 min read
Measuring Pass@k for AI Agents (and Why It Misleads)
AI Harness

Measuring Pass@k for AI Agents (and Why It Misleads)

A pass at k agent eval can hide terrible single-attempt reliability. A case study on shipping a 90% pass@5 agent that failed half its first tries in production.

August 31, 2026·8 min read

Save the prompts from this post

PromptABCD is a free prompt manager. Paste, organize, and reuse your best AI prompts — no more hunting through chat history.

Start free →
← PreviousRate Limiting Inside the Agent Harness
Share this post:
ShareShare