Building a Centralized Cost Layer
Introduction: From Repeated Code to a Cost Layer
In Unit 1, we calculated token cost under configured rates for a provider response. In Unit 2, we used similar pricing logic to compare estimates with calculated token costs from provider-reported usage. That duplication was useful while learning the formula, but it becomes risky in a real application.
If every call site performs its own pricing lookup, cache handling, and token-to-dollar arithmetic, the codebase can drift:
- One feature may forget to charge cache writes.
- Another may charge all input at the fresh-input rate.
- A pricing update may be applied in one script but missed in another.
- A caller may accidentally double-charge reasoning tokens.
This unit creates a cost layer: a small group of reusable pieces that own those responsibilities.
| Piece | Responsibility |
|---|---|
PRICING | Stores per-token rates for each model |
price_for() | Safely retrieves a model’s rates |
Completion | Defines one normalized result shape |
calculate_cost() | Converts token categories into dollars |
generate() | Calls the provider and returns a Completion |
The important boundary is this:
Provider-specific response handling belongs in
generate().
Token-to-dollar arithmetic belongs incalculate_cost().
Centralizing Pricing by Model
The first part of the layer is a table of configured models and their rates.
The table stores prices per token, not per million tokens. We divide by 1_000_000 once while defining the table, so every later calculation can simply use:
The table also makes model switching safe. A later router can choose between gpt-5.6-luna and gpt-5.6-sol without duplicating the billing formula.
Looking Up Rates Safely
We should never guess at pricing. A missing model entry means the application cannot calculate a trustworthy cost, so it should fail clearly.
For example, an unconfigured model produces an understandable error:
This is better than silently using another model’s rates and recording a confidently incorrect cost.
