Skip to content

feat(server-utils): Hash the prompt prefix to tell cache expiry from prefix changes - #27089

Draft
sentry-junior[bot] wants to merge 7 commits into
developfrom
feat/gen-ai-prefix-hashes
Draft

sentry-junior[bot] wants to merge 7 commits into
developfrom
feat/gen-ai-prefix-hashes

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 the gap between calls outlived the TTL, which cache warming or a longer TTL fixes, or the prompt prefix (tools, system prompt or history) changed, which a code change fixes. Today Sentry can't show the hit rate or tell these causes apart; together, the PRs below let Sentry show the 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.

This PR is the "which cause" piece for the JavaScript SDK. If every hash matches the previous call, the cache expired. If one changed, the prefix changed. This depends on getsentry/sentry-conventions#844 and shouldn't merge before it.

What

Anthropic and OpenAI (including OpenAI-compatible) chat spans now carry gen_ai.tool.definitions.hash, gen_ai.system_instructions.hash, gen_ai.input.messages.hash and gen_ai.input.messages.prefix_hash, for both auto-instrumentation and the instrument*Client wrappers. The keys live in gen-ai-attributes.ts until @sentry/conventions ships them.

Decisions worth a look:

  • They are set even when recordInputs is off, because that's the case where Sentry has nothing else to compare. The convention marks them apply_scrubbing: never.
  • Each runtime SDK sets the SHA-256 function in init(), like the async context strategy. Node, Bun, Deno and the Cloudflare main entry (which already needs nodejs_compat) use node:crypto. Vercel Edge and @sentry/cloudflare/request use a plain-JS SHA-256, so it stays out of the Node bundle. WebCrypto is async, so it can't set the attribute when the span starts. Only recording spans pay for the hashing.
  • Tools and system instructions are hashed as JSON.stringify of the request value, with cache_control kept. When a Responses request sets both instructions and leading system/developer input items, the system hash covers both.
  • The message hashes leave out the leading system/developer messages and every cache_control member, because apps move that breakpoint to the newest message on each call. They use the same serialization as feat(ai): Hash the prompt prefix to tell cache expiry from prefix changes sentry-python#8440, so both SDKs give the same values for the same messages.
  • In the Responses API, the model's turn also includes reasoning and *_call items. With previous_response_id or conversation, the input holds only new items, so no message hashes are set.

Part of:

via @BYK.

--

View Session [Sentry]

…ns hashes

Hash the tools and system instructions of Anthropic and OpenAI requests, so a reader can tell a changed prompt prefix apart from an expired provider cache. The hashes are set even when inputs are not recorded.

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

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: d75fdf2 (PR test merge using develop at 21c65d2)

Against: 21c65d2 (develop) · base run #68767

✅ Baseline matches: this diff contains only this PR's changes.

Path Size % Change Change
@sentry/browser 30.73 kB - -
@sentry/browser - with treeshaking flags 28.85 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.7 kB - -
@sentry/browser (incl. Tracing) 54.07 kB - -
@sentry/browser (incl. Tracing Core) 40.89 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 54.08 kB - -
@sentry/browser (incl. Tracing, Profiling) 56.97 kB - -
@sentry/browser (incl. Tracing, Replay) 94.06 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 82.59 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 98.9 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 111.85 kB - -
@sentry/browser (incl. Feedback) 48.59 kB - -
@sentry/browser (incl. sendFeedback) 31.29 kB - -
@sentry/browser (incl. FeedbackAsync) 36.34 kB - -
@sentry/browser (incl. Metrics) 32.04 kB - -
@sentry/browser (incl. Logs) 32.32 kB - -
@sentry/browser (incl. Metrics & Logs) 33 kB - -
@sentry/react 32.61 kB - -
@sentry/react (incl. Tracing) 56.57 kB - -
@sentry/vue 39.07 kB - -
@sentry/vue (incl. Tracing) 57.19 kB - -
@sentry/svelte 30.77 kB - -
@sentry/effect (client) 42.29 kB - -
@sentry/effect (client, incl. Tracing) 55.1 kB - -
@sentry/effect (client, incl. Tracing, Logs, Metrics) 66.59 kB - -
@sentry/remix (Remix 3 client bundle) 59.55 kB - -
CDN Bundle 32.54 kB - -
CDN Bundle (incl. Tracing) 54.17 kB - -
CDN Bundle (incl. Logs, Metrics) 35.25 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 56.63 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 76.2 kB - -
CDN Bundle (incl. Tracing, Replay) 91.98 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 94.49 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 97.44 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 99.89 kB - -
CDN Bundle - uncompressed 94.94 kB - -
CDN Bundle (incl. Tracing) - uncompressed 160.29 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 103.28 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 167.92 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 233.67 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 280.72 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 288.35 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 292.65 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 300.27 kB - -
@sentry/nextjs (client) 59.93 kB - -
@sentry/sveltekit (client) 54.89 kB - -
@sentry/astro (client) - rolldown, without tracing 35.83 kB +0.01% +2 B 🔺
@sentry/core/server 45.96 kB - -
@sentry/core/browser 14.55 kB - -
@sentry/node 158.3 kB +0.35% +540 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.63 kB - -
@sentry/node - without tracing 89.75 kB +0.01% +8 B 🔺
@sentry/node - without channel injection 147.67 kB +0.39% +563 B 🔺
@sentry/aws-serverless 100.66 kB +0.02% +14 B 🔺
@sentry/cloudflare (withSentry) - minified 213.47 kB +0.07% +149 B 🔺
@sentry/cloudflare (withSentry) 495.59 kB +0.09% +408 B 🔺
@sentry/cloudflare (withSentry, instrumented CommonJS package) - minified 216.41 kB +0.07% +149 B 🔺
@sentry/nextjs/cloudflare (withSentry) - minified 241.67 kB +0.07% +149 B 🔺

