Skip to content

Commit 416ccef

Browse files
authored
fix: Fix stale context usage after compaction + usage calculation (#1548)
## Problem 1. Context usage calculation omitted output_tokens, under-reporting total token occupancy. 2. Context usage bar shows stale pre-compaction values until the next agent turn, making it look like context is still full after compaction. ## Changes 1. Include output_tokens in context usage calculation (they become input on the next turn) 2. Emit a usage_update with used:0 immediately on compact_boundary 3. Reset local usage counter on compaction so subsequent turns start fresh ## How did you test this? Manually
1 parent e63f693 commit 416ccef

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

packages/agent/src/adapters/claude/claude-agent.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,18 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
371371
switch (message.type) {
372372
case "system":
373373
if (message.subtype === "compact_boundary") {
374+
// Send used:0 immediately so the client doesn't keep showing
375+
// the stale pre-compaction context size until the next turn.
374376
lastAssistantTotalUsage = 0;
375377
promptReplayed = true;
378+
await this.client.sessionUpdate({
379+
sessionId: params.sessionId,
380+
update: {
381+
sessionUpdate: "usage_update",
382+
used: 0,
383+
size: lastContextWindowSize,
384+
},
385+
});
376386
}
377387
if (message.subtype === "local_command_output") {
378388
promptReplayed = true;
@@ -530,6 +540,11 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
530540
}
531541

532542
// Store latest assistant usage (excluding subagents)
543+
// Sum all token types as a proxy for post-turn context occupancy:
544+
// current turn's output will become next turn's input.
545+
// Note: per the Anthropic API, input_tokens excludes cache tokens —
546+
// cache_read and cache_creation are reported separately, so summing
547+
// all four fields is not double-counting.
533548
if (
534549
"usage" in message.message &&
535550
message.parent_tool_use_id === null
@@ -544,6 +559,7 @@ export class ClaudeAcpAgent extends BaseAcpAgent {
544559
};
545560
lastAssistantTotalUsage =
546561
usage.input_tokens +
562+
usage.output_tokens +
547563
usage.cache_read_input_tokens +
548564
usage.cache_creation_input_tokens;
549565

0 commit comments

Comments
 (0)