Repository navigation
feat(server-utils): Record the call site of gen_ai requests to locate prompt cache misses - #27090
Open
sentry-junior[bot] wants to merge 4 commits into
Open
sentry-junior[bot] wants to merge 4 commits into
sentry-junior[bot] wants to merge 4 commits into
Conversation
Set code.file.path, code.function and code.line.number on the chat, completion and embeddings spans of the OpenAI, Anthropic, Google GenAI and LangChain embeddings integrations, so LLM calls can be grouped by where in the app they were made. The stack is captured with Error.captureStackTrace only for recording spans, capped at 20 frames, and read from V8's structured call sites when the span ends, so it's never formatted or parsed as a string. Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
Contributor
size-limit report 📦Compared: b9bd52d (PR test merge using develop at 2698107) Against: 2698107 (develop) · base run #68907 ✅ Baseline matches: this diff contains only this PR's changes.
|
Contributor
Optional E2E testsAll optional checks were successful. 65 successful. See the run. Successful checks
|
BYK
marked this pull request as ready for review
December 4, 2026 20:06
javascript-sdk-gitflow
Bot
requested review from
RulaKhaled,
isaacs and
timfish
December 4, 2026 20:06
…e URLs Bun formats the stack when it's captured, so Error.prepareStackTrace can't hand over structured call sites there. Parse that string lazily with the client's stackParser instead. Normalize call site paths with normalizeStackTracePath, so a Windows file URL like file:///C:/app/index.js becomes C:/app/index.js.
…ion.name code.function is deprecated in favor of code.function.name, which the rest of the SDK already uses.
…s for gen_ai call sites filenameIsInApp only recognizes node_modules/, so on Windows a provider or SDK frame could be picked as the call site.
This was referenced Dec 5, 2026
Draft
This was referenced Dec 5, 2026
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. The cache either expired, because the gap between calls was longer than the TTL (cache warming or a longer TTL fixes that), or the prompt prefix changed (code fixes that). Sentry can't show either cause today. This PR is the "where" piece: it records the line of user code that made each LLM call, so misses and wasted cache writes can be grouped by call site and users know which call to fix or warm.
What
Chat, completion and embeddings spans from the OpenAI, Anthropic, Google GenAI and LangChain embeddings integrations now get
code.file.path,code.function.nameandcode.line.numberfor the line in the app that made the request. Both the client wrappers (instrumentOpenAiClientand friends) and the tracing channel integrations set them. A stream gets the line that started it, not the line that consumed it.The function name uses
code.function.name, like the rest of this SDK. getsentry/sentry-python#8441 keepscode.functionbecause older Pythons can't build the qualified name thatcode.function.namerequires. The two keys are aliases in the conventions.Error.captureStackTracecollects at most 20 frames without formatting them. At span end, they're read as V8's structured call sites, so the stack is never formatted or run through the stack parser. Bun formats stacks eagerly, so there the string is parsed lazily with the client'sstackParser, one line at a time.isRecording()check.in_app_excludein JS, so an app's own LLM helper becomes the call site for every request through it. A later change could add an option to skip named modules or functions.Not covered yet: LangChain chat models and LLMs (callbacks run after LangChain's own
awaits, so no app frame is on the stack), Vercel AI (its spans come from theaipackage's telemetry), Anthropic's tool runner and message threads (often deeper than 20 frames), and source maps for bundled code.Open decisions
invoke_agentand tool spans get a call site too?captureCallSite: falsestyle opt-out?Part of
via @BYK.
--
View Session [Sentry]