View base workflow run

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

One or more gzipped bundles increased by more than 500 bytes. If this increase is intentional, add the Accept Bundlesize Increase label to this PR to rerun and accept the check.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 1 not successful, 64 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E hono-5 (cloudflare) Test (optional) Failing after 1m Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E cloudflare-think (ai v6) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (no upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hapi-21 (deno) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-flue (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-14-supabase Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E node-eve (content off) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 (MCP SDK OpenTelemetry) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hono-5 (bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-flue-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E astro-7 (static trace lifecycle) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E deno (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-agent Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E hono-5 (deno) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (cloudflare) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (0.x) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hono-5 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-without-tracing-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps enabled by Sentry) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (kv-cache) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E hapi-21 (bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-cloudflare Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry (static trace lifecycle) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-tunnel Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E react-send-to-sentry Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (no upload) Test (optional) Successful in 55s Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (development) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E react-router-7-framework-instrumentation Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E vinext-cf-workers (workers cache) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-4-sourcemaps (Vite server) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2-tunnel Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (deno) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions (latest) Test (optional) Successful in 53s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 (no upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 (manual upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (bun) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (manual upload) Test (optional) Successful in 54s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 (delete all) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (delete all) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E vue-3 (no Options API) Test (optional) Successful in 1m Details

sentry-junior Bot and others added 2 commits December 2, 2026 15:56
…ntime has it

Each runtime SDK sets the SHA-256 function in init(): node:crypto for Node, Bun, Deno and the Cloudflare main entry (which needs nodejs_compat already), the plain-JS hash for Vercel Edge and @sentry/cloudflare/request. This keeps the plain-JS SHA-256 out of the @sentry/node bundle.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
sentry-junior Bot and others added 3 commits December 2, 2026 22:13
…request hashes

Copy each object with sorted keys in a JSON.stringify replacer, and fall back to the hand-built JSON when a key starts with a digit, because JS objects enumerate integer-like keys first. The hashes stay the same. Drop the debug warning for values that can't be hashed to stay under the size limit.

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

Hash JSON.stringify of the value, in insertion order and with cache_control kept, instead of a canonical form. A key reorder or a moved cache_control breakpoint now changes the hash, because the provider sees different bytes. This removes the canonical JSON code.

Co-Authored-By: Burak Yigit Kaya <126780+BYK@users.noreply.github.com>
The prefix hash covers the input messages before the model's latest turn, so it equals the messages hash of the previous call when the app only appended to the history. Both leave out leading system messages and cache_control, and each message is serialized once.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@sentry-junior sentry-junior Bot changed the title feat(server-utils): Set gen_ai tool definitions and system instructions hashes feat(server-utils): Set gen_ai request and input message hashes Dec 3, 2026
…ssages

The OpenAI Responses API sends both, so a change to either must change gen_ai.system_instructions.hash. Matches getsentry/sentry-python#8440.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant