Skip to content

Surface OTel token usage from agent-stdio.log when proxy usage logs are missing#34036

Merged
pelikhan merged 5 commits into
mainfrom
copilot/otel-advisor-surface-token-usage
May 22, 2026
Merged

Surface OTel token usage from agent-stdio.log when proxy usage logs are missing#34036
pelikhan merged 5 commits into
mainfrom
copilot/otel-advisor-surface-token-usage

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 22, 2026

Most gh-aw.agent.conclusion spans were missing gen_ai.usage.* attributes because token enrichment depended solely on /tmp/gh-aw/agent_usage.json, which is only written when proxy token logs are present. Engines already emit equivalent usage data in agent-stdio.log result events, but that path was not being consumed for OTel enrichment.

  • Add runtime token-usage fallback

    • Extend readAgentRuntimeMetrics() to read result.usage from agent-stdio.log
    • Capture input, output, cache-read, and cache-write token counts from runtime result events
    • Preserve existing agent_usage.json behavior as the primary source when available
  • Normalize engine usage shapes

    • Map runtime usage fields onto the existing OTel enrichment shape used by agent_usage.json
    • Support both runtime cache field variants already seen in engine logs:
      • cache_read_input_tokenscache_read_tokens
      • cache_creation_input_tokenscache_write_tokens
  • Keep span semantics unchanged

    • Continue emitting gen_ai.usage.total_tokens as input + output
    • Continue attaching usage attributes only where the current agent/conclusion span routing expects them
    • Avoid changing downstream-job behavior or double-counting rules
  • Cover fallback and precedence

    • Add focused tests for:
      • fallback to agent-stdio.log when agent_usage.json is absent
      • precedence of agent_usage.json when both sources exist

Example of the runtime event now used as fallback:

{
  "type": "result",
  "num_turns": 3,
  "usage": {
    "input_tokens": 48200,
    "output_tokens": 1350,
    "cache_read_input_tokens": 41000,
    "cache_creation_input_tokens": 3100
  }
}

Copilot AI and others added 4 commits May 22, 2026 15:06
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
This reverts commit 48040c6.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve OTel instrumentation to surface token usage from agent-stdio.log Surface OTel token usage from agent-stdio.log when proxy usage logs are missing May 22, 2026
Copilot AI requested a review from pelikhan May 22, 2026 15:14
@pelikhan pelikhan marked this pull request as ready for review May 22, 2026 16:02
Copilot AI review requested due to automatic review settings May 22, 2026 16:02
@pelikhan pelikhan merged commit 3b874be into main May 22, 2026
@pelikhan pelikhan deleted the copilot/otel-advisor-surface-token-usage branch May 22, 2026 16:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a runtime fallback so OTLP gh-aw.agent.conclusion spans still get gen_ai.usage.* token attributes when /tmp/gh-aw/agent_usage.json is missing, by extracting and normalizing usage from agent-stdio.log result events (while preserving agent_usage.json as the preferred source).

Changes:

  • Extend readAgentRuntimeMetrics() to parse result.usage from agent-stdio.log and normalize cache token field variants.
  • Prefer /tmp/gh-aw/agent_usage.json when present; otherwise fall back to runtime metrics token usage for span enrichment.
  • Add tests covering fallback behavior and precedence when both sources exist.
Show a summary per file
File Description
actions/setup/js/send_otlp_span.cjs Adds runtime token-usage normalization + fallback path for GenAI OTLP usage enrichment.
actions/setup/js/send_otlp_span.test.cjs Adds regression tests for fallback to agent-stdio.log and precedence of agent_usage.json.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[otel-advisor] OTel improvement: surface token usage from agent-stdio.log when firewall-proxy logs are absent

3 participants