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

未关闭
#4,609 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Jennycruzy 已经在做这个了。

开始于 2026年10月11日。

  • #4610 来自 @Jennycruzy —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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!

主要语言
Python
星标
7.5k
派生
1.2k
平均合并
1 天 23 小时
30 天内合并 PR
3

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

traceloop/openllmetry 的其他 Issue

查看 traceloop/openllmetry 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。