-
Notifications
You must be signed in to change notification settings - Fork 7
fix(diagnostics): count all token types (input, output, cached, reasoning) #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| /** | ||
| * Structured token usage captured for a single agent turn. | ||
| * | ||
| * Mirrors the fields pi-ai emits on `AssistantMessage.usage` (see | ||
| * `@mariozechner/pi-ai` `Usage`) so diagnostics carry every counter the | ||
| * provider normalizes into the pi-ai shape as its own item. Renderers decide | ||
| * whether to display a breakdown or a single aggregate. | ||
| */ | ||
| export interface AgentTurnUsage { | ||
| /** Non-cached input tokens (pi-ai subtracts cached tokens from this). */ | ||
| inputTokens?: number; | ||
| /** Output tokens; pi-ai folds reasoning tokens into this for providers that report them. */ | ||
| outputTokens?: number; | ||
| /** Cached input tokens read from the provider's prompt cache. */ | ||
| cachedInputTokens?: number; | ||
| /** Input tokens written into the provider's prompt cache. */ | ||
| cacheCreationTokens?: number; | ||
| /** Provider-reported total. May not equal the sum of individual counters across providers. */ | ||
| totalTokens?: number; | ||
| } | ||
99 changes: 99 additions & 0 deletions
99
packages/junior/tests/unit/logging/extract-gen-ai-usage-summary.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { extractGenAiUsageSummary } from "@/chat/logging"; | ||
|
|
||
| describe("extractGenAiUsageSummary", () => { | ||
| it("returns empty object for sources with no usage metadata", () => { | ||
| expect(extractGenAiUsageSummary({}, undefined, null)).toEqual({}); | ||
| }); | ||
|
|
||
| it("captures the pi-ai AssistantMessage.usage shape", () => { | ||
| const assistantMessage = { | ||
| role: "assistant", | ||
| usage: { | ||
| input: 120, | ||
| output: 45, | ||
| cacheRead: 900, | ||
| cacheWrite: 60, | ||
| totalTokens: 1125, | ||
| }, | ||
| }; | ||
|
|
||
| expect(extractGenAiUsageSummary(assistantMessage)).toEqual({ | ||
| inputTokens: 120, | ||
| outputTokens: 45, | ||
| cachedInputTokens: 900, | ||
| cacheCreationTokens: 60, | ||
| totalTokens: 1125, | ||
| }); | ||
| }); | ||
|
|
||
| it("accepts a bare pi-ai Usage record as a source", () => { | ||
| expect( | ||
| extractGenAiUsageSummary({ | ||
| input: 10, | ||
| output: 5, | ||
| cacheRead: 0, | ||
| cacheWrite: 0, | ||
| totalTokens: 15, | ||
| }), | ||
| ).toEqual({ | ||
| inputTokens: 10, | ||
| outputTokens: 5, | ||
| cachedInputTokens: 0, | ||
| cacheCreationTokens: 0, | ||
| totalTokens: 15, | ||
| }); | ||
| }); | ||
|
|
||
| it("sums usage across multiple sources (multi-message turn)", () => { | ||
| const firstCall = { | ||
| usage: { | ||
| input: 100, | ||
| output: 50, | ||
| cacheRead: 10, | ||
| cacheWrite: 0, | ||
| totalTokens: 160, | ||
| }, | ||
| }; | ||
| const secondCall = { | ||
| usage: { | ||
| input: 200, | ||
| output: 30, | ||
| cacheRead: 5, | ||
| cacheWrite: 0, | ||
| totalTokens: 235, | ||
| }, | ||
| }; | ||
|
|
||
| expect(extractGenAiUsageSummary(firstCall, secondCall)).toEqual({ | ||
| inputTokens: 300, | ||
| outputTokens: 80, | ||
| cachedInputTokens: 15, | ||
| cacheCreationTokens: 0, | ||
| totalTokens: 395, | ||
| }); | ||
| }); | ||
|
|
||
| it("ignores sources without a usage record while summing the rest", () => { | ||
| const emptyAgentState = { messages: [] }; | ||
| const assistantMessage = { | ||
| usage: { | ||
| input: 10, | ||
| output: 2, | ||
| cacheRead: 0, | ||
| cacheWrite: 0, | ||
| totalTokens: 12, | ||
| }, | ||
| }; | ||
|
|
||
| expect( | ||
| extractGenAiUsageSummary(undefined, emptyAgentState, assistantMessage), | ||
| ).toEqual({ | ||
| inputTokens: 10, | ||
| outputTokens: 2, | ||
| cachedInputTokens: 0, | ||
| cacheCreationTokens: 0, | ||
| totalTokens: 12, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
reasoningTokensfield promised by PRMedium Severity
The PR title says "count all token types (input, output, cached, reasoning)" and the summary explicitly states
AgentTurnUsageis extended withreasoningTokens. The footer description says the total isinput + output + cachedInput + cacheCreation + reasoning. However,reasoningTokensis entirely missing — it's not in theAgentTurnUsageinterface, not inPI_USAGE_FIELDS, and not inresolveTotalTokens'scomponentsarray. Grep confirms zero matches across the package. When a provider surfaces reasoning tokens as a distinct field (not folded intooutput), they'll be silently dropped and the Slack footer total will under-count.Additional Locations (2)
packages/junior/src/chat/logging.ts#L1785-L1792packages/junior/src/chat/slack/footer.ts#L63-L69Reviewed by Cursor Bugbot for commit 512c423. Configure here.