← Back to Pull requests

Instrument copies of instrumented OpenAI and Anthropic clients #2960

Closed
unknown closed this pull request without merging on Nov 30, 2026 11:02 UTC
View on GitHub
unknown unknown commented on Nov 27, 2026 21:08 UTC
Author

Fixes #2959.

logfire.instrument_openai(client) and logfire.instrument_anthropic(client) patch request on the given instance. client.copy() and its alias client.with_options() construct a new client that only has the class's unpatched request, so requests made through a copy were silently not recorded. Instrumenting the class (no argument) wasn't affected.

Change

When instrument_llm_provider instruments an instance that has a copy method, it also wraps copy and with_options (the SDKs define with_options = copy on the class, so both need patching). The wrapper calls the SDK's own copy and instruments the returned client with the same settings, so copies of copies are covered too. Classes are left alone, since copies share the class's patched method.

A copy gets its own instrumentation even if its class is also instrumented, matching the instance it was copied from. Otherwise it would lose its recording when the class's instrumentation is removed. While the class is instrumented, the copy's wrapper calls the class's original request, so each request is recorded once.

Uninstrumenting restores the instance's copy and with_options. Copies made while instrumented keep their instrumentation: tracking every copy so it could be uninstrumented too would keep each one alive, and code that calls with_options() per request would leak. This is the one judgment call; happy to change it.

This only covers copies made after the instance is instrumented. A copy made earlier is a separate object Logfire has no reference to, so it stays uninstrumented; the docs now say so and point to instrumenting it separately or instrumenting the class.

Behavior changes

  • Requests through with_options()/copy() on an instrumented instance now produce spans, so traces, token counts and costs can go up for affected users.

  • That includes copies the SDKs make internally: OpenAI's Responses tool runner uses client.with_options(max_retries=0) in some cases, and Anthropic's client.with_middleware(...) calls self.copy(...).
  • Checks

  • New tests for OpenAI sync (with_options, copy, a copy of a copy, settings kept, behavior after uninstrumenting), OpenAI async and Anthropic. All three fail on main and pass with the fix.

  • No duplicate spans when instance and class instrumentation are combined in either order, or when an instance is instrumented twice.

  • 200 per-request with_options() copies were all garbage-collected once dropped.

  • make lint and make typecheck are clean; the OpenAI and Anthropic test files pass.
  • Contribution questions

    Answered in #2959. In short: found while exercising the OpenAI integration end to end in a small test app, not a production incident; telemetry is silently incomplete rather than the application misbehaving; not a security problem; small change; no effect on request performance.

    Diff: 361 lines (+270 / -91) across 4 files Reviewer: chrikrah Review duration: 3713m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 361 lines diff across 4 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    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.352
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #2960"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 47.6% (MEDIUM) · Higher = larger blast radius
    47.6%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 18.8% · Safe to merge
    18.8%