openclaw - ✅(Solved) Fix [Bug]: Context token / context window can be persisted incorrectly when multiple providers share the same model id, causing wrong `/status` values and potentially premature compaction [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#62472Fetched 2026-04-08 03:03:48
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
0
Author
Timeline (top)
labeled ×2commented ×1cross-referenced ×1referenced ×1

openclaw 2026.4.5 When multiple providers expose the same model id but with different contextWindow / contextTokens, OpenClaw may persist the wrong context limit into the session store.

Root Cause

In my case, several providers exposed the same model id, but one provider had a larger configured context window than the others.  After switching the session to the provider with the larger context window, /status still showed the smaller limit, and the persisted session entry contained:  { "contextTokens": 200000 }  Even though the active runtime model was using a provider configuration with a larger context window.  This appears to be caused by some context token lookup / cache paths still using the bare model id rather than a provider-qualified key.  Why this matters  This is not just a /status display issue.  A wrong persisted contextTokens value can affect:

  • /status context usage display
  • session metadata correctness
  • compaction / safeguard thresholds
  • memory flush / headroom decisions
  • long-running conversation behavior  If a larger real context window is persisted as a smaller one, OpenClaw may become overly conservative and trigger compaction / flush earlier than necessary. Config setup Use multiple providers with the same model id but different context windows.  For example:
  • provider A: same model id, smaller context window
  • provider B: same model id, larger context window  Steps
  1. Start with a session using a provider whose model has the smaller context window.
  2. Switch the session to another provider exposing the same model id but with a larger context window.
  3. Send at least one normal message so the session metadata gets updated.
  4. Run /status.
  5. Inspect the session store entry in sessions.json.  Observed
  • Active runtime model is the provider with the larger context window
  • /status may still show the smaller context window
  • persisted session entry may still contain something like:  { "contextTokens": 200000 }

Fix Action

Fixed

PR fix notes

PR #62493: fix(sessions): provider-qualified context limits (#62472)

Description (problem / solution / changelog)

Summary

  • Problem: Session usage persistence, inline directive handling, and memory-flush / preflight compaction sizing used bare model ids (lookupContextTokens / lookupCachedContextTokens) for context limits. When several providers expose the same model id with different configured context windows, the wrong limit could be written to sessionEntry.contextTokens and affect /status, compaction, and flush behavior (#62472).
  • Why it matters: Persisted contextTokens drives usage display, compaction thresholds, and related safeguards; a stale or collided value can make the runtime overly conservative or inconsistent with the active provider.
  • What changed: Use resolveContextTokensForModel({ cfg, provider, model, allowAsyncLoad: false }) in the auto-reply agent and follow-up runners, in persistInlineDirectives return value, and in resolveMemoryFlushContextWindowTokens (threading cfg + provider from the follow-up run). Follow-up runs now resolve providerUsed from agent meta when present, matching the main reply path.
  • What did NOT change: CLI updateSessionStoreAfterAgentRun already used provider-qualified resolution; gateway session row building and status logic were left as-is. The global context cache structure is unchanged; resolution goes through the existing resolveContextTokensForModel contract.

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

  • Closes #62472
  • Related #
  • This PR fixes a bug or regression

Root Cause (if applicable)

  • Root cause: Mixed strategies: resolveContextTokensForModel correctly scans cfg.models.providers per provider, but several hot paths still used bare-model cache lookups or cached tokens keyed only by model id, so duplicate ids across providers could leak the wrong window into persisted session metadata.
  • Missing detection / guardrail: Unit coverage did not assert provider-qualified resolution for resolveMemoryFlushContextWindowTokens when two providers list the same model id with different limits.
  • Contributing context (if known): N/A

Regression Test Plan (if applicable)

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file: src/auto-reply/reply/reply-state.test.ts
  • Scenario the test should lock in: Two providers in config share one model id with different contextWindow values; resolveMemoryFlushContextWindowTokens returns the limit for the requested provider.
  • Why this is the smallest reliable guardrail: Exercises the same resolveContextTokensForModel path used by reply usage persistence and memory sizing without standing up a full channel run.
  • Existing test that already covers this (if any): Partial — resolveContextTokensForModel tests in src/agents/context.lookup.test.ts; this adds coverage at the memory-flush entry point.
  • If no new test is added, why not: N/A — test added.

User-visible / Behavior Changes

  • Session contextTokens and related usage / flush thresholds should match the active provider’s configured window when multiple providers reuse the same model id.

Diagram (if applicable)

N/A

Security Impact (required)

  • New permissions/capabilities? No
  • New network endpoints or trust boundaries? No

Testing

  • pnpm exec oxlint --type-aware on touched src/auto-reply/reply/*.ts files
  • pnpm test src/auto-reply/reply/reply-state.test.ts -t resolveMemoryFlushContextWindowTokens

Changed files

  • CHANGELOG.md (modified, +1/-0)
  • src/auto-reply/reply/agent-runner-memory.ts (modified, +4/-0)
  • src/auto-reply/reply/agent-runner.ts (modified, +7/-2)
  • src/auto-reply/reply/directive-handling.persist.ts (modified, +8/-2)
  • src/auto-reply/reply/followup-runner.ts (modified, +10/-3)
  • src/auto-reply/reply/memory-flush.ts (modified, +13/-4)
  • src/auto-reply/reply/reply-state.test.ts (modified, +43/-0)
RAW_BUFFERClick to expand / collapse

Bug type

Behavior bug (incorrect output/state without crash)

Beta release blocker

No

Summary

openclaw 2026.4.5 When multiple providers expose the same model id but with different contextWindow / contextTokens, OpenClaw may persist the wrong context limit into the session store.

Steps to reproduce

 In my case, several providers exposed the same model id, but one provider had a larger configured context window than the others.  After switching the session to the provider with the larger context window, /status still showed the smaller limit, and the persisted session entry contained:  { "contextTokens": 200000 }  Even though the active runtime model was using a provider configuration with a larger context window.  This appears to be caused by some context token lookup / cache paths still using the bare model id rather than a provider-qualified key.  Why this matters  This is not just a /status display issue.  A wrong persisted contextTokens value can affect:

  • /status context usage display
  • session metadata correctness
  • compaction / safeguard thresholds
  • memory flush / headroom decisions
  • long-running conversation behavior  If a larger real context window is persisted as a smaller one, OpenClaw may become overly conservative and trigger compaction / flush earlier than necessary. Config setup Use multiple providers with the same model id but different context windows.  For example:
  • provider A: same model id, smaller context window
  • provider B: same model id, larger context window  Steps
  1. Start with a session using a provider whose model has the smaller context window.
  2. Switch the session to another provider exposing the same model id but with a larger context window.
  3. Send at least one normal message so the session metadata gets updated.
  4. Run /status.
  5. Inspect the session store entry in sessions.json.  Observed
  • Active runtime model is the provider with the larger context window
  • /status may still show the smaller context window
  • persisted session entry may still contain something like:  { "contextTokens": 200000 }

Expected behavior

  • /status should show the correct context window for the active runtime provider/model
  • persisted sessionEntry.contextTokens should match the resolved context window for the active provider/model
  • same bare model ids across providers should not contaminate each other

Actual behavior

When multiple providers expose the same model id but with different contextWindow / contextTokens, OpenClaw may persist the wrong context limit into the session store.  In my case, several providers exposed the same model id, but one provider had a larger configured context window than the others.  After switching the session to the provider with the larger context window, /status still showed the smaller limit, and the persisted session entry contained:  { "contextTokens": 200000 }  Even though the active runtime model was using a provider configuration with a larger context window.  This appears to be caused by some context token lookup / cache paths still using the bare model id rather than a provider-qualified key.

Root cause analysis  From tracing the compiled runtime, there appear to be mixed lookup strategies.  Some paths correctly resolve by provider + model There are paths using something like:  resolveContextTokensForModel({ cfg, provider, model, ... })  which can correctly prefer provider-qualified resolution.  But some paths still use bare model lookups. Examples found in the distributed build include logic equivalent to:  lookupCachedContextTokens(model) lookupContextTokens(modelUsed) lookupContextTokens(resolved.model)  where only the bare model id is used.  One especially problematic path is equivalent to:  contextTokens: agentCfg?.contextTokens ?? lookupCachedContextTokens(model) ?? 2e5  If the cache is keyed or reused by bare model id, then one provider's context value can leak into another provider's session metadata.

OpenClaw version

2026.4.5

Operating system

debian12

Install method

No response

Model

glm5、minimax-m2.7

Provider / routing chain

openclaw-minimax

Additional provider/model setup details

No response

Logs, screenshots, and evidence

Impact and severity

No response

Additional information

Additional concern  I also found that /status tends to prefer persisted session values in some cases, so once a wrong contextTokens value gets written into the session entry, it can continue to override or distort later display.  So the problem seems to be a combination of:

  1. bare-model context lookup / cache collisions
  2. incorrect session persistence
  3. status/runtime precedence rules that keep showing the stale value

extent analysis

TL;DR

To fix the issue, update the context token lookup and cache paths to use provider-qualified keys instead of bare model IDs.

Guidance

  • Identify and modify all instances of bare model ID lookups (e.g., lookupCachedContextTokens(model)) to use provider-qualified keys.
  • Update the resolveContextTokensForModel function to consistently prefer provider-qualified resolution.
  • Verify that the session store entry is updated correctly after switching providers by checking the contextTokens value in sessions.json.
  • Test the fix by repeating the steps to reproduce the issue and checking that the correct context window is displayed in /status and persisted in the session store.

Example

// Before
contextTokens: agentCfg?.contextTokens ?? lookupCachedContextTokens(model) ?? 2e5

// After
contextTokens: agentCfg?.contextTokens ?? lookupCachedContextTokens(`${provider}-${model}`) ?? 2e5

Notes

The fix may require updates to multiple parts of the codebase, and thorough testing is necessary to ensure that all instances of bare model ID lookups are addressed.

Recommendation

Apply the workaround by updating the context token lookup and cache paths to use provider-qualified keys, as this should resolve the issue without requiring an upgrade to a fixed version.

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

  • /status should show the correct context window for the active runtime provider/model
  • persisted sessionEntry.contextTokens should match the resolved context window for the active provider/model
  • same bare model ids across providers should not contaminate each other

Still need to ship something?

×6

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

Back to top recommendations

TRENDING