Skip to content

Bug: sessions.json totalTokens not updated after compaction #14979

@agent007-agileinsights

Description

@agent007-agileinsights

Summary

After compaction (both manual /compact and auto-compaction), the totalTokens field in sessions.json is never updated. This causes openclaw sessions and the dashboard to permanently display the pre-compaction token count, even after successful compaction and subsequent agent turns.

Reproduction

  1. Let a session accumulate to high context usage (e.g. 181k/200k = 91%)
  2. Run /compact — agent confirms compaction succeeded (reports ~5%)
  3. Send a follow-up message to trigger a real agent turn
  4. Run openclaw sessions or check the dashboard
  5. Session still shows 181k/200k (91%)
$ openclaw sessions
Kind   Key                           Age       Model            Tokens (ctx %)
group  agent:main:teleg...topic:30   7h ago    claude-sonnet-4-5  181k/200k (91%)

Meanwhile the agent itself reports context at ~5% post-compaction.

Expected behavior

totalTokens in the session store should be updated after compaction completes, so that openclaw sessions and the dashboard reflect the actual current context usage.

Root cause (suspected)

The compaction path writes the compacted transcript but does not call back into the session store to update totalTokens. The field is only written during regular agent turn completion, and even then appears to retain the historical high-water mark rather than the current context size.

Related: #5457 (stale token counts in flush logic) — same underlying issue of totalTokens not reflecting actual context.

Environment

  • OpenClaw 2026.2.9 (33c75cb)
  • macOS Darwin 25.2.0
  • Affected models: claude-sonnet-4-5 (200k), claude-opus-4-6 (200k)
  • Config: compaction.mode: "safeguard", contextTokens: 80000

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions