Skip to content

[Bug]: Token usage not being recorded in session history - totalTokens shows null #21819

@zymclaw

Description

@zymclaw

Summary

Token usage data is not being recorded in session history files (.jsonl). After upgrading to v2026.2.19, multiple sessions show totalTokens: null in sessions.json, even though the sessions were active and used the API extensively.

Steps to reproduce

  1. Start multiple active sessions using MiniMax-M2.5 model
  2. Have normal conversations with significant token usage (200k context)
  3. Check session token stats via openclaw sessions list or sessions.json
  4. Observe that totalTokens shows as null for older sessions

Expected behavior

Each session should record token usage (inputTokens, outputTokens, totalTokens) from API responses in the .jsonl session files, and display accurate counts in sessions.json.

Actual behavior

  • sessions.json shows totalTokens: null and totalTokensFresh: false
  • The underlying .jsonl files contain message records without any usage field
  • The issue persists across multiple sessions

OpenClaw version

2026.2.19 (upgraded today via openclaw update)

Operating system

macOS 26.3 (arm64)

Install method

No response

Logs, screenshots, and evidence

### Evidence 1: Sessions List Shows Unknown Tokens
Session Age Model Tokens
agent:main:main just now MiniMax-M2.5 26k/200k (13%) Working
agent:main:3c00d854...380f06 38h ago MiniMax-M2.5 unknown/200k (?%)  Missing
agent:main:3fc4421f...341787 47h ago MiniMax-M2.5 unknown/200k (?%)  Missing

### Evidence 2: sessions.json Token Fields Are Null

"agent:main:3fc4421f-cf67-4133-8a13-74037f341787": {
  "totalTokens": null,
  "totalTokensFresh": null,
  "inputTokens": null,
  "outputTokens": null
}
Evidence 3: Jsonl Files Contain Zero Usage Records
# Checked ALL session .jsonl files - none contain 'usage' field
3c00d854.jsonl: 0 messages with usage
3fc4421f.jsonl: 0 messages with usage  
main.jsonl:    0 messages with usage
Evidence 4: Session Files Are Large (Proof of Usage)
3c00d854-cb35-4571-9363-91b93c380f06.jsonl  2.6MB  ← 38 hours ago
3fc4421f-cf67-4133-8a13-74037f341787.jsonl   51KB   ← 47 hours ago
95558fdc-f4dc-401e-91e0-202dffccb9cd.jsonl  137KB   ← current session
Root Cause Analysis
Token usage is tracked in-memory and stored in sessions.json, but never written to .jsonl session files:

Current session shows token data in sessions.json (real-time memory tracking)
But the .jsonl files contain zero usage fields in any message
When old sessions close, the data in sessions.json is lost (possibly cleaned up)
Since jsonl has no backup → token usage is completely lost

Impact and severity

  • Billing accuracy: Cannot track actual token consumption
  • User visibility: No historical token usage data
  • Auditing: Cannot verify API usage after session ends

Additional information

Note: The v2026.2.19 release includes:

"Agents/Billing: include the active model that produced a billing error..."

This suggests billing-related issues were known but may not have been fully fixed. This bug affects billing accuracy as token usage cannot be tracked.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions