t3code icon indicating copy to clipboard operation
t3code copied to clipboard

[Bug]: Codex Agents panel counts inherited parent history in each child token total

Open rabesss opened this issue 3 months ago • 2 comments

Before submitting

  • [x] I searched existing issues and open PRs and did not find a duplicate or pending fix.
  • [x] I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

  1. Start a long-running T3 Code thread using the Codex app-server provider.
  2. Have the parent agent spawn two or more native Codex subagents.
  3. Open the Agents panel or inspect the subagent CTA while the children are working.
  4. Compare each child's reported thread/tokenUsage/updated total with its cumulative token total at the start of the child task.

Expected behavior

Each subagent should contribute only the tokens generated by that child after it was spawned.

Summing multiple children should therefore show the combined new work performed by those children, without counting the inherited parent conversation once per child.

Actual behavior

Codex child threads begin with the parent conversation's cumulative token history. T3 treats each child's raw tokenUsage.total.totalTokens value as usage generated by that child and then sums those values in the Agents UI.

In one live reproduction with two children:

Child Starting inherited total Later raw total Actual new usage
A 5,800,940,557 5,803,101,943 2,161,386
B 5,801,183,295 5,803,507,447 2,324,152

T3 summed the raw totals:

5,803,101,943 + 5,803,507,447 = 11,606,609,390

The CTA displayed:

Σ 11606.6M

The actual combined work performed by the children was:

2,161,386 + 2,324,152 = 4,485,538

The display should therefore have been approximately Σ 4.5M.

Likely cause

Current main maps the child notification's cumulative total.totalTokens directly into RuntimeTaskUsage:

https://github.com/pingdotgg/t3code/blob/ba9c9ae81dce4e554b4dd52abfd28d0c01b5c651/apps/server/src/provider/Layers/CodexAdapter.ts#L669-L689

The CTA then sums every agent's stored total:

https://github.com/pingdotgg/t3code/blob/ba9c9ae81dce4e554b4dd52abfd28d0c01b5c651/apps/web/src/components/chat/MessagesTimeline.tsx#L2163-L2168

This assumes a Codex child's cumulative total starts at zero. In practice, the child inherits the parent's existing conversation history.

The formatter also has no billions branch, so a genuinely billion-scale input is rendered as thousands of millions:

https://github.com/pingdotgg/t3code/blob/ba9c9ae81dce4e554b4dd52abfd28d0c01b5c651/packages/client-runtime/src/state/subagentRuntime.ts#L931-L939

Suggested direction

Normalize each Codex child's cumulative counters against its inherited starting baseline before storing or summing its usage.

The implementation should preserve monotonic accounting across child follow-ups and resumed activations. Simply switching every display to tokenUsage.last may not be sufficient because that value can shrink between follow-ups.

Suggested regression coverage:

  • A newly spawned child whose cumulative counter begins at a large non-zero value.
  • Two children with different inherited baselines.
  • A resumed child that must not reset or double-count prior child work.
  • Matching totals between the Agents panel and the spawn CTA.
  • A real total above one billion formatting with a B suffix rather than thousands of M.

Related work

These are related but do not fix this path:

  • #5758 covers replayed rollout history in the Usage analytics page.
  • #5681 / #5682 cover excessive Codex subagent progress-event volume, not the values inside the snapshots.
  • #4664 introduces the orchestrator-v2 Agents model but currently retains the same raw cumulative-total assumption, so the baseline behavior should also be covered there.

Impact

Minor bug or occasional failure

The agents themselves continue working, but the Agents panel's usage reporting becomes unusable and can overstate a small amount of child work by several orders of magnitude.

Version or commit

[email protected]
Installed app commit: a20923ce4633

Also reproduced by inspection against current main at ba9c9ae81dce4e554b4dd52abfd28d0c01b5c651.

Environment

Linux x86_64
Codex app-server provider
Native Codex multi-agent/subagent notifications

Workaround

Ignore the Agents total and manually subtract each child's starting cumulative counter from its latest counter.

rabesss avatar Aug 09 '26 07:08 rabesss

This happened to me too. An agent investigated my personal Mac build and said it is unmodified upstream Agents/Codex-collab code, not a fork-only bug. Posting the extra trace here instead of opening a second issue. Hope it helps; if not, ignore or delete.

Still present on current main (a3a8cbd60). Second live case: Codex native subagents (tiny probes, including a child labeled run 2) showed ~136M tok each. Same notification had last.totalTokens = 31,908 and total.cachedInputTokens = 132,374,272. Parent context meter on that thread did not look like 136M of new child work.

Extra source notes (no patch)

Proven in current source:

  • Codex child thread/tokenUsage/updated is forwarded as collabAgent/tokenUsage, then CodexAdapter stores tokenUsage.total as typedUsage.totalTokens. Comment in tree: use total, never last, because last shrinks on follow-ups. https://github.com/pingdotgg/t3code/blob/a3a8cbd60539b4af4de8f96c892dbd07a2b6c041/apps/server/src/provider/Layers/CodexAdapter.ts#L671-L704
  • Parent Codex path does the opposite: usedTokens = last.totalTokens, totalProcessedTokens = total.totalTokens. The context meter labels those “used” vs “Total processed”. https://github.com/pingdotgg/t3code/blob/a3a8cbd60539b4af4de8f96c892dbd07a2b6c041/apps/server/src/provider/Layers/CodexAdapter.ts#L160-L178
  • Agents rows, workflow Σ, panel footer Σ, and the spawn CTA all display or sum agent.usage.totalTokens (AgentsPanel.tsx, MessagesTimeline.tsx). Client fold max-merges and does not clear usage on idle→running.
  • Captured collab wire (codexMultiAgentWire.json): child total is per child thread, not the parent thread. First child turn has last === total. Next update is the sum of last frames. Those last frames already include cached input.

Inferred: the 136M chip is Codex thread-lifetime processed tokens (cache re-counted every turn, and/or inherited parent history as in the table above), labeled as if it were this subagent’s work. Switching the headline number to last alone is not a full lifetime metric; subtracting each child’s starting total (this issue’s suggested direction) still matches the live billions case. Either way, summing raw child totals double-counts overlapping cache/history.

Not proven here: that every new spawn shares one account-global counter. Routing does not copy parent usage onto children; each child gets its own notification. The huge shared baseline is still a Codex total that does not start at zero.

Cursor / OpenCode / Grok do not emit this typedUsage path. Claude uses a different SDK total. Mobile has no Agents panel.

bald-ai avatar Aug 27 '26 08:08 bald-ai

[!NOTE] 🤖 GPT-6.1-Sol responding on behalf of Theo

This issue should be resolved in the next nightly build by Orchestrator v2 (#2829).

Please try that nightly. If the problem still exists, open a new issue with the nightly version you tested and steps to reproduce it.

t3dotgg avatar Oct 02 '26 19:10 t3dotgg