Repository navigation
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
Draft
sentry-junior[bot] wants to merge 7 commits into
sentry-junior[bot] wants to merge 7 commits into
Conversation
…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>
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.
|
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. |
Contributor
Optional E2E testsSome optional checks were not successful. They do not block the merge. 1 not successful, 64 successful. See the run.
Successful checks
|
…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>
…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>
…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 was referenced Dec 5, 2026
Draft
Open
Open
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.hashandgen_ai.input.messages.prefix_hash, for both auto-instrumentation and theinstrument*Clientwrappers. The keys live ingen-ai-attributes.tsuntil@sentry/conventionsships them.Decisions worth a look:
recordInputsis off, because that's the case where Sentry has nothing else to compare. The convention marks themapply_scrubbing: never.init(), like the async context strategy. Node, Bun, Deno and the Cloudflare main entry (which already needsnodejs_compat) usenode:crypto. Vercel Edge and@sentry/cloudflare/requestuse 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.JSON.stringifyof the request value, withcache_controlkept. When a Responses request sets bothinstructionsand leading system/developer input items, the system hash covers both.system/developermessages and everycache_controlmember, 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.reasoningand*_callitems. Withprevious_response_idorconversation, the input holds only new items, so no message hashes are set.Part of:
via @BYK.
--
View Session [Sentry]