Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ version with its date and start a fresh empty `[Unreleased]` above it.

## [Unreleased]

### Fixed

- The context usage meter no longer counts cached prompt tokens twice, so its
percentage now reflects the actual occupied portion of the selected context
window.

## [1.0.17] - 2026-10-10

### Changed
Expand Down
39 changes: 4 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@codemirror/state": "^6.5.0",
"@codemirror/view": "^6.38.6",
"@modelcontextprotocol/sdk": "~1.32.1",
"@qoder-ai/qoder-agent-sdk": "^1.0.45",
"@qoder-ai/qoder-agent-sdk": "^1.0.51",
"tslib": "^2.8.1"
},
"overrides": {
Expand Down
4 changes: 2 additions & 2 deletions src/core/types/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ export type StreamChunk =
/**
* Context window usage information.
*
* `contextTokens` is the Qoder-computed total token count in the context window:
* `inputTokens + cacheCreationInputTokens + cacheReadInputTokens`.
* `contextTokens` is the complete prompt size reported by Qoder. Cache creation
* and cache read counts are subsets of `inputTokens`, not additional context.
*
* Cache token fields are optional for older Qoder CLI responses. Feature code
* should use `contextTokens` for display, not recompute from the breakdown.
Expand Down
6 changes: 4 additions & 2 deletions src/qoder/stream/transform-qoder-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ function toPromptUsageSnapshot(usage: MessageUsage): PromptUsageSnapshot {
inputTokens,
cacheCreationInputTokens,
cacheReadInputTokens,
contextTokens: inputTokens + cacheCreationInputTokens + cacheReadInputTokens,
// qodercli normalizes input_tokens to the complete prompt size. Cache read
// and creation counts are subsets used for billing detail, not extra context.
contextTokens: inputTokens,
};
}

Expand All @@ -327,7 +329,7 @@ function mergePromptUsage(
inputTokens,
cacheCreationInputTokens,
cacheReadInputTokens,
contextTokens: inputTokens + cacheCreationInputTokens + cacheReadInputTokens,
contextTokens: inputTokens,
};
}

Expand Down
22 changes: 11 additions & 11 deletions tests/unit/qoder/stream/transform-qoder-message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1522,17 +1522,17 @@ describe('transformSDKMessage', () => {
});

describe('assistant message usage extraction', () => {
it('yields usage info from main agent assistant message', () => {
it('does not double-count cache details toward context occupancy', () => {
const message = msg({
type: 'assistant',
parent_tool_use_id: null, // Main agent
message: {
content: [{ type: 'text', text: 'Hello' }],
usage: {
input_tokens: 1000,
input_tokens: 100_000,
output_tokens: 500,
cache_creation_input_tokens: 300,
cache_read_input_tokens: 200,
cache_creation_input_tokens: 10_000,
cache_read_input_tokens: 90_000,
},
},
});
Expand All @@ -1543,12 +1543,12 @@ describe('transformSDKMessage', () => {
expect(usageResults).toHaveLength(1);

const usage = (usageResults[0] as any).usage;
expect(usage.inputTokens).toBe(1000);
expect(usage.cacheCreationInputTokens).toBe(300);
expect(usage.cacheReadInputTokens).toBe(200);
expect(usage.contextTokens).toBe(1500); // 1000 + 300 + 200
expect(usage.contextWindow).toBe(200000); // Standard context window
expect(usage.percentage).toBe(1); // 1500 / 200000 * 100 rounded
expect(usage.inputTokens).toBe(100_000);
expect(usage.cacheCreationInputTokens).toBe(10_000);
expect(usage.cacheReadInputTokens).toBe(90_000);
expect(usage.contextTokens).toBe(100_000);
expect(usage.contextWindow).toBe(200_000);
expect(usage.percentage).toBe(50);
});

it('yields usage from assistant message when usage state has no stream prompt usage', () => {
Expand Down Expand Up @@ -1580,7 +1580,7 @@ describe('transformSDKMessage', () => {
cacheCreationInputTokens: 300,
cacheReadInputTokens: 200,
contextWindow: 200000,
contextTokens: 1500,
contextTokens: 1000,
percentage: 1,
});
});
Expand Down
Loading