[PR #4747] [Engine] serve: price a prompt the way an API does — a cache write and a cache read #5160

Open
opened 2026-11-07 16:33:09 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4747
Author: @au70ma70n
Created: 10/30/2026
Status: 🔄 Open

Base: main ← Head: engine-cache-rate-pricing


📝 Commits (4)

  • 145e349 [GUI] settings: let a long help line wrap instead of squeezing every field
  • 419a478 [Engine] serve: keep the Monitor's token counts between starts
  • 0b7d7d9 [Engine] serve: print what the counted tokens would have cost at your own prices
  • c26248b [Engine] serve: price a prompt the way an API does — a cache write and a cache read

📊 Changes

7 files changed (+537 additions, -6 deletions)

View changed files

📝 docs/DETAILS.md (+18 -1)
📝 serve/runconfig.py (+10 -0)
📝 serve/server.py (+183 -1)
➕ serve/test_totals.py (+282 -0)
📝 serve/web/app.css (+7 -2)
📝 serve/web/app.js (+36 -2)
📝 serve/web/index.html (+1 -0)

📄 Description

Draft — the costing story wants scope agreement first: #4742. It lands after #4746 (it splits that PR's plain prompt price into a cache write and a cache read); merge order is #4743 → #4745 → #4746 → this one, and until the branch before it merges this pull request's diff also shows those commits and shrinks on its own as they merge.

Branch engine-cache-rate-pricing, base main.

Problem and result

To an API a prompt is not one price. The tokens the conversation cache already held are re-read far below the price of a new token, and the tokens that had to be added to it are a cache write. Before this PR the whole prompt was priced at the plain input price, so a long cached agent session looked far dearer than the same work at an API. After: cost_cache_write_per_million and cost_cache_hit_per_million in the run config and the Settings view, with --cost-cache-write and --cost-cache-hit for one start. Either one left unset takes cost_input_per_million, so a model priced without prompt caching needs only #4746's two numbers and is priced exactly as before; an explicit 0 is a price of its own (some APIs charge nothing for a re-read), not an unset one. The server prints the rates it is costing at when it starts, so a wrong price shows at once. The page names the parts apart — "about $4.08 at $6.25 per million written to the cache, $0.50 per million re-read, $25 per million out" — and only when they are priced apart from plain input.

#4742 — the scope thread for the costing story (this pull request, #4745 which keeps the counts, and #4746 which prices them plainly).

Otherwise none found: sampled searches on cost OR price OR totals (342 pull requests, 753 issues — first pages only) turned up nothing on cache write or cache read pricing; nothing claims this work, though the search was not exhaustive.

Approach

totals_cost splits the prompt at reused: given - reused at the write rate, reused at the read rate, the reply at the output price, all over 1,000,000 — and reused is clamped to given, so a damaged count cannot invent a credit. totals_with_cost echoes the rate each kind of token is actually paid at, which is the plain input rate when a cache rate was never set, so a page can name the rate it shows rather than guess one. The same counts as #4746's example — 2,000,000 given, 1,900,000 of them held — priced at $6.25 written, $0.50 read and $25 out come to 0.1 × $6.25 + 1.9 × $0.50 + 0.1 × $25 = $4.075, against #4746's $12.50 for the same work.

Validation

Passed: the 9 tests that are this PR's: Cost.test_a_read_the_cache_held_is_paid_for_as_input, Cost.test_a_re_read_is_paid_at_the_cache_price, Cost.test_an_added_token_is_paid_at_the_write_price, Cost.test_the_prices_of_a_real_price_list_on_a_day_of_agent_turns, Cost.test_a_free_cache_read_is_a_price_and_not_a_missing_one, Cost.test_the_page_is_told_the_rate_each_kind_of_token_is_paid_at, and PriceSettings.test_the_cache_prices_are_editable_from_the_page_too, test_a_cache_price_is_dollars_and_cents, test_a_cache_price_of_nothing_is_a_price_of_nothing. Environment as in #4745's Validation (Windows 11 NT 10.0.26200, Python 3.10.11, no GPU and no downloads, at 870aedd9 + these changes).

Failed: none observed.

Not run: these figures against a real API bill (the arithmetic is tested; the price list is the user's own); npm test and the Terminal suite, as in #4745's and #4746's notes.

Limits

One flat rate per part, so a tiered price list (one price to 200k, another above) still has to be a single number; whether a cache write is the 5-minute or the 1-hour rate is the user's choice, not a server decision, and the shipped default leaves both rates unset.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4747 **Author:** [@au70ma70n](https://github.com/au70ma70n) **Created:** 10/30/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `engine-cache-rate-pricing` --- ### 📝 Commits (4) - [`145e349`](https://github.com/Niko1221/Strata/commit/145e349444f39c6c7dae300f40a15223098e173b) [GUI] settings: let a long help line wrap instead of squeezing every field - [`419a478`](https://github.com/Niko1221/Strata/commit/419a4784aff7e76b94799ee19e9e29342bb58b05) [Engine] serve: keep the Monitor's token counts between starts - [`0b7d7d9`](https://github.com/Niko1221/Strata/commit/0b7d7d9a54e375945f9b6e8e13b8aa47018e3740) [Engine] serve: print what the counted tokens would have cost at your own prices - [`c26248b`](https://github.com/Niko1221/Strata/commit/c26248beed4aa4bc904ff81d926384674b078054) [Engine] serve: price a prompt the way an API does — a cache write and a cache read ### 📊 Changes **7 files changed** (+537 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+18 -1) 📝 `serve/runconfig.py` (+10 -0) 📝 `serve/server.py` (+183 -1) ➕ `serve/test_totals.py` (+282 -0) 📝 `serve/web/app.css` (+7 -2) 📝 `serve/web/app.js` (+36 -2) 📝 `serve/web/index.html` (+1 -0) </details> ### 📄 Description Draft — the costing story wants scope agreement first: #4742. It lands after #4746 (it splits that PR's plain prompt price into a cache write and a cache read); merge order is #4743 → #4745 → #4746 → this one, and until the branch before it merges this pull request's diff also shows those commits and shrinks on its own as they merge. Branch `engine-cache-rate-pricing`, base `main`. ## Problem and result To an API a prompt is not one price. The tokens the conversation cache already held are re-read far below the price of a new token, and the tokens that had to be added to it are a cache write. Before this PR the whole prompt was priced at the plain input price, so a long cached agent session looked far dearer than the same work at an API. After: `cost_cache_write_per_million` and `cost_cache_hit_per_million` in the run config and the Settings view, with `--cost-cache-write` and `--cost-cache-hit` for one start. Either one left unset takes `cost_input_per_million`, so **a model priced without prompt caching needs only #4746's two numbers and is priced exactly as before**; an explicit `0` is a price of its own (some APIs charge nothing for a re-read), not an unset one. The server prints the rates it is costing at when it starts, so a wrong price shows at once. The page names the parts apart — *"about $4.08 at $6.25 per million written to the cache, $0.50 per million re-read, $25 per million out"* — and only when they are priced apart from plain input. ## Related discussion **#4742** — the scope thread for the costing story (this pull request, #4745 which keeps the counts, and #4746 which prices them plainly). Otherwise none found: sampled searches on `cost OR price OR totals` (342 pull requests, 753 issues — first pages only) turned up nothing on cache write or cache read pricing; nothing claims this work, though the search was not exhaustive. ## Approach `totals_cost` splits the prompt at `reused`: `given - reused` at the write rate, `reused` at the read rate, the reply at the output price, all over 1,000,000 — and `reused` is clamped to `given`, so a damaged count cannot invent a credit. `totals_with_cost` echoes the rate each kind of token is *actually* paid at, which is the plain input rate when a cache rate was never set, so a page can name the rate it shows rather than guess one. The same counts as #4746's example — 2,000,000 given, 1,900,000 of them held — priced at $6.25 written, $0.50 read and $25 out come to 0.1 × $6.25 + 1.9 × $0.50 + 0.1 × $25 = **$4.075**, against #4746's $12.50 for the same work. ## Validation **Passed:** the 9 tests that are this PR's: `Cost.test_a_read_the_cache_held_is_paid_for_as_input`, `Cost.test_a_re_read_is_paid_at_the_cache_price`, `Cost.test_an_added_token_is_paid_at_the_write_price`, `Cost.test_the_prices_of_a_real_price_list_on_a_day_of_agent_turns`, `Cost.test_a_free_cache_read_is_a_price_and_not_a_missing_one`, `Cost.test_the_page_is_told_the_rate_each_kind_of_token_is_paid_at`, and `PriceSettings.test_the_cache_prices_are_editable_from_the_page_too`, `test_a_cache_price_is_dollars_and_cents`, `test_a_cache_price_of_nothing_is_a_price_of_nothing`. Environment as in #4745's Validation (Windows 11 NT 10.0.26200, Python 3.10.11, no GPU and no downloads, at `870aedd9` + these changes). **Failed:** none observed. **Not run:** these figures against a real API bill (the arithmetic is tested; the price list is the user's own); `npm test` and the Terminal suite, as in #4745's and #4746's notes. ## Limits One flat rate per part, so a tiered price list (one price to 200k, another above) still has to be a single number; whether a cache write is the 5-minute or the 1-hour rate is the user's choice, not a server decision, and the shipped default leaves both rates unset. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5160
No description provided.