Skip to content

Commit da6f65b

Browse files
committed
🤖 fix: omit providerMetadata from compaction summary to fix context display
The compaction API response's providerMetadata contains cacheCreationInputTokens from the pre-compaction context, which inflates the context usage display until the next message is sent. By omitting providerMetadata from the summary message, context usage correctly shows just the compaction output tokens. The historicalUsage field still preserves full cost accounting from pre-compaction messages. _Generated with mux_
1 parent d4c0c36 commit da6f65b

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

‎src/node/services/compactionHandler.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ describe("CompactionHandler", () => {
255255
model: "claude-3-5-sonnet-20241022",
256256
usage,
257257
duration: 2000,
258-
providerMetadata: { foo: "bar" },
258+
providerMetadata: { anthropic: { cacheCreationInputTokens: 50000 } },
259259
systemMessageTokens: 100,
260260
});
261261
await handler.handleCompletion(event);
@@ -266,14 +266,15 @@ describe("CompactionHandler", () => {
266266
});
267267
expect(summaryEvent).toBeDefined();
268268
const sevt = summaryEvent?.data.message as MuxMessage;
269+
// providerMetadata is omitted to avoid inflating context with pre-compaction cacheCreationInputTokens
269270
expect(sevt.metadata).toMatchObject({
270271
model: "claude-3-5-sonnet-20241022",
271272
usage,
272273
duration: 2000,
273-
providerMetadata: { foo: "bar" },
274274
systemMessageTokens: 100,
275275
compacted: true,
276276
});
277+
expect(sevt.metadata?.providerMetadata).toBeUndefined();
277278
});
278279

279280
it("should emit stream-end event to frontend", async () => {

‎src/node/services/compactionHandler.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ export class CompactionHandler {
112112
}
113113
const deletedSequences = clearResult.data;
114114

115-
// Create summary message with metadata
115+
// Create summary message with metadata.
116+
// We omit providerMetadata because it contains cacheCreationInputTokens from the
117+
// pre-compaction context, which inflates context usage display. The historicalUsage
118+
// field preserves full cost accounting from pre-compaction messages.
116119
const summaryMessage = createMuxMessage(
117120
`summary-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`,
118121
"assistant",
@@ -123,7 +126,6 @@ export class CompactionHandler {
123126
model: metadata.model,
124127
usage: metadata.usage,
125128
historicalUsage,
126-
providerMetadata: metadata.providerMetadata,
127129
duration: metadata.duration,
128130
systemMessageTokens: metadata.systemMessageTokens,
129131
muxMetadata: { type: "normal" },

0 commit comments

Comments
 (0)