Skip to content

fix(ai): Match dotted and dashed model versions so cache tokens get priced - #6808

Open
sentry-junior[bot] wants to merge 2 commits into
masterfrom
fix/ai-model-version-separator-match
Open

sentry-junior[bot] wants to merge 2 commits into
masterfrom
fix/ai-model-version-separator-match

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Why

LLM apps pay a cache-write premium every time the provider's prompt cache misses. A miss has one of two causes: the cache expired because calls were further apart than the TTL (fixed by cache warming or a longer TTL), or the prompt prefix changed (tools, system prompt, or history), which needs a code fix. Sentry can't show either today. The cost of a miss is only real if cache reads and writes are priced, but some models get no price because providers and price feeds spell the same version two ways (claude-opus-5.5 and claude-opus-5-5) and ModelMetadata::get only tries the id's own spelling.

What

When the exact key misses, get also tries the other spelling of the normalized id, swapping each . or - between two single-digit numbers (5.5 ↔ 5-5, 3-5 ↔ 3.5). An exact key in either spelling wins over a glob, and the id's own spelling wins over the other one. The metadata map gets no new keys, and multi-digit numbers such as gpt4-21-04 or 5-55 are left alone. Exact hits cost nothing extra; the alias is a SmallVec<[u8; 64]>, so ids up to 64 bytes don't allocate.

Refs CW-1897, getsentry/sentry#133197.

Open decision: should the fix live in normalization instead of the lookup? normalize_ai_model_name would turn 5-5 into 5.5, and metadata keys would get the same treatment when loaded. The lookup stays one exact match plus one glob pass and the normalized name can feed the UI, but two keys that differ only by separator collapse into one.

Part of

Together these let Sentry show the cache hit rate, alert on drops, price cache tokens correctly, name the cause of each miss, attribute it to a call site, and rank where warming would save money across an org.

via @BYK.

--

View Session [Sentry]

Model ids spell the same version as 5.5 or 5-5, so a dotted id missed a dashed cost key and the reverse. Look up the other spelling after an exact miss, for exact and glob keys, without adding keys to the metadata map.

Refs CW-1897
@linear-code

linear-code Bot commented Dec 2, 2026

Copy link
Copy Markdown

CW-1897

@BYK
BYK marked this pull request as ready for review December 3, 2026 22:25
@BYK
BYK requested a review from a team as a code owner December 3, 2026 22:25
@sentry-junior
sentry-junior Bot requested a review from BYK December 3, 2026 22:25
@jjbayer

jjbayer commented Dec 4, 2026

Copy link
Copy Markdown
Member

Would it simplify the code to lookup the version as part of VERSION_AND_DATE_REGEX, i.e. make normalize_ai_model_name normalize 5-5 into 5.5 and then proceed as before?

@sentry-junior

sentry-junior Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@jjbayer Only if we also normalize the keys. Price feeds spell keys both ways (some claude-opus-5-5, some claude-opus-5.5), so if only the incoming id becomes 5.5, a dashed key still misses.

With both sides canonicalized, it would look like this:

  • normalize_ai_model_name turns a single-digit 5-5 into 5.5 after it strips the date and version. It would return a Cow<str>, so it allocates only when it swaps something. Today the alias is built only after an exact miss, and it doesn't allocate for ids up to 64 bytes.
  • ModelPattern canonicalizes each key once, when the metadata is deserialized, globs included.
  • get goes back to one exact lookup plus one glob pass, and version_separator_alias and the second glob pass go away.

What we give up: two keys that differ only by separator would collapse into one, and the last one wins. So the "exact spelling wins" test (claude-opus-5.5 → 1.0, claude-opus-5-5 → 2.0) would go. That precedence was a requirement for this PR, but I don't expect real feeds to price the same model differently by spelling, so I'm fine dropping it for the simpler lookup. If you and @BYK agree, I'll switch to that.

via @BYK.

@jjbayer
jjbayer requested a review from vgrozdanic December 4, 2026 12:47
@jjbayer

jjbayer commented Dec 4, 2026

Copy link
Copy Markdown
Member

but I don't expect real feeds to price the same model differently by spelling, so I'm fine dropping it for the simpler lookup.

I'll let @vgrozdanic weigh in as well.

@Dav1dde

Dav1dde commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

There is also a discussion here: https://sentry.slack.com/archives/C019637C760/p1796336674610849

With the same thoughts that the model name normalization ideally can be improved as that can fix the lookup and it will help in the product/ui.

@sentry-junior

sentry-junior Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@Dav1dde Thanks, that matches Joris's suggestion: fix the normalization instead of adding fallbacks to the lookup. Concretely, normalize_ai_model_name would canonicalize single-digit version separators (5-5 → 5.5) and the metadata keys would get the same treatment when they're loaded, so the lookup stays a single exact match plus one glob pass. The same normalized name could then feed the UI.

@vgrozdanic mentioned in Slack that a larger cost-calculation project is planned, so I'll hold off on reworking this PR until he's had a look and says whether this should land now or fold into that project.

via @BYK.

@sentry-junior

sentry-junior Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

@BYK, who asked for this PR, is OK with dropping the "exact spelling wins" rule and normalizing both the model names and the price keys to one spelling, as long as dotted and dashed ids still get priced. @vgrozdanic, should I switch this PR to normalization (keeping the tests for both spellings and for globs), or leave it for your cost-calculation rework?

via @BYK.

This branch has not been deployed

No deployments
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.

3 participants