🐛 Bug Report: Anthropic: since anthropic 1.14.0 each call reports its usage on two spans
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 56/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- python
- 領域
- backend, observability
調査の方向性
Start by reading the Anthropic instrumentation entry point where instrument() configures SDK behavior, then check the tests for environment variable handling and client creation order. Confirm the existing SDK switch prevents duplicate usage spans while preserving a caller's explicit setting, and verify the documented cases around clients created before instrumentation and streaming failures.
索引モデルが issue の本文から書いたものです。
説明
Which component is this bug for?
Anthropic Instrumentation
📜 Description
Since anthropic 1.14.0 the SDK starts its own span for each call by default when a global tracer provider is registered. With this instrumentation on top, each call reports its token usage on two spans, anthropic.messages.create and anthropic.chat, in two separate traces. A call with 1,000 input and 200 output tokens then costs $0.012 in the project totals of Phoenix 21.0.0 and Opik 2.2.143, where it should be $0.006 (raw responses, r2-A-lllatest, o1-A-lllatest).
👟 Reproduction steps
# double_span.py
import json
import threading
from http.server import BaseHTTPRequestHandler, HTTPServer
import anthropic
from opentelemetry import trace
from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
class FakeAnthropic(BaseHTTPRequestHandler):
def do_POST(self):
self.rfile.read(int(self.headers["content-length"]))
body = json.dumps({
"id": "msg_1", "type": "message", "role": "assistant", "model": "claude-sonnet-5-5",
"content": [{"type": "text", "text": "hi"}],
"stop_reason": "end_turn", "stop_sequence": None,
"usage": {"input_tokens": 1000, "output_tokens": 200},
}).encode()
self.send_response(200)
self.send_header("content-type", "application/json")
self.send_header("content-length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
server = HTTPServer(("127.0.0.1", 0), FakeAnthropic)
threading.Thread(target=server.serve_forever, daemon=True).start()
exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
trace.set_tracer_provider(provider)
AnthropicInstrumentor().instrument()
client = anthropic.Anthropic(api_key="x", base_url=f"http://127.0.0.1:{server.server_port}")
client.messages.create(
model="claude-sonnet-5-5", max_tokens=10, messages=[{"role": "user", "content": "hi"}]
)
for span in exporter.get_finished_spans():
tokens = {k: v for k, v in span.attributes.items() if k in ("gen_ai.usage.input_tokens", "gen_ai.usage.output_tokens")}
print(span.name, "|", span.instrumentation_scope.name, "|", tokens)
# anthropic 2.6.0, opentelemetry-instrumentation-anthropic 0.63.0, opentelemetry-sdk 1.45.2, Python 3.12
$ python double_span.py
anthropic.messages.create | com.anthropic.sdk.python | {'gen_ai.usage.input_tokens': 1000, 'gen_ai.usage.output_tokens': 200}
anthropic.chat | opentelemetry.instrumentation.anthropic | {'gen_ai.usage.input_tokens': 1000, 'gen_ai.usage.output_tokens': 200}
$ ANTHROPIC_OPEN_TELEMETRY=false python double_span.py
anthropic.chat | opentelemetry.instrumentation.anthropic | {'gen_ai.usage.input_tokens': 1000, 'gen_ai.usage.output_tokens': 200}
👍 Expected behavior
One span per call reports usage. The fix I'd pick: instrument() calls os.environ.setdefault("ANTHROPIC_OPEN_TELEMETRY", "false"). It uses the SDK's public switch, and a caller's own setting wins. Added after instrument() in the script above, it gives the anthropic.chat span alone. It does not reach a client created before instrument().
👎 Actual Behavior with Screenshots
Two spans with usage, as above.
🤖 Python Version
3.12
📃 Provide any additional context for the Bug.
- Until #4536 and #4643 land, the SDK's span is the only record of a stream the app leaves early or of a sync stream that fails, so those should land first.
- Arize-ai/openinference#4121 reaches every client by wrapping a private SDK function. Here it needs
anthropic.chatcurrent first, as #4366 does.
👀 Have you spent some time to check if this bug has been raised before?
- I checked and didn't find similar issue
Are you willing to submit PR?
Yes I am willing to submit a PR!
- 主要言語
- Python
- スター
- 7.5k
- フォーク
- 1.2k
- 平均マージ
- 1日 23時間
- マージ済み PR(30日)
- 3
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
traceloop/openllmetry のほかの issue
-
🐛 Bug Report: OpenAI Responses: with_raw_response.create() raises when the body can't be parsed, though the uninstrumented call returns対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンbug python
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
traceloop/openllmetry#4717 ·
メンテナーはふだん 1 日以内に返信
-
🐛 Bug Report: Incomplete cache token migration after #4243 breaks Bedrock and Groq instrumentation対応中かも @Quantum-Enigma-dot が 21 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
traceloop/openllmetry#4665 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
🐛 Bug Report: OpenAI instrumentation drops gen_ai.usage.reasoning_tokens on streamed chat completions対応中かも @jayfeng7 が 23 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
traceloop/openllmetry#4660 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Bug Report: Failed dataset deletes still remove local rows and columns対応中かも @inchang-ing が 32 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
traceloop/openllmetry#4630 ·
メンテナーはふだん 1 日以内に返信
-
🐛 Bug Report: LiteLLM instrumentation does not record seed, stop, frequency_penalty, presence_penalty対応中かも @shantam77 が 38 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
traceloop/openllmetry#4598 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
traceloop/openllmetry の issue をすべて見る
似ている issue
-
CI flake: test-windows times out spawning Node in tests/test_antigravity_node_hooks.py (develop red)オープンarea/ci P2
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 2 日以内に返信
-
benchmark
難易度 1/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
mozilla-ai/cq#601 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信