Skip to content

OTel: Subagent chat spans after write_agent lose gen_ai.usage.* attributes and report parent model name #5622

Backlog

Description

@tcx4c70

Describe the bug

When a background subagent is launched via task(..., mode: "background", model: "<subagent-model>"), its initial OpenTelemetry spans export normally under an invoke_agent <agent_type> child span with the subagent's gen_ai.request.model and gen_ai.usage.{input_tokens,output_tokens} attributes populated.

However, as soon as the parent session sends a message to that background subagent via write_agent (either waking an idle subagent for a multi-turn follow-up or steering a running subagent), all subsequent OTel chat spans produced by the subagent exhibit two telemetry regressions:

  1. Wrong model and parent span attribution: Once the initial invoke_agent <agent_type> span closes, no new invoke_agent span is opened for the write_agent turn. Subsequent subagent chat and execute_tool spans attach directly to the parent session's span, and the subagent's chat spans are named chat <parent-model> with gen_ai.request.model = "<parent-model>" instead of <subagent-model> (even though gen_ai.input.messages / gen_ai.output.messages on those spans belong to the subagent).
  2. Missing token usage attributes (gen_ai.usage.*): Although events.jsonl still writes complete session.usage_record entries for the subagent (usage.accounting.sourceSessionId = "<subagent-session-id>", usage.model = "<subagent-model>", inputTokens, outputTokens, cacheReadTokens), the OTel exporter fails to attach usage metrics to those post-write_agent chat spans — gen_ai.usage.input_tokens and gen_ai.usage.output_tokens are completely absent on every subsequent subagent chat span. Downstream OTel collectors / Prometheus spanmetrics pipelines therefore drop 100% of subagent token consumption after write_agent.

Affected version

1.0.115-7 (GitHub Copilot App / copilot-agent)

Steps to reproduce the behavior

  1. Enable OpenTelemetry trace export (OTEL_EXPORTER_OTLP_ENDPOINT / OTLP receiver such as Grafana Alloy + Tempo).
  2. Start a parent session running <parent-model> (e.g. gemini-4-neon).
  3. Have the parent launch a background subagent running a different <subagent-model> (e.g. task(agent_type: "general-purpose", mode: "background", model: "gemini-4-hydrogen", ...)).
  4. Observe in Tempo that initial subagent LLM calls appear under invoke_agent general-purpose as chat gemini-4-hydrogen with gen_ai.request.model = "gemini-4-hydrogen" and gen_ai.usage.input_tokens / gen_ai.usage.output_tokens populated.
  5. Have the parent send a follow-up or steering message via write_agent(agent_id: "<subagent-id>", message: "...") (reproduces both when the subagent is idle between turns and when running).
  6. Inspect the newly exported OTel chat spans for the subagent's subsequent turns in Tempo:
    • Span name is chat gemini-4-neon (gen_ai.request.model = "gemini-4-neon") instead of gemini-4-hydrogen.
    • gen_ai.usage.input_tokens and gen_ai.usage.output_tokens are missing (null), while events.jsonl and local assistant_usage_events still record the subagent's gemini-4-hydrogen token usage normally.

Expected behavior

After write_agent wakes or steers a background subagent:

  • Subsequent subagent turns should be scoped under a subagent invoke_agent span (preserving trace hierarchy) and report the subagent's actual gen_ai.request.model (chat <subagent-model>).
  • session.usage_record events for sourceSessionId = "<subagent-session-id>" should continue binding to the corresponding subagent chat spans so gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, and cache token attributes remain populated across multi-turn write_agent interactions.

Additional context

  • Surface: GitHub Copilot App (service.name = "github-copilot-app", otel.scope.name = "github.copilot", otel.scope.version = "1.0.115-7")
  • OS / Arch: macOS (ARM64)
  • Root-cause clue from events.jsonl vs Tempo: After write_agent, subagent assistant.turn_start events still reference the original parentToolCallId from the initial task call (whose invoke_agent span has already closed), while session.usage_record carries usage.accounting.sourceSessionId = "<subagent-native-session-id>" and assistant.message carries apiCallId. The OTel span tracker appears to fall back to the parent session's active span/model context and misses the usage-record correlation for subagent turns resumed via write_agent.

Activity

  1. tcx4c70 commented on Nov 30, 2026

    @tcx4c70
    Author

    Additional reproduction path (without write_agent): parallel background subagents

    Further testing shows the underlying issue is broader than write_agent alone — any new top-level turn in the parent session while a mode: "background" subagent is still running triggers the exact same telemetry regression:

    1. Parent session (<parent-model>, e.g. gemini-4-neon) dispatches two mode: "background" subagents in parallel in the same turn — Subagent A (gpt-6.2-sol, fast) and Subagent B (grok-4.7, slower).
    2. Initially, both subagents export chat spans under their respective invoke_agent spans with accurate gen_ai.request.model and gen_ai.usage.{input_tokens,output_tokens}.
    3. Subagent A finishes first while Subagent B is still actively running.
    4. The runtime delivers Subagent A's completion <system_notification> to the parent session, starting a new parent turn (e.g. calling read_agent on Subagent A) and opening a new parent trace.
    5. Starting that new parent turn closes the previous parent trace and prematurely closes Subagent B's active invoke_agent span.
    6. Every subsequent LLM call made by the still-running Subagent B is exported onto the new parent trace as chat <parent-model> (gen_ai.request.model = "<parent-model>") with gen_ai.usage.input_tokens and gen_ai.usage.output_tokens missing (null), even though events.jsonl continues recording complete session.usage_record events for Subagent B.

    This suggests the OTel span tracker ties subagent invoke_agent span lifetimes and usage-record routing to the single parent interaction turn that spawned them, rather than maintaining independent per-sourceSessionId subagent span state across parent turn boundaries.

  2. added
    area:agents
    and removed on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:agents

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions