Repository navigation
feat(attributes): Add gen_ai prompt prefix hashes to tell cache expiry from prefix changes - #844
sentry-junior[bot] wants to merge 6 commits into
Conversation
…instructions.hash Hashes of the stable start of a gen_ai request, so a reader can tell a changed prompt prefix apart from an expired provider cache. Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨Attributes
Internal Changes 🔧
🤖 This preview updates automatically when you update the PR. |
|
Can we just compare the raw values of |
|
@alexander-alderman-webb When inputs are recorded, comparing the raw
If most users keep input capture on, the raw values cover most of the use case today, and deferring this until someone needs it with capture off is a fair call. That's a scope decision for you and @BYK. The SDK PRs (getsentry/sentry-python#8440, getsentry/sentry-javascript#27089) depend on this one, so they'd wait with it. |
|
Hashing client-side also has costs. Is there a concrete reason we expect the comparisons to be too expensive? |
|
@alexander-alderman-webb No, I don't have a measurement for that. The query-cost point was an expectation, based on tools and system prompts often being several KB per span, not something I profiled. It shouldn't carry the decision. On the client side, the cost is one canonical JSON serialization plus one SHA-256 of the tools and system instructions per call. It's skipped for unsampled spans. Python uses The concrete reason for the attributes is the input-capture-off case. Without it, comparing the raw values is a reasonable alternative. |
|
@alexander-alderman-webb One more reason I left out: with input capture on, the stored raw values still aren't reliable to compare, because Relay changes them before storage.
The SDK computes the hash from the request before either step, so it doesn't have these problems. |
|
@alexander-alderman-webb Following up on the client-side cost: here are the measurements on the code paths in getsentry/sentry-python#8440 and getsentry/sentry-javascript#27089. The payload is tools plus system instructions, about 25% system prompt and the rest JSON-schema tools, with one
The Edge path runs the same canonical JSON as Node; only the hash differs.
For typical 2–20 KB prefixes, this is 0.03–0.33 ms of CPU per sampled call, compared with LLM calls that take hundreds of milliseconds or more. At 100 KB it is 1–1.6 ms. I didn't add memoization by object identity. Python lists and dicts can't be weakly referenced, so the cache would need |
|
@alexander-alderman-webb Both SDK PRs now take a faster path for the canonical JSON, and the hashes are unchanged. Same setup as before: times are per call for tools plus system together, measured locally, not in production.
The Python columns include
The cross-language test vectors pass unchanged. New tests run the fast and slow paths on the same inputs, including |
|
One more variant I measured: skip building the canonical JSON string and do it in one pass. This walks the value with sorted keys and without
The one-pass walk is slower in both: about 2.3× in Python, where a Python walker replaces the C |
…nds them Drop the canonical form. The hash input is the JSON as sent, in insertion order and with cache_control kept, so the examples change. Co-Authored-By: Burak Yigit Kaya <126780+BYK@users.noreply.github.com>
|
The hash input is now the JSON as the provider client sends it, with no canonical form: insertion order,
In Node, the hash is now about a third of the cost. On Edge, the plain-JS SHA-256 is most of it. |
| "is_in_otel": false, | ||
| "visibility": "public", | ||
| "examples": ["776e84186ddaa3b9", "474a1bd4591d8c2d"], | ||
| "additional_context": [ |
There was a problem hiding this comment.
Could you say which part? Is it the example values here, which are just the hashes of the strings in the description, or the attribute itself? If it's the attribute: its job is to tell a changed system prompt apart from an expired provider cache when cache reads drop. That works even when input recording is off, and when Relay has scrubbed or trimmed gen_ai.system_instructions. If that use case isn't worth an attribute, that's a call for you and @BYK, and I'll leave it as is until you decide.
| "additional_context": [ | ||
| "Providers such as Anthropic and OpenAI cache the start of a request, which begins with the tool definitions. When this value differs from the previous call, the tool definitions changed and the provider could not reuse its cache for them.", | ||
| "The hash input is the tools request parameter in the provider's own format, as the caller passed it, before any SDK normalization or truncation. Examples are `tools` for the Anthropic Messages API and for the OpenAI Chat Completions and Responses APIs. It is not the value of `gen_ai.tool.definitions`.", | ||
| "Serialize the input as JSON the way the provider client sends it: object keys in insertion order, array order kept, `cache_control` members kept, no whitespace between tokens, and non-ASCII characters not escaped. A change to the key order changes the value. This is intended, because the provider receives different bytes. A `cache_control` breakpoint that moves within the tool definitions also changes the value. This is rare, because frameworks usually move breakpoints in the messages, which are not part of the input.", | ||
| "The value is the first 16 lowercase hexadecimal characters (64 bits) of the SHA-256 digest of the UTF-8 bytes of that JSON. The example is the hash of `[{\"name\":\"get_weather\",\"description\":\"Get the weather for a location\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"location\":{\"type\":\"string\"}},\"required\":[\"location\"]}}]`.", | ||
| "SDKs set this attribute whether or not they record `gen_ai.tool.definitions` or other request content. Omit it when the request has no tools or an empty list of tools, or when the SDK cannot serialize them.", | ||
| "Compare values only between spans from the same SDK with the same `gen_ai.provider.name`. SDKs in different languages can serialize the same value differently, for example key order or floating-point numbers.", | ||
| "Use it with `gen_ai.system_instructions.hash` and `gen_ai.request.model`. When all three match the previous call and cache reads still drop, the provider cache likely expired. Other changes that invalidate a provider cache, such as a different `tool_choice`, thinking settings, or images in the messages, do not change this value." | ||
| ], |
There was a problem hiding this comment.
This does not look useful either.
There was a problem hiding this comment.
Same question here: is it this guidance (compare only within one SDK and provider, use it with the model and the system hash), or the tools hash as a whole? If it's the guidance, I can cut it down to the hash input and the value format.
cleptric
left a comment
There was a problem hiding this comment.
In general, we try to add conventions with clear intent and it is not clear to me what these will be used for based of the word soup junior dumped in here.
Lead with what the hashes are for, and keep only the value format and the comparison rule.
|
Fair. I cut the descriptions down in 01eb537 and rewrote the PR description to lead with the intent: when cached input tokens drop, these two hashes show whether the app changed its tools or system prompt, or whether the provider cache just expired. Each attribute now says what it's for, how the value is computed, and what to compare it with. If the intent itself doesn't justify new attributes, that's for you and @BYK to settle. |
Scrubbing a hash would break the comparison it exists for, and the hashes are not tied to input recording. Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
Most cached input tokens in an agent loop are the conversation history. Comparing the prefix hash of a call with the full hash of the previous call shows whether the app only appended to the history or rewrote it. Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
Description
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: it defines a hash for each part of the cached prefix. If every hash matches the previous call, the cache expired. If one changed, the prefix changed, and the hash says which part.
What: Providers cache the start of a request: tools, then system instructions, then the conversation history. The hashes:
gen_ai.tool.definitions.hash: thetoolsrequest parameter.gen_ai.system_instructions.hash: the system instructions (system,instructions, and the leadingsystem/developermessages).gen_ai.input.messages.hash: the input messages, without the leading system messages andcache_control.gen_ai.input.messages.prefix_hash: the same hash over the messages before the model's latest turn.How to read them against the previous call in the conversation:
prefix_hashequals the previous call'sgen_ai.input.messages.hash: the app only appended to the history. A single hash of the whole request can't show this, because every call appends messages.The SDK computes the hashes from the request as sent, so they work when input recording is off. Comparing stored raw values doesn't work, because Relay scrubs and trims them. All four attributes use
apply_scrubbing: never: scrubbing a hash breaks the comparison it exists for, and the hashes don't followsend_default_pii/recordInputs.Part of:
via @BYK.
--
View Session [Sentry]