Skip to content

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
developfrom
feat/gen-ai-call-site
Open

sentry-junior[bot] wants to merge 4 commits into
developfrom
feat/gen-ai-call-site

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. 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.name and code.line.number for the line in the app that made the request. Both the client wrappers (instrumentOpenAiClient and 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 keeps code.function because older Pythons can't build the qualified name that code.function.name requires. The two keys are aliases in the conventions.

  • Only recording spans capture anything. At span start, Error.captureStackTrace collects 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's stackParser, one line at a time.
  • The client wrappers pass their own function as the capture boundary, so SDK frames are never collected. In tracing channel subscribers, the site is the first in-app frame after the first library frame.
  • The added cost is tens of microseconds per sampled span in a micro-benchmark. Unsampled spans pay one isRecording() check.
  • Wrappers: there is no in_app_exclude in 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 the ai package's telemetry), Anthropic's tool runner and message threads (often deeper than 20 frames), and source maps for bundled code.

Open decisions

  • Is 20 frames the right cap?
  • Should invoke_agent and tool spans get a call site too?
  • Do we want a captureCallSite: false style opt-out?

Part of

via @BYK.

--

View Session [Sentry]

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>
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
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.

Path Size % Change Change
@sentry/browser 31.22 kB - -
@sentry/browser - with treeshaking flags 29.25 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 29.21 kB - -
@sentry/browser (incl. Tracing) 54.77 kB - -
@sentry/browser (incl. Tracing Core) 41.31 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 54.77 kB - -
@sentry/browser (incl. Tracing, Profiling) 57.61 kB - -
@sentry/browser (incl. Tracing, Replay) 96.18 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 84.28 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 101.17 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 114.3 kB - -
@sentry/browser (incl. Feedback) 49.64 kB - -
@sentry/browser (incl. sendFeedback) 31.83 kB - -
@sentry/browser (incl. FeedbackAsync) 37.57 kB - -
@sentry/browser (incl. Metrics) 32.5 kB - -
@sentry/browser (incl. Logs) 32.79 kB - -
@sentry/browser (incl. Metrics & Logs) 33.53 kB - -
@sentry/react 33.7 kB - -
@sentry/react (incl. Tracing) 57.95 kB - -
@sentry/vue 39.36 kB - -
@sentry/vue (incl. Tracing) 57.75 kB - -
@sentry/svelte 31.24 kB - -
@sentry/effect (client) 42.84 kB - -
@sentry/effect (client, incl. Tracing) 55.6 kB - -
@sentry/effect (client, incl. Tracing, Logs, Metrics) 67.46 kB - -
@sentry/remix (Remix 3 client bundle) 59.82 kB - -
CDN Bundle 33.45 kB -0.01% -3 B 🔽
CDN Bundle (incl. Tracing) 55.34 kB - -
CDN Bundle (incl. Logs, Metrics) 36.3 kB -0.01% -2 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) 57.92 kB -0.01% -1 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) 78.44 kB -0.01% -1 B 🔽
CDN Bundle (incl. Tracing, Replay) 94.18 kB -0.01% -2 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 96.85 kB -0.01% -1 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) 99.36 kB -0.01% -1 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 101.85 kB -0.01% -1 B 🔽
CDN Bundle - uncompressed 97.58 kB - -
CDN Bundle (incl. Tracing) - uncompressed 163.76 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 106.19 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 171.51 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 239.49 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 287.15 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 294.89 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 299.13 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 306.86 kB - -
@sentry/nextjs (client) 61.01 kB - -
@sentry/sveltekit (client) 55.64 kB - -
@sentry/astro (client) - rolldown, without tracing 31.41 kB - -
@sentry/core/server 46.84 kB +0.03% +13 B 🔺
@sentry/core/browser 14.6 kB - -
@sentry/node 161.79 kB +0.28% +438 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.7 kB - -
@sentry/node - without tracing 99.29 kB -0.01% -1 B 🔽
@sentry/node - without channel injection 151.48 kB +0.32% +479 B 🔺
@sentry/aws-serverless 111.34 kB +0.33% +358 B 🔺
@sentry/cloudflare (withSentry) - minified 214.81 kB +0.49% +1.03 kB 🔺
@sentry/cloudflare (withSentry) 498.36 kB +0.44% +2.14 kB 🔺
@sentry/cloudflare (withSentry, instrumented CommonJS package) - minified 217.73 kB +0.48% +1.03 kB 🔺
@sentry/nextjs/cloudflare (withSentry) - minified 243.07 kB +0.42% +1.01 kB 🔺

View base workflow run

@sentry-junior
sentry-junior Bot requested a review from BYK December 2, 2026 16:33
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

All optional checks were successful. 65 successful. See the run.

Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 2m 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 hapi-21 (bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hono-5 (deno) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions (latest) Test (optional) Successful in 59s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E deno (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-without-tracing-send-to-sentry Test (optional) Successful in 1m 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 solidstart-2 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 node-express-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 56s Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-agent Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hono-5 (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-14-supabase Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (no upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 58s Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-think (ai v6) 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 hono-5 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-flue-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-eve (content off) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) 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 nuxt-5 (cloudflare) 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 nextjs-15 (latest) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) 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 solidstart-2-tunnel 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 1m Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 1m 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-v2 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 56s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-tunnel 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-5 Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (delete all) 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 react-router-7-framework-instrumentation Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E react-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (kv-cache) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (deno) Test (optional) Successful in 3m 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-flue (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 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 nuxt-5 (development) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (manual upload) 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 solidstart-2 (no upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 (delete all) 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 vue-3 (no Options API) Test (optional) Successful in 1m Details

@BYK
BYK marked this pull request as ready for review 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 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