openclaw - ✅(Solved) Fix session_status not showing cache hit tokens despite provider returning cache usage data [1 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#58522Fetched 2026-04-08 02:01:40
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Author
Timeline (top)
cross-referenced ×2closed ×1commented ×1locked ×1

The session_status tool and /status command have built-in support for displaying cache hit statistics, but the cache line is never shown because readUsageFromSessionLog() does not return cache-related fields (cacheRead, cacheWrite) from the session transcript.

Root Cause

In readUsageFromSessionLog(), the function parses usage from session logs and calls normalizeUsage() which correctly extracts cache fields. However, the return object only includes:

return {
  input,
  output,
  promptTokens,
  total,
  model
};

It should also return:

return {
  input,
  output,
  cacheRead: lastUsage.cacheRead ?? 0,
  cacheWrite: lastUsage.cacheWrite ?? 0,
  promptTokens,
  total,
  model
};

Additionally, in buildStatusMessage(), when includeTranscriptUsage is true and logUsage is available, the cache fields should be used:

if (!cacheRead || cacheRead === 0) cacheRead = logUsage.cacheRead;
if (!cacheWrite || cacheWrite === 0) cacheWrite = logUsage.cacheWrite;

PR fix notes

PR #59247: fix(status): hydrate cache usage in transcript fallback

Description (problem / solution / changelog)

Fixes #58522.

Restores cacheRead / cacheWrite in the /status transcript fallback path.

Today, buildStatusMessage() can fall back to transcript-derived usage for token counts and model info, but readUsageFromSessionLog() drops cache usage fields. That means cache usage can disappear from status output when transcript fallback is the only complete usage source.

Before:

  • transcript fallback could restore token counts
  • cache usage line could still disappear

After:

  • transcript fallback also restores cacheRead / cacheWrite
  • existing nonzero session-entry cache values are still preserved

Changes

  • status.ts
    • include cacheRead / cacheWrite in readUsageFromSessionLog()
    • hydrate missing cache fields from transcript fallback in buildStatusMessage()
  • status.test.ts
    • add regression for cache hydration from transcript fallback
    • add regression proving existing nonzero cache values are not overwritten

Verification

  • pnpm build
  • pnpm check
  • pnpm test -- src/auto-reply/status.test.ts
  • codex review --base origin/main

Backward compatible, narrow scope, no provider normalization changes.

AI-assisted: yes; author reviewed and verified the final code and tests.

Changed files

  • CHANGELOG.md (modified, +1/-0)
  • src/auto-reply/status.test.ts (modified, +57/-0)
  • src/auto-reply/status.ts (modified, +17/-1)

Code Example

🦞 OpenClaw 2026.3.13
🧮 Tokens: 17k in / 271 out · 💵 Cost: $0.12
📚 Context: 17k/200k (8%) · 🧹 Compactions: 0

---

🦞 OpenClaw 2026.3.13
🧮 Tokens: 17k in / 271 out · 💵 Cost: $0.12
🗄️ Cache: 85% hit · 15k cached, 2k new
📚 Context: 17k/200k (8%) · 🧹 Compactions: 0

---

return {
  input,
  output,
  promptTokens,
  total,
  model
};

---

return {
  input,
  output,
  cacheRead: lastUsage.cacheRead ?? 0,
  cacheWrite: lastUsage.cacheWrite ?? 0,
  promptTokens,
  total,
  model
};

---

if (!cacheRead || cacheRead === 0) cacheRead = logUsage.cacheRead;
if (!cacheWrite || cacheWrite === 0) cacheWrite = logUsage.cacheWrite;
RAW_BUFFERClick to expand / collapse

Description

The session_status tool and /status command have built-in support for displaying cache hit statistics, but the cache line is never shown because readUsageFromSessionLog() does not return cache-related fields (cacheRead, cacheWrite) from the session transcript.

Current Behavior

🦞 OpenClaw 2026.3.13
🧮 Tokens: 17k in / 271 out · 💵 Cost: $0.12
📚 Context: 17k/200k (8%) · 🧹 Compactions: 0

The 🗄️ Cache: line is missing even when using Anthropic models with prompt caching enabled.

Expected Behavior

When the provider returns cache usage data (e.g., cache_read_input_tokens, cache_creation_input_tokens from Anthropic, or cached_tokens from OpenAI), the status output should include:

🦞 OpenClaw 2026.3.13
🧮 Tokens: 17k in / 271 out · 💵 Cost: $0.12
🗄️ Cache: 85% hit · 15k cached, 2k new
📚 Context: 17k/200k (8%) · 🧹 Compactions: 0

Root Cause

In readUsageFromSessionLog(), the function parses usage from session logs and calls normalizeUsage() which correctly extracts cache fields. However, the return object only includes:

return {
  input,
  output,
  promptTokens,
  total,
  model
};

It should also return:

return {
  input,
  output,
  cacheRead: lastUsage.cacheRead ?? 0,
  cacheWrite: lastUsage.cacheWrite ?? 0,
  promptTokens,
  total,
  model
};

Additionally, in buildStatusMessage(), when includeTranscriptUsage is true and logUsage is available, the cache fields should be used:

if (!cacheRead || cacheRead === 0) cacheRead = logUsage.cacheRead;
if (!cacheWrite || cacheWrite === 0) cacheWrite = logUsage.cacheWrite;

Supporting Code References

  • normalizeUsage() already extracts cache fields correctly from multiple formats:

    • cache_read_input_tokens (Anthropic)
    • cached_tokens, prompt_tokens_details.cached_tokens (OpenAI)
    • cache_creation_input_tokens (Anthropic cache write)
  • formatCacheLine() already formats the output: 🗄️ Cache: X% hit · Y cached, Z new

Environment

  • OpenClaw version: 2026.3.13
  • Provider: Anthropic (with cacheRetention: "short" or "long")

Related Files

  • Where readUsageFromSessionLog is defined
  • Where buildStatusMessage is defined

extent analysis

TL;DR

  • Update the readUsageFromSessionLog() function to return cache-related fields (cacheRead, cacheWrite) to display cache hit statistics.

Guidance

  • Modify the readUsageFromSessionLog() function to include cacheRead and cacheWrite in its return object.
  • Update the buildStatusMessage() function to utilize the cacheRead and cacheWrite fields when constructing the status message.
  • Verify that the normalizeUsage() function correctly extracts cache fields from the session transcript.
  • Test the updated functionality with Anthropic models and prompt caching enabled to ensure the cache line is displayed as expected.

Example

return {
  input,
  output,
  cacheRead: lastUsage.cacheRead ?? 0,
  cacheWrite: lastUsage.cacheWrite ?? 0,
  promptTokens,
  total,
  model
};

Notes

  • The fix relies on the normalizeUsage() function correctly extracting cache fields from the session transcript, which is already implemented for multiple formats.
  • The formatCacheLine() function is assumed to be correctly implemented to format the cache line output.

Recommendation

  • Apply the workaround by updating the readUsageFromSessionLog() and buildStatusMessage() functions to include and utilize the cache-related fields, as this will allow the cache hit statistics to be displayed as expected.

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…

Still need to ship something?

×6

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

Back to top recommendations

TRENDING