Skip to content

feat(ai): Record the call site on LLM request spans - #8441

Draft
sentry-junior[bot] wants to merge 2 commits into
masterfrom
feat/gen-ai-call-site
Draft

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

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

LLM request spans now carry code.file.path, code.function, code.line.number and code.namespace for the first in-app frame that made the request. Sentry can then group LLM calls by where they come from in the user's code, without manual instrumentation.

add_gen_ai_source reuses add_source, the frame walk behind DB and HTTP client span source, with the same project_root, in_app_include and in_app_exclude options. Scope.start_span calls it for sampled spans whose sentry.op is gen_ai.chat, gen_ai.embeddings, gen_ai.responses or gen_ai.text_completion. All AI integrations pass sentry.op at span start, so this one hook covers openai, anthropic, google_genai, langchain, litellm, mistral, cohere, huggingface_hub, bedrock and manually created gen_ai spans.

The source is taken when the span starts. A streamed response ends its span wherever the stream is consumed, which is often not where the request was made. There is no duration threshold or new option, since one stack walk per LLM request costs microseconds (about 17µs per span start in a micro-benchmark). Other spans pay one dict lookup.

Wrappers: when an app routes every LLM call through one helper, the first in-app frame is always that helper. add_source already skips modules listed in in_app_exclude, so in_app_exclude=["myapp.llm"] makes the span point at the helper's caller. The conventions define only single-frame code.* attributes, so recording a list of frames would need a new attribute.

Not covered: pydantic_ai and openai_agents send model requests from tasks the framework spawns, so no user frame is on the stack and their chat spans get no code.*. Covering them needs the source captured at Agent.run / Runner.run and carried to child spans. litellm's own span for async calls can also lack a site, because its callbacks run off the caller's stack. The underlying openai span still gets one.

Open decisions

  • Is in_app_exclude the right lever for wrappers? It also marks those frames as not in-app in error stack traces, which changes grouping for errors raised through the wrapper. The alternative is a new option that only span source reads.
  • Should there be an enable_* opt-out like enable_db_query_source?
  • Should invoke_agent spans get the call site too? That works for langchain/langgraph, but not for the two frameworks above.

via @BYK.

--

View Session [Sentry]

Set code.file.path, code.function, code.line.number and code.namespace
on gen_ai.chat, gen_ai.embeddings, gen_ai.responses and
gen_ai.text_completion spans when they start, using the same frame walk
as database and HTTP client spans. Recording it at start keeps the
request's call site for streamed responses, whose spans end wherever the
stream is consumed.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 566fdd2.

Main concerns: reach, data sent to Sentry, host performance.

This label is a hint for reviewers, not an approval.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 57920 passed | ⏭️ 3011 skipped | Total: 60931 | Pass Rate: 95.06% | Execution Time: 158m 30s

📊 Comparison with Base Branch

Metric Change
Total Tests 📉 -1440
Passed Tests 📉 -1333
Failed Tests 📉 -1
Skipped Tests 📉 -106

➖ Removed Tests (1)

View removed tests
  • test_cron_job_ok
    • File: tests.integrations.arq.test_arq_crons

All tests are passing successfully.

✅ Patch coverage is 93.75% (15 of 16 changed executable lines covered; target 80%).
Project statement coverage is 90.18% (up 0.01 percentage points from base (d860464) to head (566fdd2)).

Changed files with executable lines (2)
File Patch coverage Changed executable lines
sentry_sdk/tracing_utils.py 87.50% 7/8 covered; missed: 396; partial branches: 395
sentry_sdk/scope.py 100.00% 8/8 covered
Coverage diff
@@            Coverage Diff             @@
##        master     #8441       +/-##
==========================================
+ Coverage    90.17%    90.18%    +0.01%
==========================================
  Files          201       201         —
  Tracked lines     22184     22198       +14
  Branches      7772      7776        +4
==========================================
+ Hits         20003     20018       +15
+ Misses        2181      2180        -1
+ Partials      1283      1282        -1

Generated by Coverage Action

Python 3.7 reports the last line of a multi-line call as the frame's
line number.
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Dec 2, 2026
@sentry-junior
sentry-junior Bot requested a review from BYK December 2, 2026 16:24

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

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant