Instrument copies of instrumented OpenAI and Anthropic clients #2960
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
chrikrah
Fixes #2959.
logfire.instrument_openai(client)andlogfire.instrument_anthropic(client)patchrequeston the given instance.client.copy()and its aliasclient.with_options()construct a new client that only has the class's unpatchedrequest, so requests made through a copy were silently not recorded. Instrumenting the class (no argument) wasn't affected.Change
When
instrument_llm_providerinstruments an instance that has acopymethod, it also wrapscopyandwith_options(the SDKs definewith_options = copyon the class, so both need patching). The wrapper calls the SDK's owncopyand 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
copyandwith_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 callswith_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
with_options()/copy()on an instrumented instance now produce spans, so traces, token counts and costs can go up for affected users.client.with_options(max_retries=0)in some cases, and Anthropic'sclient.with_middleware(...)callsself.copy(...).Checks
with_options,copy, a copy of a copy, settings kept, behavior after uninstrumenting), OpenAI async and Anthropic. All three fail onmainand pass with the fix.with_options()copies were all garbage-collected once dropped.make lintandmake typecheckare 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.