DEV Community

Cover image for Part 36: Measuring token cost per invoice
Shakar Bisetty
Shakar Bisetty

Posted on

Part 36: Measuring token cost per invoice

Part 36 · One Agent, Five Platforms

Cost per business outcome beats cost per token. A token price tells you what the model charges. It doesn't tell you what one handled invoice costs, and that's the number finance will ask for.

Read this first: no hands-on build has been run yet, so there are no measured numbers in this part. Every number below is illustrative: made up to show the method, including the prices. They're not a quote for any model or platform. Platform features come from vendor docs read on 24 November 2026.

Formula card: cost per invoice handled equals tokens times price, divided by invoices handled, with an illustrative worked example

1. The formula

Cost per invoice handled = (tokens × price) ÷ invoices handled

Each part of that needs a definition, or two teams will get two different answers:

  • Tokens: every model call the agent made in the period, including retries, failed runs and cases that ended in an escalation. Count input, output, cache-read and cache-write tokens separately, because they're priced differently. AWS's cost report guide says all four token types are needed to reconcile to the bill.
  • Price: your price per token type for the model you actually use, from your pricing page or contract, with the date you read it.
  • Invoices handled: exceptions where the agent produced a usable outcome. That means a proposal the approver accepted, or a correct hand-off to a person (GS17, the out-of-scope case from Part 10, is "handled" when the agent routes it correctly). A run that produced a wrong proposal doesn't count.

The tokens include the failures, but the count of invoices doesn't. That's on purpose: an agent that fails often costs more per invoice, even if each run is cheap.

Platform fees sit on top: runtime, gateway, or a consumption currency such as SAP's AI units. Add them separately. This part only covers model tokens.

2. A worked example (illustrative)

One agent run on one invoice, with three model calls. All figures are made up:

  • System prompt plus tool definitions: 2,500 tokens, sent on every call.
  • The invoice, PO and goods receipt data: 1,000 tokens.
  • Each tool result adds 600 tokens; each model reply is 250 tokens.

So call 1 sends 3,500 input tokens, call 2 sends 4,350 and call 3 sends 5,200, because the history grows. That's 13,050 input and 750 output tokens, about 14,000 per run. That matches "Platform A" in Part 35's illustrative table.

With example prices of $2 per million input tokens and $10 per million output tokens (again, not a real quote):

  • Cost per run: 13,050 × $2/M + 750 × $10/M = $0.0336
  • A month: 1,000 runs, plus 100 retries at about $0.013 each = $34.89
  • Invoices handled: 930 of the 1,000
  • Cost per invoice handled: $34.89 ÷ 930 = about $0.0375

That's 12% more than the cost per run, because of retries and the 70 cases that weren't handled. The number to compare it with is your own cost of a person handling one exception. Only you have that figure.

3. Why prompt size dominates

In the example, the output is 750 tokens. The input is 13,050, and 7,500 of those are the same system prompt and tool definitions, sent three times. That's 57% of the input that never changes.

Agent loops resend the whole context on every call. So the levers, roughly in order of effect:

  1. Fewer calls per invoice. Every extra call resends everything, so a retry limit is also a cost limit.
  2. Smaller tool definitions. Give the agent only the tools it needs, with short, precise descriptions. The shared tool contract from Part 7 keeps them identical across platforms, so a trim helps all five.
  3. Compact tool results. Return the fields the agent uses, not the whole ERP record.
  4. Prompt caching. The static prefix can be cached. On Amazon Bedrock, cache reads are billed at a reduced rate and don't count toward the tokens-per-minute quota; writes can cost more than normal input. If your cache-read rate were a tenth of the input rate, and the cache stayed warm, this example run would drop from $0.0336 to about $0.020, roughly 40% less. Check your model's pricing page for the real rate.
  5. A smaller model for easy cases. Route the clear-cut exceptions to a cheaper model, but only after the golden set shows it passes.

4. Where each platform shows token use

From vendor docs read on 24 November 2026:

  • MuleSoft: the LLM Token Based Rate Limit policy on Omni Gateway counts request, response and reasoning tokens per key, such as a client_id, over a fixed window. Over the limit, it returns 429 with x-token-limit, x-token-remaining and x-token-reset headers. It needs gateway 1.11 or later. It's a budget, not a cost report (Series 1 Part 5 used it as the token budget behind the retry limit; Part 11).
  • Boomi: the AI Gateway product page says it can "track budget and optimize token cost" at the org, department, user or agent level, and limit token usage per user, app or agent. That's from a product page, not the docs, so I can't say how the reports look (Part 15).
  • SAP: SAP Business AI is consumed in AI units, a virtual currency with "predefined conversion rates per request, user, record or other metric", plus an AI unit estimator. So for Joule, cost per invoice is AI units per request, not tokens. I couldn't read token-level detail for the generative AI hub (Part 18).
  • Azure: in API Management, llm-emit-token-metric sends total, prompt and completion token metrics to Application Insights, with up to five custom dimensions; cached and reasoning token categories are in preview. llm-token-limit sets tokens per minute or a quota per period, keyed by a counter-key (Part 22).
  • AWS: Bedrock publishes InputTokenCount, OutputTokenCount, CacheReadInputTokenCount and CacheWriteInputTokenCount to CloudWatch by ModelId (runtime metrics). The cost and usage report can split cost by application inference profile or IAM principal tags, but has no per-request ID; for that it points to model invocation logs (Part 25).

5. The catch: no platform gives you cost per invoice

Every platform above gives you totals per model, agent, key or app. None gives you cost per invoice out of the box. Azure's custom dimensions are capped at 100 unique values each, so an invoice ID can't be a metric dimension anyway.

The fix is the same on all five: log the usage from each model response with the case ID, then add it up per invoice. Most model APIs return token counts in the response. Part 37 picks this up with one correlation ID across platforms.

6. Caveats

  • Illustrative numbers. When the builds run, this part gets measured token counts from the golden set (Part 35), with the price and date used.
  • Prices change. Note the pricing page and date next to every cost figure.
  • Same model, different bill. The same model on two platforms can be priced differently, and platform fees differ. Compare the total, not just the token line.
  • Token counts differ by model. The same prompt can be a different number of tokens on a different model's tokenizer.

Do you track cost per outcome?


This series describes a reference build on a fictional company. All numbers and prices in this part are illustrative, not measured or quoted; no hands-on build has been run yet. Platform features are from vendor documentation and product pages read on 24 November 2026. Views are my own.

Top comments (0)