Repository navigation
Description
Describe the bug
When OpenTelemetry is enabled, Copilot CLI creates a chat span for every model call, but the outbound model request does not carry that span's W3C trace context. Neither the request headers nor the request metadata include a traceparent/tracestate.
As a result, any model backend or gateway that participates in distributed tracing cannot parent its spans under the CLI's chat span. Each model call starts a new, disconnected trace on the server side. One agent turn with N model calls therefore shows up as 1 + N traces in a tracing backend.
We hit this when embedding the CLI through the Python SDK (copilot package, 2.0.1) with a custom model request handler (CopilotClient(models={"request_handler": ...}), i.e. the llmInference.setProvider path). Trace context already flows correctly in the other direction:
- The host's trace context is passed to the CLI on
session.create/session.resume/session.send(traceparent/tracestateparams), and the CLI'sinvoke_agent,chatandexecute_toolspans are correctly parented under it. hooks.invokecallbacks andhook.startsession events carry atraceparent. For example,userPromptSubmitted,preToolUseandagentStopcarry theinvoke_agentspan. External tool callbacks (external_tool.requested) also definetraceparent/tracestate, and the SDK attaches them around the handler.
llmInference.httpRequestStart is the exception: it has no trace fields, and the forwarded headers contain no traceparent.
Affected version
Copilot CLI 1.0.114 (linux-x64), Python SDK 2.0.1.
Steps to reproduce the behavior
- Start the CLI through the Python SDK with telemetry enabled (OTLP exporter configured) and a custom
models.request_handlerthat forwards requests to the model endpoint. - Call
session.send(...)while a host span is active, e.g. with tracedddddddddddddddddddddddddddddd04and parent span4444444444444444. Use a prompt that triggers at least one tool call, e.g. "Run 'echo hi' in bash, then tell me the output." - Capture the CLI's stdout JSON-RPC frames, or inspect the requests reaching the model endpoint.
Observed in our capture (264 CLI→host messages for one turn):
- Exported spans are correct:
invoke_agent(parent4444444444444444) → 2×chat gpt-5andexecute_tool bash, all in tracedddd…04. - Both
llmInference.httpRequestStartframes have only these params:agentId,agentInvocationId,headers,interactionType,method,requestId,sessionId,transport,url. - Their
headersare onlyaccept,authorization,content-type,user-agent,x-initiator,x-interaction-typeandx-stainless-*. There is notraceparent,tracestateorbaggage. - Neither
chatspan ID appears anywhere in the CLI→host message stream. Atraceparentvalue does appear inhooks.invokeandhook.startmessages, but it always identifies theinvoke_agentspan (or the host's span forsessionStart).
Expected behavior
While a chat span is active, the model request it covers carries that span's W3C trace context. For example, traceparent: 00-<trace-id>-<chat-span-id>-<flags> (plus tracestate, if any), the same way an OTel-instrumented HTTP client would.
Preferably, for the external provider path:
- Add optional
traceparent/tracestatefields tollmInference.httpRequestStart, consistent withexternal_tool.requested. In the SDKs, expose them onCopilotRequestContextand runsend_requestunder that context (like the external-tool and hook handlers do withtrace_context(...)). Host-side HTTP instrumentation then creates correctly parented child spans instead of new root traces. - And/or inject
traceparent/tracestateinto the model request headers, so the default pass-through handler and direct (non-provider) requests propagate context without host changes.
Additional context
- This matters for any deployment where the model endpoint is behind a tracing-aware gateway, which then emits its own spans per model call. Without the
chatspan context, those spans can't be correlated with the agent turn. - The host cannot work around this exactly. The
chatspan ID exists only inside the CLI and is exported only after the span ends. The closest a host can do is parent model calls to theinvoke_agentcontext taken from hook callbacks. - Related but different: OTEL
chatspans do not have the proper parent span for subagents #4858 (chatspans' internal parenting for subagents), OTel: parallel task dispatch emits successful chat spans without response identity or usage #4860 / OTel: parallel task dispatch emits successful chat spans without response identity or usage #4862 (chatspan response identity and usage).
Activity