🐛 Bug Report: openai-agents records every model call as two LLM spans (calls and tokens doubled)

Đang mở
#4,609 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@Jennycruzy đang làm issue này rồi.

Từ ngày 11/10/2026.

  • #4610 của @Jennycruzy — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
observability, testing

Hướng nghiên cứu

Read the open PR linked to this issue and inspect the OpenAI Agents _hooks.py processor and responses_wrappers.py wrappers. Start by reproducing the span and token counts with the offline httpx.MockTransport example, then check the existing suppression handling in the chat, completion, embedding, and image wrappers. Done means one LLM span per non-streamed model call with the provider's token counts, including the listed failure and turn-limit cases.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Which component is this bug for?

OpenAI Agents Instrumentation (with the OpenAI instrumentation, as Traceloop.init() enables both by default)

📜 Description

With the default Traceloop.init(), every non-streamed model call made through the OpenAI Agents SDK is recorded as two LLM spans: one from the openai-agents processor and one from the OpenAI Responses wrapper. Call counts, latency and token usage in a dashboard are doubled.

Cause: the openai-agents processor does not set SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY while its generation/response span is open, and the Responses wrappers (responses_get_or_create_wrapper and its async twin) do not check that key. The chat, completion, embedding and image wrappers already check it.

👟 Reproduction steps

Standalone, offline (httpx.MockTransport), no API key:

"""OpenLLMetry + OpenAI Agents SDK: one model call is recorded as two LLM spans.
Needs traceloop-sdk and openai-agents. Offline: httpx.MockTransport answers. Leave OPENAI_API_KEY
unset so the Agents SDK's own exporter skips its upload."""
import asyncio

import httpx
from openai import AsyncOpenAI
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from traceloop.sdk import Traceloop

exporter = InMemorySpanExporter()
Traceloop.init(app_name="repro", exporter=exporter, disable_batch=True, telemetry_enabled=False)
from agents import Agent, OpenAIResponsesModel, Runner  # noqa: E402

BODY = {"id": "resp_1", "object": "response", "created_at": 0, "status": "completed", "model": "m",
        "output": [{"type": "message", "id": "msg_1", "role": "assistant", "status": "completed",
                    "content": [{"type": "output_text", "text": "Hello!", "annotations": []}]}],
        "parallel_tool_calls": True, "tool_choice": "auto", "tools": [],
        "usage": {"input_tokens": 11, "output_tokens": 7, "total_tokens": 18,
                  "input_tokens_details": {"cached_tokens": 0}, "output_tokens_details": {"reasoning_tokens": 0}}}
transport = httpx.MockTransport(lambda request: httpx.Response(200, json=BODY))
client = AsyncOpenAI(api_key="x", base_url="http://fake/v1", http_client=httpx.AsyncClient(transport=transport))
agent = Agent(name="helper", model=OpenAIResponsesModel("m", client))
asyncio.run(Runner.run(agent, "hi"))
llm = [s for s in exporter.get_finished_spans() if s.attributes.get("gen_ai.operation.name") == "chat"]
tokens = sum(s.attributes.get("gen_ai.usage.output_tokens", 0) for s in llm)
print(f"provider requests: 1; LLM spans: {len(llm)}; output tokens on LLM spans: {tokens} (provider sent 7)")
👍 Expected behavior

One LLM span per model call, with the tokens the provider sent (7).

👎 Actual Behavior with Screenshots
provider requests: 1; LLM spans: 2; output tokens on LLM spans: 14 (provider sent 7)

Cases checked (offline fake provider, LLM spans vs real provider requests):

case provider requests LLM spans today
plain agent run with one tool call 2 4
run whose tool raises 2 4
run whose model call fails 2 4
run that hits max_turns 3 6
model calls a tool that does not exist 1 2
streamed runs n n (correct)
🤖 Python Version

3.12

📃 Provide any additional context for the Bug.

traceloop-sdk 0.62.4, opentelemetry-instrumentation-openai-agents 0.62.4, openai-agents 0.23.1, openai 3.28.0. Still the same on main (17d0c5a) and with every open PR that touches _hooks.py or responses_wrappers.py (12 tested). #4060 mentions "double-instrumentation artifacts from two spans per LLM call" in passing; I could not find an issue for it.

I found this with Trueup, an AI agent I am building that runs instrumented apps against a fake provider and compares what really happened with what the trace says. The cases, the repro and the fix in the linked PR were produced by it.

👀 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!

Ngôn ngữ chính
Python
Star
7.5k
Fork
1.2k
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
3

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của traceloop/openllmetry

Tất cả issue của traceloop/openllmetry

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.