Skip to content

Price sheets in config.yaml, a daily default table, and a price source on every request - #30

Merged
fylorn merged 1 commit into
mainfrom
feat/price-sheets
Sep 17, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/price-sheets

Conversation

@fylorn

@fylorn fylorn commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Why

Pricing had one table and a side file, and the pieces disagreed:

  • Relays charge differently, and one relay's rates apply to several upstreams. pricing.yaml only had per-model and per-provider overrides, so a discount meant copying rows, and nothing tied an upstream to a set of prices.
  • Per-upstream prices never reached the ledger. The recorder priced by model alone, with its own copy of the table loaded at startup. cheapest routing sorted by one price while the cost column recorded another, and an edited price needed a restart.
  • The default table only changed by hand. The three-step update flow was the only way to get a vendor's price cut, so most installs priced with the bundled snapshot indefinitely.
  • An amount could not be traced. Nothing recorded which prices a request was charged at, so "yesterday's figure doesn't match today's" had no answer.

What

Two layers (tw-pricing)

  • Default price table. The public dataset: the embedded snapshot, or the copy refreshed at runtime (below). Whichever is newer prices requests.
  • Price sheets. They live in config.yaml under pricing.sheets: a multiplier over the default table, plus per-model overrides in dollars per million tokens.
    • Overrides spell out input, output and the three cache prices. The long-context pair is optional and written together.
    • Nothing is inferred when pricing, so a refreshed default table never moves an override.
  • Upstreams pick a sheet with pricing:. Without one, the default table applies. Naming a sheet that does not exist is a config error, rather than silently charging default prices.
  • One order. For upstream U and model M: an override on U's sheet, otherwise the default price × the sheet's multiplier, otherwise the default price.
  • One live book. A single PriceBook behind ArcSwap is shared by forwarding (cheapest), the recorder, the speed-test and replay quotes, and the dry run. A config reload swaps the sheets and a refresh swaps the table, both through rcu, so neither overwrites the other.

Runtime refresh (tw-control/src/pricing.rs)

  • Schedule. Once a day, unless pricing.auto_update: false. The first check is a minute after start.
  • Clock. Time is compared against the wall clock every ten minutes, so a laptop that slept still refreshes when it wakes.
  • Failures. A failed attempt retries after an hour. The dataset URL stays in code, never in config.
  • Storage. The download is capped at 32 MB and must parse as the dataset. It is written to model_prices.json beside config.yaml before the book is swapped.

Endpoints

  • GET /pricing returns the table's date, source and size, whether auto-update is on, the last attempt and its error, and unpriced requests from the last 7 days grouped by upstream and model.
  • POST /pricing/refresh refreshes now. It runs whether or not auto-update is on.
  • PUT /pricing/auto_update writes auto_update: false, or removes the key when switched back on.
  • POST /pricing/query prices models against the default table, a saved sheet, or an unsaved draft, and returns each price's source and context window. With no models given, it searches by name, and plain names come before platform-prefixed keys. The UI never re-implements the order.
  • POST /pricing/sheets, PUT and DELETE /pricing/sheets/{name} work like the upstream resources:
    • A rename moves upstreams to the new name in the same version.
    • Deleting a sheet an upstream still uses is a 409 that names the upstream.
    • Removing the last sheet leaves no empty pricing: section behind.
  • The overview gains price_sheets (multiplier, override count, users) and ProviderView.pricing. ProviderInput.pricing sets it.

Recording

  • Each request stores where its price came from: the default table and its date, a sheet and its multiplier, or a sheet's override. This is schema 11, and HistoryRow.price_source exposes it.
  • The unpriced total is counted separately from the list. It used to be the sum of the top 20 models only.
  • The diagnostics bundle reports the table in effect and each upstream's sheet, rather than the snapshot constant.

Removed

  • pricing.yaml and its overrides.
  • PUT /pricing.
  • /pricing/update/{offer,fetch,apply} and their types.
  • The blake3 dependency in tw-control.

Tests

  • tw-pricing, 48 tests:
    • Resolution order, with an override kept through a table refresh.
    • A multiplier covering the cache tiers.
    • Long-context rates.
    • Displayed rates equal the charged rates.
    • Draft previews.
    • Validation of names, multipliers, negative prices, missing cache prices, and a half-written long-context pair.
  • tw-store:
    • A request is priced by its upstream's sheet, a sheet change applies without a restart, and each row records its source.
    • The migration keeps old rows with no source.
    • The unpriced total is not capped by the list.
  • tw-control/tests/pricing.rs, 10 tests against the real config file:
    • Sheet create, assign, rename and delete.
    • A missing sheet is refused.
    • Saved and draft queries, and search order.
    • The auto-update switch writes one line and removes it again.
    • Refresh against a local fake dataset: saved, used without a restart, and reloaded on restart.
    • Refresh failure on HTTP 500 and on a response that is not the dataset.
    • The background task stays offline while switched off and refreshes once switched on.
  • Unit tests: the saved table vs the snapshot by date, a corrupt saved file, and the refresh and retry timing.
  • Gates: fmt, clippy -D warnings and cargo test --workspace (1,234 tests) pass.
  • scripts/smoke.sh: 48 passed, 0 failed, 1 over target and not a regression (memory 34 MB against a 30 MB target).

🤖 Generated with Claude Code

…d a price source on every request

Prices now come in two layers, and every consumer reads one live price book.

- The default price table is the public dataset: the embedded snapshot, or
  the copy the control plane refreshes once a day (`pricing.auto_update`,
  on unless set to false) and saves as model_prices.json beside
  config.yaml. Whichever is newer prices requests. POST /pricing/refresh
  refreshes on demand, and PUT /pricing/auto_update switches the schedule.
- Price sheets live in config.yaml under `pricing.sheets`: a multiplier
  over the default table, plus per-model overrides with every field
  written out. Nothing is inferred when pricing, so a refreshed default
  table never moves an override. An upstream picks a sheet with
  `pricing:`, and naming a sheet that does not exist is a config error.
  /pricing/sheets creates, renames (upstreams follow in the same version)
  and deletes (refused while an upstream uses it).
- POST /pricing/query resolves prices against the default table, a saved
  sheet or an unsaved draft, and returns where each price came from, so
  the UI never re-implements the order.
- The recorder, the speed-test and replay quotes, cheapest routing and the
  dry run all read the gateway's price book. The recorder used to keep its
  own copy from startup: edited prices needed a restart, and per-upstream
  prices never reached the ledger.
- Each request stores where its price came from: the default table and
  its date, a sheet's multiplier, or a sheet's override (schema 11).
- The unpriced-request count on /pricing is grouped by upstream and model,
  and its total no longer stops at the first 20 models.

Removed: pricing.yaml with its per-model and per-provider overrides,
PUT /pricing, and the three-step /pricing/update/{offer,fetch,apply}.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for the PR. One thing before review: this targets main, and it should target dev.

main is a release-only line — branch-protected, linear history, and every commit on it is a tagged release snapshot. Routine work lands on dev and reaches main with the next release. GitHub pre-filled main here because it is the default branch, so this is our defaults being unhelpful rather than anything you did wrong.

No need to close and reopen: click Edit next to the PR title and change the base branch to dev. The commits and the discussion carry over.

See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract.

@fylorn
fylorn merged commit b629ee4 into main Sep 17, 2026
2 checks passed
@fylorn
fylorn deleted the feat/price-sheets branch September 17, 2026 04:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant