Skip to content

claude runtime: metrics.json num_turns takes only the last result event, undercounting turns when the agent is re-invoked for background sub-agents #9228

Open

Description

@fullsend-ai-retro

What happened

On the review run for fullsend-ai/react-shopping-cart#39 (fullsend 0.54.0, claude runtime, agents fullsend-ai/agents@v0.54.0), the review agent dispatched four sub-agents in parallel (correctness, security, style, risk). The risk sub-agent finished after the main agent's first final answer.

  • The Claude Code stream (iteration-1/output.jsonl in the fullsend-review artifact) has two type: result events:
    • first: num_turns: 20, duration_ms: 144300, total_cost_usd: 1.6576
    • second: num_turns: 3, duration_ms: 20361, total_cost_usd: 1.6576
  • The harness log prints both blocks one after the other ("→ Result: success / Turns: 20 / Cost: $1.6576" and then "→ Result: success / Turns: 3 / Cost: $1.6576"), with identical token totals.
  • The run's metrics.json records "num_turns": 3, alongside "tool_calls": 32 and the cumulative cost and tokens.

So the cost and token fields are cumulative, but num_turns (and likely any duration taken from the result event) comes only from the last result segment, the short continuation after the background sub-agent returned.

What could go better

metrics.json underreported turns by about 85% (3 against at least 20 real turns) on a run whose cost and tool-call counts were correct. Retro analysis, cost dashboards and eval judges that read num_turns (for example the max_turns judges discussed in fullsend-ai/agents#968) would see a misleadingly cheap, short run whenever the agent is re-invoked after background sub-agents finish. This is now the normal pattern for the review and retro rosters.

Confidence: high that the two result events exist and that metrics.json matches the second one. Medium on the exact cause: I have not read the metrics-extraction code. The pattern (cumulative cost but last-segment num_turns) strongly suggests the parser overwrites rather than accumulates per-segment fields. If duration_ms is also taken from the last event, durations are undercounted too. Impact is limited to telemetry accuracy; agent behavior is unaffected.

Proposed change

In the claude runtime's result/metrics extraction (the code that parses type: result events from the Claude Code stream-json output and writes metrics.json and the "→ Result" log block):

  1. When a stream contains several result events, sum the per-segment fields (num_turns, duration_ms, duration_api_ms if used). Keep taking cumulative fields (total_cost_usd, usage/modelUsage if they are already cumulative) from the last event. Check first which fields Claude Code reports cumulatively and which per segment, and handle each one explicitly.
  2. Print a single aggregated "→ Result" block, optionally with a segments: N count, instead of one block per result event, so logs don't show conflicting turn counts.
  3. Add a unit test with a fixture stream containing two result events (20 turns then 3 turns, same cost) and assert num_turns == 23 and a single summary block.

Validation criteria

  • The unit test with a two-result-event fixture passes, with num_turns equal to the sum of the segments.
  • On the next 5 review runs that use background sub-agents (any repo), metrics.json num_turns is at least the number of main-agent assistant turns in the transcript, and the harness log shows one "→ Result" block per agent run.
  • total_cost_usd and token totals in metrics.json are unchanged compared with the last result event (no double counting).

Related issues checked

Searched: Turns mismatch result block printed twice; num_turns; metrics.json turns; multiple result events; second result event background sub-agent
Considered: #6806, fullsend-ai/agents#968, #9149, #7413
fullsend#6806: metrics zeroed when the agent exits with an error. That is a different failure mode (all metrics lost, not a wrong turn count). agents#968: eval max_turns judge variance. It is a consumer of num_turns, so it is related context, not a duplicate. fullsend#9149: the headless CLI stops pending background sub-agents after a 600s idle wait. Same background-sub-agent mechanism but a different symptom (run killed, not metrics miscounted). fullsend#7413: run summary artifact for chained workflows. Unrelated to turn accuracy.


Generated by retro agent from fullsend-ai/react-shopping-cart#39

Activity

  1. AshwinUgale commented on Nov 23, 2026

    @AshwinUgale
    Member

    I'd like to take this. In 355 Claude runs from the agents nightlies (Claude Code 2.1.306), 11 review runs wrote 2–6 result events. In all 11, num_turns is per segment (one run: 18, 2, 1, 1, 1, 4) and metrics.json has only the last: 2–11 instead of 23–30. total_cost_usd and modelUsage never drop between results, and grow in the two runs whose results weren't all written at the end, so they should stay last-wins.

    Plan: total num_turns in parseClaudeStream, so each ResultEvent carries the turns so far and recordClaudeMetrics stays last-wins. That also works with #7687, which sets a running count on the same field. I'd leave the single "→ Result" block for a follow-up.

  2. added a commit that references this issue on Dec 3, 2026
  3. added 2 commits that reference this issue on Dec 4, 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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions