← Back to Pull requests

fix(anthropic): disable the SDK native tracing to avoid duplicate spans #4703

Open
unknown opened this pull request with 477 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 12, 2026 17:15 UTC
Author

Fixes #4700.

Problem

anthropic >= 1.14 emits its own span per API call when a client is constructed while a
global tracer provider is registered (anthropic.messages.create, instrumentation scope
com.anthropic.sdk.python). This instrumentation emits anthropic.chat for the same call,
in a separate trace, and both spans carry the same gen_ai.usage.* attributes, so backends
that sum usage per span or per trace count the call twice.

Measured with anthropic 2.8.0, opentelemetry-instrumentation-anthropic 0.63.0 and an
InMemorySpanExporter (mock transport, no network):

span count: 2
name='anthropic.messages.create' trace=11885258 usage={'gen_ai.usage.input_tokens': 1000, 'gen_ai.usage.output_tokens': 200, ...}
name='anthropic.chat'            trace=e068d245 usage={'gen_ai.usage.input_tokens': 1000, 'gen_ai.usage.output_tokens': 200, 'gen_ai.usage.total_tokens': 1200, ...}

Two trace IDs, identical usage → 1000 in / 200 out counted twice.

Change

AnthropicInstrumentor._instrument() now does

os.environ.setdefault("ANTHROPIC_OPEN_TELEMETRY", "false")

When the SDK reads it: not at import. anthropic._tracing.resolve_open_telemetry()
calls env_open_telemetry() once per client, when the client is constructed, and the
client keeps the result; nothing reads the variable at import time. Verified by importing
anthropic first, then instrumenting, then building the client: one span. A client that
already exists keeps what it resolved, so instrumenting after it was built does not reach
it (see the limitation below).

uninstrument() puts the environment back: the instrumentor records that it wrote the
default and removes it again, so a client built after uninstrument() gets the SDK's own
spans back. A value that was already there — including an explicit false — is never
removed.

Known limitation

A client that exists before instrument() runs keeps the native tracing it resolved at
construction: creating the client first and instrumenting afterwards still reports
span count: 2. Setting ANTHROPIC_OPEN_TELEMETRY=false in the application's environment,
or building clients after instrument(), avoids it.

Tests

tests/test_native_tracing_coexistence.py adds five tests:

  • test_instrument_disables_native_sdk_tracing — after instrument() the variable is "false"; fails on main.

  • test_uninstrument_removes_the_default_it_wrote — after uninstrument() the variable is gone; fails on the previous commit.

  • test_instrument_keeps_explicit_native_tracing_setting — an explicit "true" survives instrument() and uninstrument().

  • test_an_explicit_false_is_not_removed_by_uninstrument — a value that came from the environment stays.

  • test_ownership_survives_a_second_construction — instrument() and uninstrument() on separately constructed instrumentor instances still clean up; fails without the class-level flag, because BaseInstrumentor is a singleton.
  • The span count is not a test in the suite on purpose: reproducing the duplicate needs
    trace.set_tracer_provider(...), which mutates process-wide state and would leak into the
    other tests in the same session. I checked it out of band instead, with the same mock
    transport:

    main:  span count: 2   anthropic.messages.create, anthropic.chat   (1000 in / 200 out twice)
    this:  span count: 1   anthropic.chat                              (1000 in / 200 out once)
    

    pytest tests/test_native_tracing_coexistence.py → 5 passed; ruff check . clean.

    Merge order

    Merge #4705 first. It ends the span of a stream that is left early, closed, dropped or failed, in streaming.py only. Until it lands, turning the SDK's native span off would leave those calls with no span at all; with it landed, this PR only removes the duplicate.

    Diff: 477 lines (+357 / -120) across 5 files Reviewer: simin-yuan Review duration: 17120m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.46) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 477 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.369
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #4703"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.8% (MEDIUM) · Higher = larger blast radius
    45.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.4% · Safe to merge
    24.4%