Skip to content

feat(ai-monitoring): Rank prompt cache misses and warming savings across an org - #133195

Draft
sentry-junior[bot] wants to merge 7 commits into
masterfrom
feat/ai-prompt-cache-opportunities
Draft

sentry-junior[bot] wants to merge 7 commits into
masterfrom
feat/ai-prompt-cache-opportunities

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 its lifetime (cache warming or a longer lifetime fixes that), or the prompt prefix changed (tools, system prompt or history, which only a code change fixes). Sentry can't show either today, and the savings in any one conversation are small, so the money only shows up when misses are ranked across an org by model, agent and call site. This PR is that ranking: the API behind the Prompt Cache tab in #133205.

What

Adds GET /organizations/{org}/agents/prompt-cache/, gated behind organizations:gen-ai-prompt-cache-insights. Each row is a provider × model × agent (gen_ai.agent.name) × call site (code.file.path + code.function.name, falling back to code.function, which sentry-conventions keeps as an alias and sentry-python sends today). Rows are ranked by estimated monthly waste and link to their worst conversations, so the conversation view stays the drill-down.

The calls of each conversation, model and agent are replayed in start order (src/sentry/ai_monitoring/prompt_cache.py). For each pair of consecutive calls, with P the prefix the earlier call left in cache:

  • Expired: the gap is longer than the cache lifetime, P ≥ 4,096 tokens, and the later call writes ≥ 50% of P again. Waste is rewritten × (write price − read price).
  • Warming: keeping the cache warm costs ceil((gap − TTL) / (TTL − 30s)) × P × read price. When the waste is higher, the difference is the warming saving. Only gaps under 2h count.
  • Prefix changed: a rewrite of more than 50k tokens that reads less than 20k while the cache is still warm. Warming can't fix it.
  • Longer TTL: the rewrites a longer lifetime would avoid, minus its higher write price on the writes that remain. The saving is credited to the call site that left the prefix cached.

A read after a gap between the default and extended lifetimes means the caller already uses the extended lifetime, so that sequence is replayed with the extended values.

Spans have no TTL attribute, so the lifetime comes from gen_ai.provider.name, or from the model name for gateways like Bedrock:

Provider Default TTL Extended Source
Anthropic 5m 1h at 2× input docs
OpenAI 30m 12h at 2.5× input docs
Gemini best effort — docs: retention isn't guaranteed, so Gemini calls are counted but not analyzed

Prices come from the existing model price feed (model_costs). For providers with implicit caching (OpenAI, Gemini), uncached input counts as a miss.

Each request makes three EAP queries: input totals for the top 200 groups, the top 50 conversations by input tokens, and up to 200 calls per conversation (≤ 10k rows). Waste found in the sampled conversations is scaled by each group's input total and the time range to give a monthly estimate. meta.truncated is set when a cap is hit.

Part of

via @BYK.

--

View Session [Sentry]

Add an org endpoint that replays the LLM calls of the conversations with
the most input tokens and prices each prompt cache rewrite. A rewrite after
the provider's cache lifetime is waste that a warming ping or a longer
lifetime could avoid; a rewrite inside it means the prefix changed. Results
are grouped by provider, model, agent and call site, and scaled to a month
by each group's total input tokens.

Gated behind organizations:gen-ai-prompt-cache-insights.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Dec 2, 2026
The frontend builds in-app conversation links itself, so it needs the
project ID rather than an absolute URL.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

No findings on the latest commit.

@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Dec 2, 2026
A longer cache lifetime turns an expired rewrite into a read, so that
write no longer pays the higher write price. Only charge the premium on
the writes that would remain.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
sentry-junior Bot and others added 3 commits December 2, 2026 16:07
API responses return numeric IDs as strings.
… sites

sentry-conventions keeps code.function as an alias of code.function.name
rather than deprecating it, so ingestion does not backfill the new name,
and SDKs such as sentry-python still send only code.function. Read
code.function.name first and fall back to code.function, so both land in
the same call site group.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
…t sets it

A longer cache lifetime pays off for the call that leaves the prefix
cached, so credit an avoided rewrite to that call's group rather than
the one that found it expired. Otherwise a group gets the benefit while
another pays the higher write price.

Implicit providers also write the uncached prompt, so charge the longer
lifetime's write price on those tokens too.

This branch was successfully deployed

1 active (outdated) deployment
Preview — c1a8a82b Deployed Dec 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant