Example

Recompute cost per million tokens from pinned assumptions

Turn a hardware rate, a token rate and a utilisation into a cost estimate, then sweep utilisation to expose the bound.

Cost per million tokens is arithmetic, but only after four assumptions are explicit: how many tokens are counted, what the hardware costs per hour, how many tokens per second it produces, and what fraction of the hour is actually used. Change any one and the result moves. A cost table without those inputs is a price, not an estimate.

This example uses one million tokens, hardware at $2.00 per hour, a throughput of 100 tokens per second and 50% utilisation. The effective rate is 50 tokens per second, so one million tokens take 20,000 seconds, or 5.555 hours, and cost $11.11. The sensitivity sweep at 25%, 50%, 75% and 100% utilisation gives $22.22, $11.11, $7.41 and $5.56. These are model outputs from stated assumptions, not a bill from a provider.

Run it

From the canonical repository at commit f958967d70992d7b09d9c75000bd8c543f1644d4:

python examples/ch37/cost.py --json

Python 3.11 or later, standard library only. No model, network, GPU, provider API or credentials. It finishes immediately.

Implementation

estimate_cost divides tokens by the effective token rate, converts seconds to hours, multiplies by the hourly cost, and normalises to one million tokens. sensitivity repeats that calculation at four utilisation values.

def estimate_cost(tokens: int, hardware_cost_per_hour: float, tokens_per_second: float, utilization: float = 1.0) -> CostEstimate:
    if tokens_per_second <= 0 or utilization <= 0 or utilization > 1:
        raise ValueError("invalid parameters")
    seconds = tokens / (tokens_per_second * utilization)
    hours = seconds / 3600.0
    total_cost = hours * hardware_cost_per_hour
    cost_per_million = (total_cost / tokens) * 1_000_000 if tokens > 0 else 0.0
    return CostEstimate(
        tokens=tokens,
        hardware_cost_per_hour=hardware_cost_per_hour,
        tokens_per_second=tokens_per_second,
        utilization=utilization,
        cost_per_million_tokens=cost_per_million,
        metadata={},
    )


def sensitivity(tokens: int, base_tps: float, cost_per_hour: float) -> List[CostEstimate]:
    results = []
    for util in (0.25, 0.5, 0.75, 1.0):
        results.append(estimate_cost(tokens, cost_per_hour, base_tps, util))
    return results

Expected outcome

Observed with the command above (exit 0):

{
  "estimate": {
    "tokens": 1000000,
    "hardware_cost_per_hour": 2.0,
    "tokens_per_second": 100.0,
    "utilization": 0.5,
    "cost_per_million_tokens": 11.11111111111111,
    "metadata": {}
  },
  "sensitivity": [
    {
      "tokens": 1000000,
      "hardware_cost_per_hour": 2.0,
      "tokens_per_second": 100.0,
      "utilization": 0.25,
      "cost_per_million_tokens": 22.22222222222222,
      "metadata": {}
    },
    {
      "tokens": 1000000,
      "hardware_cost_per_hour": 2.0,
      "tokens_per_second": 100.0,
      "utilization": 0.5,
      "cost_per_million_tokens": 11.11111111111111,
      "metadata": {}
    },
    {
      "tokens": 1000000,
      "hardware_cost_per_hour": 2.0,
      "tokens_per_second": 100.0,
      "utilization": 0.75,
      "cost_per_million_tokens": 7.407407407407407,
      "metadata": {}
    },
    {
      "tokens": 1000000,
      "hardware_cost_per_hour": 2.0,
      "tokens_per_second": 100.0,
      "utilization": 1.0,
      "cost_per_million_tokens": 5.555555555555555,
      "metadata": {}
    }
  ]
}

The 2x change from 25% to 50% utilisation halves the cost. That is the sensitivity the page wants visible. The estimate is linear in the hourly rate and inverse in the token rate and utilisation.

Mechanism and limitations

Documented: estimate_cost rejects a non-positive token rate, a non-positive utilisation and a utilisation above 1. It computes seconds, hours, total cost and cost per million. sensitivity runs the four utilisation values. The tests check a positive estimate, invalid parameters, four sensitivity rows, the demo and JSON serialisation.

Observed: the command exited 0 with the estimate and sweep above. The targeted tests.test_ch37_cost is part of the canonical suite; no independent test count is claimed here.

Not shown: a real bill, provider pricing, input/output price differences, batching effects, energy, cooling, idle capacity, or a measured token rate. The model treats every token the same and assumes the rate is achieved at the stated utilisation. A token rate measured at batch 1 cannot simply be reused at batch 8; a rate measured with cache hits cannot be reused for cold requests.

Challenge this result

Set utilisation to 0.1 and compute the cost by hand before running. Then explain why “the GPU is busy” is not the same as “the GPU is fully utilised”: a busy device may be stalled on memory, and a 100% utilisation counter does not mean 100% of the theoretical token rate. Finally, state the three measurements you would require before replacing the 100 tokens per second assumption with a measured value.

Source and verification

Canonical source: examples/ch37/cost.py at commit f958967d70992d7b09d9c75000bd8c543f1644d4. Tests: tests/test_ch37_cost.py . Chapter: Cost per Million Tokens . The run above is a fresh local execution on Windows 11 with Python 3.13.15; the rates are assumptions in the source, not a provider bill.

Understand this example

Copy this prompt into your AI tool. No code runs here.

Apply this example

Copy this prompt into your AI tool. No code runs here.