openclaw - ✅(Solved) Fix [Bug]: /status shows 0% context usage even when session has messages (totalTokens not saved to session) [4 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#67667Fetched 2026-04-17 08:29:50
View on GitHub
Comments
1
Participants
2
Timeline
11
Reactions
0
Timeline (top)
cross-referenced ×4referenced ×4labeled ×2commented ×1

Bug Description The /status command always shows Context: 0/264k (0%) even though the session has thousands of messages and has undergone compaction.

Investigation sessions.json shows totalTokens: null (never set) Compaction checkpoints contain token counts (e.g., tokensBefore: 93, tokensAfter: 21225) But these values are NOT saved to the session's totalTokens field The /status command reads totalTokens which is null, hence shows 0%

Root Cause

Bug Description The /status command always shows Context: 0/264k (0%) even though the session has thousands of messages and has undergone compaction.

Investigation sessions.json shows totalTokens: null (never set) Compaction checkpoints contain token counts (e.g., tokensBefore: 93, tokensAfter: 21225) But these values are NOT saved to the session's totalTokens field The /status command reads totalTokens which is null, hence shows 0%

Fix Action

Fixed

PR fix notes

PR #67695: fix(agents): preserve session totalTokens when provider omits usage data

Description (problem / solution / changelog)

Summary

  • Problem: /status shows Context: 0/264k (0%) even after thousands of messages and compaction, because totalTokens in the session entry is never set.
  • Root cause: When a provider (e.g. MiniMax via its Anthropic-compatible endpoint) does not include usage data (input_tokens, output_tokens) in its API response, hasNonzeroUsage() returns false and the entire totalTokens update block in persistSessionAfterRun (src/agents/command/session-store.ts) is skipped. The session entry keeps totalTokens: undefined, and /status reads this as 0%.
  • Fix: When the current run has no usage data but the session entry already has a totalTokens value (from a prior run or compaction), preserve it instead of letting it reset to undefined. The preserved value is marked totalTokensFresh: false so display layers know it is stale. This is strictly better than null — the user sees the last known context usage instead of 0%.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • Fixes #67667

Changed files

  • src/agents/command/session-store.test.ts (modified, +49/-0)
  • src/agents/command/session-store.ts (modified, +10/-0)

PR #21: fix: save compaction tokensAfter to session totalTokens (#67667)

Description (problem / solution / changelog)

Summary

The /status command shows "Context: 0/264k (0%)" even when session has messages and compaction has occurred. Investigation revealed that:

  • sessions.json shows totalTokens: null (never set)
  • Compaction checkpoints contain token counts (e.g., tokensBefore: 93, tokensAfter: 21225)
  • But these values are NOT saved to the session's totalTokens field

Root Cause

Manual compaction (via /compact command) correctly calls incrementCompactionCount to save tokensAfter, but auto-compaction triggered by context overflow in run.ts calculates compactResult.result.tokensAfter but never persists it.

Solution

  1. Added compactionTokensAfter field to EmbeddedPiAgentMeta (types.ts):

    • Carries the post-compaction token count from run.ts to session store
    • Used when API usage is unavailable to update totalTokens
  2. Track lastCompactionTokensAfter in run.ts:

    • When auto-compaction succeeds, capture compactResult.result.tokensAfter
    • Pass it to EmbeddedPiAgentMeta
  3. Updated updateSessionStoreAfterAgentRun (session-store.ts):

    • Use compactionTokensAfter as totalTokens when:
      • API usage is not available (zero usage)
      • compactionTokensAfter is a valid positive number

This ensures /status shows accurate context usage after compaction without relying on API usage which reflects pre-compaction state.

Changes

  • src/agents/pi-embedded-runner/types.ts - Added compactionTokensAfter field
  • src/agents/pi-embedded-runner/run.ts - Track and pass lastCompactionTokensAfter
  • src/commands/agent/session-store.ts - Use compactionTokensAfter for totalTokens

Testing

  • pnpm tsgo (TypeScript check): ✅ Passed
  • vitest run src/agents/pi-embedded-runner/run.overflow-compaction.test.ts: ✅ 4 passed
  • vitest run src/commands/agent/session-store.test.ts: ✅ 1 passed
  • vitest run src/auto-reply/reply/commands.test.ts: ✅ 42 passed

Linked Issue

Fixes #67667

Changed files

  • src/agents/pi-embedded-runner/run.ts (modified, +3/-0)
  • src/agents/pi-embedded-runner/types.ts (modified, +6/-0)
  • src/commands/agent/session-store.ts (modified, +4/-0)

PR #67701: fix: save compaction tokensAfter to session totalTokens (#67667)

Description (problem / solution / changelog)

Summary

The /status command shows "Context: 0/264k (0%)" even when session has messages and compaction has occurred. Investigation revealed that:

  • sessions.json shows totalTokens: null (never set)
  • Compaction checkpoints contain token counts (e.g., tokensBefore: 93, tokensAfter: 21225)
  • But these values are NOT saved to the session's totalTokens field

Root Cause

Manual compaction (via /compact command) correctly calls incrementCompactionCount to save tokensAfter, but auto-compaction triggered by context overflow in run.ts calculates compactResult.result.tokensAfter but never persists it.

Solution

  1. Added compactionTokensAfter field to EmbeddedPiAgentMeta (types.ts):

    • Carries the post-compaction token count from run.ts to session store
    • Used when API usage is unavailable to update totalTokens
  2. Track lastCompactionTokensAfter in run.ts:

    • When auto-compaction succeeds, capture compactResult.result.tokensAfter
    • Pass it to EmbeddedPiAgentMeta
  3. Updated updateSessionStoreAfterAgentRun (session-store.ts):

    • Use compactionTokensAfter as totalTokens when:
      • API usage is not available (zero usage)
      • compactionTokensAfter is a valid positive number

This ensures /status shows accurate context usage after compaction without relying on API usage which reflects pre-compaction state.

Changes

  • src/agents/pi-embedded-runner/types.ts - Added compactionTokensAfter field
  • src/agents/pi-embedded-runner/run.ts - Track and pass lastCompactionTokensAfter
  • src/commands/agent/session-store.ts - Use compactionTokensAfter for totalTokens

Testing

  • TypeScript check (npx tsc --noEmit): ✅ Passed
  • vitest run src/agents/pi-embedded-runner/run.overflow-compaction.test.ts: ✅ 4 passed
  • vitest run src/commands/agent/session-store.test.ts: ✅ 1 passed
  • vitest run src/auto-reply/reply/commands.test.ts: ✅ 42 passed

Linked Issue

Fixes #67667

Changed files

  • src/acp/translator.ts (modified, +5/-1)
  • src/agents/pi-embedded-runner/run.ts (modified, +3/-0)
  • src/agents/pi-embedded-runner/types.ts (modified, +6/-0)
  • src/commands/agent/session-store.ts (modified, +4/-0)
  • src/cron/isolated-agent/delivery-dispatch.ts (modified, +20/-1)
  • src/telegram/send.ts (modified, +7/-1)

PR #67678: fix(status): persist totalTokens after memory-flush compaction

Description (problem / solution / changelog)

Summary

The /status command incorrectly reports 0% context usage after automatic memory-flush compaction because totalTokens is never persisted.

Problem

The memory-flush compaction path in runMemoryFlushIfNeeded did not pass tokensAfter to incrementCompactionCount, so session.totalTokens was never updated after automatic compaction. The compaction engine computes tokensAfter internally, but the onAgentEvent callback in handleAutoCompactionEnd did not include the compaction result in the event data. The memory-flush path therefore had no way to propagate the post-compaction token count.

Changes

src/agents/pi-embedded-subscribe.handlers.compaction.ts

  • Forward evt.result in the onAgentEvent compaction-end callback so consumers can access tokensAfter

src/auto-reply/reply/agent-runner-memory.ts

  • Extract tokensAfter from the compaction event in runMemoryFlushIfNeeded and pass it to incrementCompactionCount

src/auto-reply/reply/agent-runner-memory.test.ts

  • Add regression test verifying tokensAfter flows through the memory flush path and is persisted to the session store
  • Replace claude-opus-4-6 with claude-sonnet-4-6 per CLAUDE.md convention (3 occurrences)

Testing

  • npx vitest run src/auto-reply/reply/agent-runner-memory.test.ts — all tests pass
  • New test validates the happy path for token count persistence after compaction

Closes #67667


[!NOTE] This contribution was made with the assistance of AI tools (GitHub Copilot, Antigravity).

Changed files

  • src/agents/pi-embedded-subscribe.handlers.compaction.ts (modified, +6/-1)
  • src/auto-reply/reply/agent-runner-memory.test.ts (modified, +68/-4)
  • src/auto-reply/reply/agent-runner-memory.ts (modified, +9/-0)
RAW_BUFFERClick to expand / collapse

Bug type

Regression (worked before, now fails)

Beta release blocker

No

Summary

Bug Description The /status command always shows Context: 0/264k (0%) even though the session has thousands of messages and has undergone compaction.

Investigation sessions.json shows totalTokens: null (never set) Compaction checkpoints contain token counts (e.g., tokensBefore: 93, tokensAfter: 21225) But these values are NOT saved to the session's totalTokens field The /status command reads totalTokens which is null, hence shows 0%

Steps to reproduce

Start a conversation with the agent Have multiple message exchanges Wait for compaction to occur Run /status - shows 0% context usage

Expected behavior

Should show the actual token count based on session message history (e.g., "Context: 21k/264k (8%)")

Actual behavior

Shows "Context: 0/264k (0%)" - the percentage is always 0%

OpenClaw version

OpenClaw version: 2026.4.12

Operating system

Ubuntu 24.4

Install method

No response

Model

Minmax-2.5

Provider / routing chain

Minmax-2.5

Additional provider/model setup details

This is NOT related to the model provider not returning usage info Token counting DOES happen during compaction (visible in checkpoint data) The bug is that the calculated token count is not persisted to totalTokens field in sessions.json

Logs, screenshots, and evidence

Impact and severity

No response

Additional information

No response

extent analysis

TL;DR

Update the session's totalTokens field with the calculated token count after compaction to fix the incorrect context usage display.

Guidance

  • Investigate why the totalTokens field in sessions.json is not being updated with the token counts from compaction checkpoints.
  • Verify that the compaction process is correctly calculating the token counts by checking the checkpoint data.
  • Modify the code to persist the calculated token count to the totalTokens field in sessions.json after compaction.
  • Test the /status command after updating the totalTokens field to ensure it displays the correct context usage percentage.

Example

No code example is provided as the issue does not include specific code snippets, but the fix would involve updating the totalTokens field in the session data structure after compaction.

Notes

The issue seems to be related to the persistence of token counts after compaction, and the fix would require modifying the code to update the totalTokens field correctly.

Recommendation

Apply a workaround to update the totalTokens field manually after compaction, or modify the code to persist the calculated token count automatically, as the root cause of the issue is the missing update of the totalTokens field.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Should show the actual token count based on session message history (e.g., "Context: 21k/264k (8%)")

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - ✅(Solved) Fix [Bug]: /status shows 0% context usage even when session has messages (totalTokens not saved to session) [4 pull requests, 1 comments, 2 participants]