Skip to content

refactor(modelinfo): one source for the cache price multiples - #849

Merged
amiddavid merged 3 commits into
mainfrom
feat/cache-price-multiples
Nov 2, 2026
Merged

amiddavid merged 3 commits into
mainfrom
feat/cache-price-multiples

Conversation

@amiddavid

@amiddavid amiddavid commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Behavior change (needs a human developer's yes)

  • Who: an operator whose price table leaves a model's cache-write rate blank, for a model of a family with no write premium (GPT before 5.6, Gemini, unknown).
  • Old: the extract gate (agentRates) priced that write at 1.25x the input rate.
  • New: 1.0x, the same answer ParseTable and the price feed already give (CacheWriteFracFor).
  • Effect: the gate sees a lower value for a saved token on a cold turn of those models.
  • Turn off: state cache_write in the price table for the model. Or revert commit 2.

Anthropic and GPT-5.6+ values do not change.

Summary

Closes #797. The cache multiples (read 0.1x, 5-minute write 1.25x, 1-hour write 2.0x) now live in internal/modelinfo. Seven other places read them.

What changed

Commit 1, pure refactor, every value the same:

  • internal/modelinfo/cachefrac.go: AnthropicCacheReadFrac, AnthropicCacheWrite5mFrac, AnthropicCacheWrite1hFrac.
  • modelinfo.go: new Price.CacheWrite1h(). CostWithCacheWrite1h uses it.
  • Round 1: seven more 2 * p.Input sites in dash/spendexplain.go (4), adapttools.go, toolsuggest.go and adaptcompaction.go now call Price.CacheWrite1h(). A repo-wide grep finds no other bare 2x or 2*p.Input in non-test code.
  • table.go (ParseTable), kvcache/pricing.go (Default*Multiple), dash/cachestate.go, dash/adaptcompaction.go, dash/keepalive.go (two places), components/offload/extract_econ.go, cheapmodel/usage.go, proxy/proxy.go read the constants.

Commit 2, behavior: agentRates blank write tier follows the model family.

Decisions

  • Agent-rate copy in proxy: stays separate. Its comment holds: the gate prices the traffic it sees, /stats the aggregate. It now reads the shared read multiple. The $3.00 fresh rate is still a local literal.
  • Non-Anthropic write 1.0: the repo's own tests and CacheWriteFracFor docs say Gemini and pre-5.6 GPT charge no write premium. So 1.0 wins.

Data dependence

No change. Metadata only (token counts and prices).

How it was tested

-race on the box: ./internal/..., ./kvcache/..., ./components/offload/..., ./cheapmodel/..., ./dash/..., ./proxy/... all pass. gofmt -l and go vet are clean.

  • New: internal/modelinfo/multiples_test.go (pins 0.1 / 1.25 / 2.0, CacheWrite1h, ParseTable defaults), dash/cachemultiples_test.go, and TestAgentRatesBlankWriteFollowsTheModelFamily (now with a GPT-5.6+ case at 1.25; fails on the old code for gemini and gpt-5.5).

Open questions

  • Operator override: the kvcache.Multipliers override is a per-request query parameter of the simulator page. It is not a deployment setting. So it cannot reach the dashboard or the gate without a new config key. I did not add one. Do we want it?
  • Blank read tier: agentRates, ParseTable and the price feed apply 0.1x to every family. I found no per-family read fraction in the repo, its docs, the Go module cache or on the box (no bifrost or LiteLLM price file). Gemini's read rate (about 0.25x per reviewer 1) has no source here. So I kept 0.1x. Which source do we trust for a family read fraction?
  • The $3.00 / $0.30 agent rates stay as local literals in offload and proxy.

The read (0.1x), 5-minute write (1.25x) and 1-hour write (2.0x) multiples
were written in about 8 places. They now live in internal/modelinfo, and
kvcache, dash, components/offload, cheapmodel and proxy read them. Every
value is unchanged.

Refs #797

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
When the price table left the cache-write rate blank, agentRates used 1.25x
for every model. ParseTable and the price feed use 1.0x for a family with no
write premium. agentRates now asks modelinfo.CacheWriteFracFor.

Refs #797

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.316 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 fff1938fcdc6 blocking

Verdict: blocking

Findings

  1. dash/spendexplain.go:229 — the 1-hour write rate stays the literal 2 * p.Input in seven dash sites the refactor did not reach
    • What fails: someone tunes AnthropicCacheWrite1hFrac in internal/modelinfo/cachefrac.go (its comment says "This is the ONE place they are written"). dash/cachestate.go, dash/adaptcompaction.go:1024, dash/keepalive.go and kvcache follow the new value. The seven sites below keep 2.0x. The same 1-hour write then shows two different costs on two dashboard pages.
    • Proof: the literal remains at dash/spendexplain.go:229, :388, :405 and :479 (the last one publishes cache_write_1h to the page), dash/adapttools.go:481, dash/toolsuggest.go:236, and dash/adaptcompaction.go:1036 — six lines below the line this PR did convert (adaptcompaction.go:1024). spendexplain.go:229 also repeats the premium expression 2*p.Input - p.CacheWrite that commit 1 just wrapped in Price.CacheWrite1h() for modelinfo.go:93. All values match today, so no current figure is wrong; the defect is the false "ONE place" claim plus the split source.
    • Fix: replace each 2 * p.Input with p.CacheWrite1h() (and the premium with p.CacheWrite1h() - p.CacheWrite) in those seven dash sites, or narrow the comment in cachefrac.go to the truth.

Non-blocking

  • The blank read tier in agentRates (extract_econ.go:131) stays flat 0.1x for every family. Gemini bills a cached read near 0.25x of input. This is pre-existing and out of this PR's scope, but the family rule now covers only the write tier. Worth its own issue.
  • The behavior change (commit 2) is named at the top of the PR as the repo rules require. It still needs a human developer's yes before merge. I did not see that yes.
  • dash/kvcachesim.go:378-380 tells the user "Defaults to 1.25x / 2.0x input" in UI strings. Those defaults now read the shared constants, so the strings stay true. No action.

What I checked

  • Full diff of both commits (67ad917e, fff1938f), plus the code around every change.
  • Commit 1 value equivalence, site by site: agentCacheReadPerMTok = 3.00 x 0.1 = 0.30; agentCacheWritePerMTok = 3.75; HaikuPricing = 1.25 / 0.10; NextRequestUSD 1h cold = Input x 2.0; adaptRates mixed write = (1-f)*CacheWrite + f*Input*2.0; CostWithCacheWrite1h premium = 2*Input - CacheWrite; medianPingUSD = usd/1.25x0.1; kvcache Default multiples = 0.1 / 1.25 / 2.0; ParseTable defaults = 0.1 / 1.25. All identical to the old literals. Commit 1 is behavior-neutral.
  • Commit 2: CacheWriteFracFor(c.ModelName) gates on IsClaude (substring, handles aws/ prefixes) and GPT56OrLater (handles routing prefixes). Verified the three test models: claude-sonnet-5 → 1.25, azure/gpt-5.5 → minor 5 < 6 → 1.0, gemini-3-flash-preview → 1.0. SelfRates and ModelName both come from the request's own model (proxy/modes.go:61, proxy/proxy.go:853), so fresh rate and family frac describe the same model. The PR body names the change, its effect, and the turn-off.
  • Consistency claim "the same answer ParseTable and the price feed give": confirmed at table.go:118-128 (family-gated fill) and litellm/litellm.go:222-223.
  • New tests: TestAgentRatesBlankWriteFollowsTheModelFamily fails on the old code for the 1.0 cases; multiples_test.go and cachemultiples_test.go match the code (checked CostWithCacheWrite1h(0,0,1000,0,1000) = 1000 x 6e-6 by hand).
  • Imports: no cycle — modelinfo imports only stdlib and yaml; kvcache, cheapmodel, dash, components/offload, proxy import it.
  • Scanned the repo for leftover rate literals: no 0.1 or 1.25 rate literals remain in code; the 1h 2.0x sites are finding 1. dash/schema.go:150,166 and kvcachesim.go hits are comments or UI strings.
  • Data dependence: no change (metadata only). docs/data-dependence.md needs no row change.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.316 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 fff1938fcdc6 approve

Verdict: approve

Findings

None. I found no wrong behavior, no data loss, no broken fail-open path, and no missing test for the new behavior.

Non-blocking

  • Merge gate, not a code defect: commit 2 changes what users get. A model family with no write premium (GPT before 5.6, Gemini, unknown) now gets a blank write tier of 1.0x fresh in agentRates, not 1.25x. The PR names this at the top, with who, old, new, and the turn-off. Per RELEASING.md, a human developer must say yes before merge. Do not merge on this review alone.
  • dash/adaptcompaction.go:1036 still writes the 1h tail rate as 2 * p.Input. p.CacheWrite1h() gives the same number. The literal is the drift risk this PR exists to remove.
  • dash/spendexplain.go:229, 388, 479 still compute the 1h premium as 2*p.Input - p.CacheWrite and expose "cache_write_1h": 2 * p.Input. This duplicates Price.CacheWrite1h / CostWithCacheWrite1h with bare literals. Same values today. A follow-up can route them through the method.
  • TestAgentRatesBlankWriteFollowsTheModelFamily has no GPT-5.6+ case (expect 1.25). CacheWriteFracFor covers that family in internal/modelinfo/cachefrac_test.go, so coverage exists elsewhere.
  • The blank read tier still applies the Anthropic 0.1x to every family (extract_econ.go:131, table.go:92, litellm.go:220). Pre-existing and consistent across all three sources. Not this PR's scope.

What I checked

  • Read CLAUDE.md and the diff of both commits in full, plus the code around every hunk.
  • Commit 1 is value-identical. I checked each rewrite against the old literal: HaikuPricing (1.25/0.10), the agentRates fallbacks, NextRequestUSD, adaptRates, medianPingUSD, write1hMultiple, ParseTable defaults, the kvcache defaults, CostWithCacheWrite1h's premium, and proxy's agentCacheReadPerMTok. Price.CacheWrite1h() = 2*p.Input exactly, so every derived figure is unchanged.
  • Commit 2 behavior: CacheWriteFracFor delegates to IsClaude/GPT56OrLater, which I read. The test's three cases are correct: claude-sonnet-5 → 1.25, azure/gpt-5.5 → 1.0 (minor 5 < 6), gemini-3-flash-preview → 1.0. The new answer matches what ParseTable (table.go:118-128) and the LiteLLM feed (litellm.go:223) already give the same model, so the gate, the table and the feed now agree.
  • Reach of the change: SelfRates comes from proxy.selfRates through the price chain, and both chain sources fill a blank write tier when input is known. The blank-write path in agentRates needs a Price with input but no write rate. An empty ModelName returns zero SelfRates, so the family lookup always sees a real id. The fallback constants (no SelfRates at all) are unchanged at 1.25x.
  • Float equality: 3.00 * 0.1 differs from the old literal 0.30 by 1 ulp. I checked every test that pins these constants (extract_econ_test.go, extract_cold_test.go, extract_improve_test.go, TestBreakEvenSizesMatchTheDocumentedVerdict): all compare with tolerances or use explicit SelfRates, so none breaks.
  • New tests: read all three. They assert the shipped multiples, CacheWrite1h, the ParseTable defaults, NextRequestUSD's two tiers, and the family-following write tier. The arithmetic in each is correct.
  • Imports and cycles: cheapmodel, kvcache, dash, proxy, components/offload now import internal/modelinfo; modelinfo imports none of them, so no cycle.
  • I did not run builds or tests. Live tests are not approved for this review, so the PR's "-race all pass" claim is unverified by me.

Seven dash sites still wrote `2 * p.Input` for the 1-hour rate. They now call
Price.CacheWrite1h(). Values are unchanged. A test pins the old literal.

Refs #797

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Fixed in 86e260c. All seven sites now use p.CacheWrite1h() (premium form: p.CacheWrite1h() - p.CacheWrite). A repo-wide grep finds no other bare 2 * p.Input. Values are the same. TestCacheWrite1hMatchesTheOldBareLiteral pins the old literal.

@amiddavid

Copy link
Copy Markdown
Collaborator Author

No source found, so 0.1x stays. I checked the repo code and docs, prices.example.yaml, the Go module cache and the eval box. None holds a per-family read fraction or a bifrost/LiteLLM price file. The question is under Open questions in the PR body.

@amiddavid

Copy link
Copy Markdown
Collaborator Author

Added a GPT-5.6+ case (azure/gpt-5.6-luna, expect 1.25) to TestAgentRatesBlankWriteFollowsTheModelFamily in 86e260c.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.316 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 86e260c31687 approve

Verdict: approve

Findings

None. My round-1 finding is fixed, and the new commit introduces no defect.

Status of earlier findings

  1. Mine, round 1 — the 1h write rate stayed the literal 2 * p.Input in seven dash sites. Fixed in 86e260c3. I read the full diff: all seven sites now call p.CacheWrite1h(), and the three premium sites use p.CacheWrite1h() - p.CacheWrite. spendexplain.go:479 now publishes "cache_write_1h": p.CacheWrite1h(). I grepped dash, kvcache, components, cheapmodel, proxy and internal for 2 * p.Input under any receiver name: no hit outside tests. The "ONE place" comment in cachefrac.go is now true.
  2. Reviewer 2, round 1 — same literal sites (non-blocking). I checked them myself; fixed by the same commit. Agree with reviewer 2, and add only this: my grep also covered 2*x.Input forms with other receiver names, and found none.
  3. Reviewer 2, round 1 — no GPT-5.6+ case in the family test. Fixed: 86e260c3 adds azure/gpt-5.6-luna expecting 1.25. I traced GPT56OrLater: last gpt- segment gives major 5, minor 6, so it returns true and the frac is 1.25. The case is correct.
  4. Mine and reviewer 2, round 1 — blank read tier flat 0.1x for every family (non-blocking). Rejected by the author: no source holds a per-family read fraction (repo, docs, prices.example.yaml, module cache, eval box). I checked the reason: table.go:92 and litellm.go:220 apply the same flat 0.1x, so the three sources agree and no data exists to do better. I accept the rejection. The question stays open in the PR body.

Non-blocking

  • The merge gate stands: commit fff1938f changes what users get (blank write tier 1.0x for families with no premium). The PR names it as required. A human developer must still say yes before merge.
  • TestCacheWrite1hMatchesTheOldBareLiteral compares the method against the same arithmetic (2*in - in*1.25), so it cannot fail on a pure refactor of the test's own expression. It still pins the method to Input x 2.0, which is the point. No action needed.

What I checked

  • Last review: round 1, fff1938fcdc69249811c8b2cb19cb41babbcd78f, 2026-11-02T10:28:31Z.
  • New commits read: 1 (86e260c3 "refactor(dash): read the 1-hour write rate from Price.CacheWrite1h").
  • Read in full: 2 reviews, 3 comments, 0 inline comments (from the three files in my folder).
  • New items since my last review:
    • Reviewer 2's round-1 review (10:29:05Z, approve) — read in full; its literal-site notes are my finding 1, its other notes handled above.
    • Author comment 10:56:49 (fix summary) — verified against the diff and a repo-wide grep.
    • Author comment 10:56:51 (read tier stays 0.1x, no source) — reason checked and accepted.
    • Author comment 10:56:53 (GPT-5.6+ test case added) — traced the case by hand.
    • Commit 86e260c3 — read the full diff and the code around each hunk. Every conversion is value-identical (CacheWrite1h() = Input x 2.0). Both test additions checked by hand. No import changes, no behavior change, no data-dependence change.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.316 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 86e260c31687 approve

Verdict: approve

Findings

None. The new commit is value-identical everywhere it touches, and it fixes every open item.

Status of earlier findings

  • Reviewer 1, finding 1 (seven dash sites with the bare 2 * p.Input): fixed in 86e260c3. I re-read all seven sites — spendexplain.go (4), adapttools.go, toolsuggest.go, adaptcompaction.go. Each now calls p.CacheWrite1h() (premium form: p.CacheWrite1h() - p.CacheWrite). I grepped the repo for any remaining bare 2 * <something>.Input: none outside tests. adapttools.go and toolsuggest.go hold a modelinfo.Price, so the method applies. Values are unchanged.
  • Reviewer 2 (me), non-blocking notes from round 1:
    • adaptcompaction.go:1036 literal — fixed.
    • spendexplain.go literals — fixed.
    • Missing GPT-5.6+ test case — added (azure/gpt-5.6-luna, expect 1.25). I traced GPT56OrLater("azure/gpt-5.6-luna"): last gpt- token, major 5, minor 6 → true, so 1.25. Correct.
    • Blank read tier flat 0.1x — the author rejected a change with a reason: no per-family read fraction exists in the repo, prices.example.yaml, the module cache or the box. I accept the reason. All three price sources (extract_econ.go:131, table.go:92, litellm.go:220) apply 0.1x consistently, so no source disagrees. Gemini's real read rate is a separate issue, as reviewer 1 also noted.

Non-blocking

  • Merge gate, still open: the behavior change in commit 2 (blank write tier 1.0x for non-premium families) is named at the top of the PR body. I found no human developer's yes in the PR body or the comments. Per RELEASING.md, record that yes before merge.
  • TestCacheWrite1hMatchesTheOldBareLiteral's second assertion (CacheWrite1h() - CacheWrite equals 2*in - CacheWrite) restates the first. Harmless; it documents the premium form.

What I checked

  • Last review: round 1, fff1938fcdc69249811c8b2cb19cb41babbcd78f, 2026-11-02T10:29:05Z.
  • New commits read: 1 — 86e260c3 "refactor(dash): read the 1-hour write rate from Price.CacheWrite1h".
  • Read in full: 2 reviews, 3 comments, 0 inline comments (from the three saved files).
  • New items since my last review:
    • Reviewer 1's round-1 review (posted 34 s before mine, so I read it only now): checked its finding against the code myself, see above.
    • Author comment 10:56:49 (seven sites fixed): verified in the diff and by grep.
    • Author comment 10:56:51 (read tier stays 0.1x): checked the reason against the three fill sites; accepted.
    • Author comment 10:56:53 (GPT-5.6+ test case): verified in the diff and traced the family rule.
  • New commit, site by site: every changed line replaces 2 * p.Input with p.CacheWrite1h() or the premium form. CacheWrite1h() is Input * AnthropicCacheWrite1hFrac with the constant at 2.0, so each value is bit-identical. The commit touches no logic beyond the rate expression.
  • New tests: TestCacheWrite1hMatchesTheOldBareLiteral — arithmetic correct, including the in = 0 case. The azure/gpt-5.6-luna case — correct per GPT56OrLater.
  • Imports: the test additions need no new imports; the dash files already import modelinfo.
  • I did not run builds or tests. Live tests are not approved for this review.

@amiddavid
amiddavid merged commit 7cf058a into main Nov 2, 2026
5 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 2, 2026
@amiddavid
amiddavid deleted the feat/cache-price-multiples branch November 2, 2026 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

refactor: cache price multiples (0.1 / 1.25 / 2.0) are written in about 8 places

1 participant