cleandataengineering.aiAgent Harness, drawn simply
You are here: Cost › The meter

Agent Harness Tutorial · Cost

How cost is measured

The harness prices every model response the moment it arrives, using a price table in the code. The running total for the turn goes on the trace and is checked against the turn's ceiling.

Model responseusage: tokensin, out, cachedMeter.record()price from PRICESadd to the turn's totalOpen: agent.cost.usdagent.cost.usdon the spanzoom in ›Open: exceeded()?exceeded()?$0.50 ceilingzoom in ›agent.spenda counteryesstop the turncost_ceiling_reached
One meter per turn, starting at $0.
ModelPer million tokens inPer million tokens out
openai/gpt-oss-120b (used)$0.15$0.75
openai/gpt-oss-20b$0.10$0.50

The prices are approximate, dated 1 September 2026. A model with no price fails at start-up; it is never priced as free. APIM also emits its own token counts per call (llm-emit-token-metric, namespace claims-fnol) into Application Insights, so there is a second, independent count.

Why our own meter? Azure bills by resource. Only our meter can say "this registered claim cost $0.00039".

Code view: input, process, output, the call stack, the classes

Goes ineach model call's token usage
What happensA fresh Meter per task looks up the model's price at startup, prices every call in Decimal, adds it up, and the loop stops before the next tool step once spend reaches the ceiling.
Comes outspend so far, cost per call, ceiling reached?
CALL STACK · START TO FINISHGOES IN → COMES OUT1Open the source: claims_fnol/entrypoint/__init__.py:378build(..., config=config)claims_fnol/entrypoint/__init__.py:378in: RunConfig with model and budgetsout: Agent with metering=partial(Meter, ...)2Open the source: agent_harness/cost/__init__.py:100Meter.__init__(model, ceiling_usd=, prices=) # startup pro…agent_harness/cost/__init__.py:100in: model name, max_cost_usdout: a Meter (unknown model fails at startup)3Open the source: agent_harness/cost/__init__.py:71price_of(model, prices)agent_harness/cost/__init__.py:71in: model nameout: Price from PRICES, or UnknownPrice4Open the source: claims_fnol/entrypoint/__init__.py:261Agent._dispatch # meter=self.metering()claims_fnol/entrypoint/__init__.py:261in: the Agentic routeout: a fresh Meter passed to loop.run5Open the source: agent_harness/loop/__init__.py:293_Run._account(response)agent_harness/loop/__init__.py:293in: response.usageout: trace.usage and spend_usd; cost on span6Open the source: agent_harness/loop/spend.py:22account(running, call, meter=self.meter)agent_harness/loop/spend.py:22in: running and call Usage, the meterout: (total Usage, spend or None, call cost)7Open the source: agent_harness/cost/__init__.py:113Meter.record(usage)agent_harness/cost/__init__.py:113in: one call's Usageout: this call's cost as Decimal8Open the source: agent_harness/cost/__init__.py:79cost_of(usage, price)agent_harness/cost/__init__.py:79in: token counts and Priceout: Decimal USD for this call9Open the source: agent_harness/cost/__init__.py:135Meter.as_usd()agent_harness/cost/__init__.py:135in: accumulated spendout: a float for span attributes10Open the source: agent_harness/cost/__init__.py:121Meter.exceeded # property, read in _Run.stepagent_harness/cost/__init__.py:121in: spend vs ceilingout: True -> stop COST_CEILING_REACHED11Open the source: agent_harness/loop/__init__.py:404_Run._stop(COST_CEILING_REACHED, PASS_ON)agent_harness/loop/__init__.py:404in: the reasonout: Completed with a hand-on reply
Read top to bottom: each box is one function call, indented under the function that called it; the dark box is where it starts. Click a box to open its source file.
Dashed boxes are protocols (the ports) or enums; grey boxes are data models; white boxes are classes with behaviour. A dashed arrow means "implements". Click a class to open its source.

How it is done: the real code

Cut from the live repositories each time the site is built, so it always matches what runs.

PRICES: dict[str, Price] = {
    "openai/gpt-oss-120b": Price(Decimal("0.15"), Decimal("0.75")),
    "openai/gpt-oss-20b": Price(Decimal("0.10"), Decimal("0.50")),
    "qwen/qwen3.8-27b": Price(Decimal("0.15"), Decimal("0.60")),
}
agent_harness/cost/__init__.py:93The meter: one per turn
class Meter:
    """Accumulates spend across one run and answers whether the ceiling is past.

    Constructed with the price resolved, so an unknown model fails when the run
    is set up rather than three calls in.
    """

    def __init__(
        self,
        model: str,
        *,
        ceiling_usd: float | Decimal,
        prices: dict[str, Price] | None = None,
    ) -> None:
        self.model = model
        self.price = price_of(model, prices)
        self.ceiling = Decimal(str(ceiling_usd))
        self.spend = Decimal(0)
        self.calls = 0

    def record(self, usage: Usage) -> Decimal:
        """Price one call, add it, and return what it cost."""
        amount = cost_of(usage, self.price)
        self.spend += amount
        self.calls += 1
        return amount

    @property
    def exceeded(self) -> bool:
        return self.spend >= self.ceiling

    @property
    def remaining(self) -> Decimal:
        return max(Decimal(0), self.ceiling - self.spend)