headroom/tests/test_pricing_cache_ttl.py
Tejas Chopra 6ec3e3478a
feat(cli,pricing): add CLI extension seam and prompt-cache TTL pricing (#2802)
## Description

Two small, independent additions. Both exist because an out-of-tree
package needed
them and neither had a home in the current API.

1. **`headroom.cli_extension`** — an entry-point group so a package can
add a
`headroom` subcommand. `headroom.proxy_extension` requires a running
FastAPI
app, so it cannot carry a read-only CLI tool, and `_register_commands()`
was a
   hardcoded import list with no discovery.
2. **`headroom/pricing/cache_ttl.py`** — the prompt-cache TTL price
structure
(read `0.10x`, 5m write `1.25x`, 1h write `2.00x` of base input) plus
the
break-even share above which the 1h TTL is cheaper. `ModelPricing`
carries
`cached_input_per_1m` for reads but has no field for the *write* side.

Closes #

## Type of Change

- [ ] Bug fix (non-breaking change that fixes an issue)
- [x] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `headroom/cli/extensions.py` (new) — `register_all(main)` discovers
the
`headroom.cli_extension` group. Contract: `register(main: click.Group)
-> None`.
Invoked from `_register_commands()` **last**, so built-ins are already
attached.
- Deliberately **not** opt-in gated, unlike proxy extensions: installing
the
package is the opt-in, because adding a subcommand cannot silently
change what
an existing command does. What *would* be a silent change is shadowing a
built-in, so that is detected and rolled back — a stale plugin can never
quietly
take over `headroom proxy`. Load failures and partial registrations roll
back
  too, and one bad plugin never blocks another.
- `headroom/pricing/cache_ttl.py` (new) — `CACHE_READ_MULTIPLIER`,
`CACHE_WRITE_MULTIPLIERS`, `cache_write_multiplier()`,
`cache_rates_per_1m()`,
`ttl_breakeven_share()`. Ratios are derived from base input rather than
transcribed into a per-model table that would triple its columns and
drift.
- `cache_write_multiplier()` raises on an unknown TTL rather than
falling back to
  the cheaper 5m rate, which would understate cost.
- `headroom/pricing/litellm_pricing.py` — three additive optional fields
on
`LiteLLMModelPricing` exposing LiteLLM's own
`cache_read_input_token_cost`,
`cache_creation_input_token_cost` and
`cache_creation_input_token_cost_above_1hr`
(present for 212 and 123 models respectively). Published rates should
win over
derived ones. All default to `None`, and `None` means "not published" —
distinct
  from `0.0` meaning "free" — so every existing caller is unaffected.
- `headroom/pricing/__init__.py` — re-exports.

### Why `ttl_breakeven_share()` exists

The TTL trade has two terms and both must be counted: moving to 1h turns
idle-gap
rewrites into cheap reads **and** raises the price of every write that
still
happens. Modelling only the recovery overstates the saving. On a real
531-transcript corpus that error was **1.9x** — $1,021 claimed against
$538 real.
`test_write_premium_is_not_forgotten` pins those exact figures so the
mistake
cannot be reintroduced quietly.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`) — see note below
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
$ python -m pytest tests/test_cli_extension_seam.py tests/test_pricing_cache_ttl.py tests/test_pricing_from_litellm.py -q
tests/test_cli_extension_seam.py .......                                 [ 25%]
tests/test_pricing_cache_ttl.py ..........                               [ 62%]
tests/test_pricing_from_litellm.py ..........                            [100%]
======================== 27 passed, 1 warning in 3.54s =========================

$ .venv/bin/ruff check headroom/cli/extensions.py headroom/cli/main.py headroom/pricing/ tests/test_cli_extension_seam.py tests/test_pricing_cache_ttl.py
All checks passed!

$ .venv/bin/mypy --python-version 3.12 headroom/cli/extensions.py headroom/pricing/cache_ttl.py headroom/pricing/litellm_pricing.py
Success: no issues found in 3 source files
```

`tests/test_pricing_from_litellm.py` is the **pre-existing** pricing
suite, included
to show the `LiteLLMModelPricing` change is non-breaking.

**mypy note.** With the repo's configured `python_version = "3.10"`,
mypy fails on
numpy's own stubs for any file that transitively reaches numpy:

```text
$ .venv/bin/mypy headroom/pricing/cache_ttl.py
.venv/lib/python3.12/site-packages/numpy/__init__.pyi:737: error: Type statement is only supported in Python 3.12 and greater  [syntax]
Found 1 error in 1 file (errors prevented further checking)
```

This is pre-existing and unrelated — untouched files reproduce it
identically
(`mypy headroom/cli/doctor.py`, `mypy headroom/pricing/registry.py`).
Hence the
`--python-version 3.12` run above, which matches the interpreter
actually in use.
Worth fixing separately; not addressed here.

## Real Behavior Proof

- **Environment:** macOS 15 (darwin 25.4.0), Python 3.12.6,
`headroom-ai` 0.34.0
  working tree, branch off `upstream/main`.
- **Exact command / steps:**
  1. Built a separate out-of-tree package declaring
`[project.entry-points."headroom.cli_extension"] fleet =
"headroom_fleet.cli:register"`.
  2. `pip install --no-deps headroom_fleet-0.1.0-py3-none-any.whl`
  3. `headroom econ --help`
- **Observed result:** the subcommand registers with no configuration
and appears
  in `headroom --help`:

  ```text
$ python -c "import importlib.metadata as m; print([e.name+' ->
'+e.value for e in m.entry_points(group='headroom.cli_extension')])"
  ['fleet -> headroom_fleet.cli:register']

  $ headroom --help | grep econ
econ Report where local AI-coding token spend goes, and what...

  $ headroom econ --help
  Usage: headroom econ [OPTIONS] [COMMAND] [ARGS]...
  Commands:
    fix    Write the recommended cache-TTL and compaction settings.
unfix Restore every setting ``econ fix`` changed, exactly as it was.
  ```

`headroom --help` and every built-in still work with the plugin
installed and
after it is uninstalled. Verified per-model cache rates resolve from
LiteLLM for
  `claude-opus-4-8`, `claude-opus-5`, `claude-sonnet-5` and
`claude-haiku-4-5-20251001` — all exactly `1.250x` / `2.000x` / `0.100x`
of base
  input, matching the derived fallback.

- **Not tested:** Windows; a plugin that raises at *import* time rather
than in
`register()` (covered by unit test with a stubbed entry point, not a
real
package); the `--python-version 3.10` mypy path, which is blocked by the
  pre-existing numpy stub issue above.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines

## Note for reviewers

This PR contains **only** the two additions above. Unrelated
`plugins/opencode/src/transport.ts` changes in my working tree are
deliberately
excluded and will follow separately.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-08-05 12:24:49 -07:00

89 lines
3.4 KiB
Python

"""Tests for prompt-cache TTL pricing structure."""
from __future__ import annotations
import pytest
from headroom.pricing import cache_ttl
def test_multipliers_match_anthropic_structure() -> None:
assert cache_ttl.CACHE_READ_MULTIPLIER == 0.10
assert cache_ttl.CACHE_WRITE_MULTIPLIERS == {"5m": 1.25, "1h": 2.00}
assert cache_ttl.DEFAULT_CACHE_TTL == "5m"
def test_cache_write_multiplier() -> None:
assert cache_ttl.cache_write_multiplier("5m") == 1.25
assert cache_ttl.cache_write_multiplier("1h") == 2.00
def test_unknown_ttl_raises_rather_than_defaulting_cheap() -> None:
"""Silently returning the 5m rate would understate cost."""
with pytest.raises(ValueError, match="unknown cache TTL"):
cache_ttl.cache_write_multiplier("30m")
def test_rates_derive_from_base_input() -> None:
rates = cache_ttl.cache_rates_per_1m(5.00) # opus-class base input
assert rates == {"read": 0.50, "write_5m": 6.25, "write_1h": 10.00}
def test_breakeven_share_is_39_5_percent() -> None:
assert cache_ttl.ttl_breakeven_share() == pytest.approx(0.3947, abs=1e-4)
def test_breakeven_is_model_independent() -> None:
"""Every term scales with base input, so the threshold is a pure ratio."""
for base in (1.00, 3.00, 5.00, 15.00):
r = cache_ttl.cache_rates_per_1m(base)
share = (r["write_1h"] - r["write_5m"]) / (r["write_1h"] - r["read"])
assert share == pytest.approx(cache_ttl.ttl_breakeven_share())
class TestBreakevenDecision:
"""The threshold must actually predict which TTL is cheaper."""
@staticmethod
def _cost(total_writes: int, idle_gap_writes: int, base: float) -> tuple[float, float]:
r = cache_ttl.cache_rates_per_1m(base)
at_5m = total_writes * r["write_5m"]
at_1h = (total_writes - idle_gap_writes) * r["write_1h"] + idle_gap_writes * r["read"]
return at_5m / 1e6, at_1h / 1e6
def test_above_threshold_1h_wins(self) -> None:
at_5m, at_1h = self._cost(1_000_000, 500_000, 5.00) # 50% > 39.5%
assert at_1h < at_5m
def test_below_threshold_5m_wins(self) -> None:
at_5m, at_1h = self._cost(1_000_000, 300_000, 5.00) # 30% < 39.5%
assert at_5m < at_1h
def test_at_threshold_costs_are_equal(self) -> None:
share = cache_ttl.ttl_breakeven_share()
at_5m, at_1h = self._cost(1_000_000, int(1_000_000 * share), 5.00)
assert at_1h == pytest.approx(at_5m, rel=1e-5)
def test_write_premium_is_not_forgotten() -> None:
"""Regression guard for the 1.9x overstatement class of error.
Figures are the real measured corpus: 306,631,892 cache-write tokens of
which 177,636,344 followed a 5m-1h idle gap, at opus-class $5/1M input.
Counting only the recovered rewrites reports ~$1,021; the honest net after
the write premium on the remaining 128,995,548 writes is ~$538.
"""
total_writes = 306_631_892
idle_gap = 177_636_344
r = cache_ttl.cache_rates_per_1m(5.00)
naive = idle_gap * (r["write_5m"] - r["read"]) / 1e6
at_5m = total_writes * r["write_5m"] / 1e6
at_1h = ((total_writes - idle_gap) * r["write_1h"] + idle_gap * r["read"]) / 1e6
net = at_5m - at_1h
assert naive == pytest.approx(1021.41, abs=0.5)
assert net == pytest.approx(537.68, abs=0.5)
assert naive / net == pytest.approx(1.9, abs=0.05)
# And this corpus is past the threshold, so the switch is correct here.
assert idle_gap / total_writes > cache_ttl.ttl_breakeven_share()