Home · Designing an AI Gateway for Enterprises · Part 6 of 17

The middleware that counts every token

Streaming makes honest metering surprisingly hard.

costmetering

A model doesn't answer all at once. It streams its reply piece by piece, so the user sees words appear. That's lovely for the user and awkward for the accountant: the size of the answer, and therefore its cost, isn't known until the last piece arrives. By then most of the response has already left.

There are two easy answers. Estimate and be roughly right most of the time, or wait until the end and make everyone wait with you. Both fail for the same reason: money is involved, and "roughly" is a poor word to use about money.

What I needed

I wanted an exact, money-denominated record of every request, streamed ones included, written down in a form a finance team would accept. I also wanted it to be mine to reason about, test and change. So I built it as a component of its own, which I call a metering relay.

It's deliberately ordinary: small, narrow, one job. It's another hop in the path, and the rest of the product treats it as exactly that. A component this central should be easy to understand, easy to test and, if it ever stopped earning its place, easy to remove.

The idea: a witness in the middle

The relay sits between the gateway and the models. It has one job: watch what really happened, write it down, and pass the stream through untouched.

Client GatewayAPIM Metering relay ModelsMicrosoft Foundry the reply streams back, untouched Usage record Portal
The relay is a witness, not a toll booth. What it writes down is what the portal later tells.

Three rules I held myself to

  1. Never slow the user. A witness may watch. It may not hold the door.
  2. Be honest about what's unknown. A record that says "unknown" beats one that invents a number.
  3. One source of truth for money. Every figure that adds up money should come from the same records. Otherwise you'll spend your meetings choosing between two totals.

None of these rules is exotic. Together they decide whether a number can be trusted, and a number that can't be trusted is worse than no number, because people act on it.

What it buys you

Three things a finance team asks for. The same total whichever page you open. A figure you can trace back to the requests behind it. And a clear label when an amount is a floor rather than exact, for instance when a stream was cut short before it finished.

Where the records go

A record nobody can read is a diary. The relay writes the truth; the portal tells it. Every figure on a usage page, a budget bar or a cost report is built from what the relay wrote down, which is why I think of the relay and the portal as two ends of one pipe. If the first end is dishonest, the second end is a beautifully designed lie.

That's also why the relay had to be built with the whole product in mind. It isn't a clever trick at the edge. It's the point where the product's most important numbers are born, and everything downstream (budgets, reports, health) inherits its honesty or its faults.

What I'll keep to myself

How the relay actually works is the part I'll keep to myself. The shape is worth stealing, though: put one small, well-behaved witness where the truth is known, and give it exactly one job.

Meter at the point where the truth is known, not at the point where it's convenient.

Was this useful?

Want to discuss this? Connect with me on LinkedIn.

Next: a product that keeps working when its control room goes offline.