Skip to content

fix(pi-coding-agent): avoid double-counting token usage and cost - #2672

Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit into
mainfrom
pi-token-rollup-fix-3a02a89a
Oct 26, 2026
Merged

Abhijeet Prasad (AbhiPrasad) merged 1 commit into
mainfrom
pi-token-rollup-fix-3a02a89a

Conversation

@AbhiPrasad

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

🤖 This text was AI-generated.

Pi Coding Agent instrumentation copied each child LLM's token usage onto the
AgentSession.prompt task span. Trace rollups counted both, doubling token totals
and estimated cost.

Before, two calls using 29 prompt tokens each produced:

AgentSession.prompt (task): 58 prompt tokens
|-- LLM call: 29 prompt tokens
`-- LLM call: 29 prompt tokens
Trace rollup: 116 prompt tokens

Keep usage on the child LLM spans and record only timing metrics on the prompt
span. Remove both the stream-result aggregation and the turn_end fallback.

AgentSession.prompt (task): timing only
|-- LLM call: 29 prompt tokens
`-- LLM call: 29 prompt tokens
Trace rollup: 58 prompt tokens

Regression coverage checks cached usage across multiple turns, deferred prompts,
and exported traces for wrapped and automatic instrumentation.

Fixes #2659
resolves https://linear.app/braintrustdata/issue/SDK-621

> 🤖 **This text was AI-generated.**

Pi Coding Agent instrumentation copied each child LLM's token usage onto the
AgentSession.prompt task span. Trace rollups counted both, doubling token totals
and estimated cost.

Before, two calls using 29 prompt tokens each produced:

```text
AgentSession.prompt (task): 58 prompt tokens
|-- LLM call: 29 prompt tokens
`-- LLM call: 29 prompt tokens
Trace rollup: 116 prompt tokens
```

Keep usage on the child LLM spans and record only timing metrics on the prompt
span. Remove both the stream-result aggregation and the turn_end fallback.

```text
AgentSession.prompt (task): timing only
|-- LLM call: 29 prompt tokens
`-- LLM call: 29 prompt tokens
Trace rollup: 58 prompt tokens
```

Regression coverage checks cached usage across multiple turns, deferred prompts,
and exported traces for wrapped and automatic instrumentation.

Fixes #2659
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T12:49:03.537582Z dc76879 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) merged commit eb36452 into main Oct 26, 2026
58 checks passed
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) deleted the pi-token-rollup-fix-3a02a89a branch October 26, 2026 15:33
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.

Pi Coding Agent instrumentation double-counts tokens and estimated cost in trace rollups (AgentSession.prompt carries summed LLM usage)

2 participants