Skip to content

Irreversible context compaction when swapping to a model with a smaller context window #98426

Description

Bug Report / Feature Request: Irreversible Context Compaction on Model Swap

Filed: 2026-07-01
Project: OpenClaw Gateway
Type: Bug + Feature Request
Severity: Medium-High (silent data loss, affects all multi-model fallback chains)


Summary

When the fallback chain swaps from a large-context model (e.g. DeepSeek V4 Flash, 1M tokens) to a smaller-context model (e.g. Kimi K2.6 at 128K, GPT 5.4 Mini at 128K, Haiku 4.5 at 200K), the session context is compacted and permanently truncated to fit the smaller window. When the chain later swaps back to a large-context model (e.g. GPT 5.4 at 1M, Opus 4.8 at 200K), the compacted context does not regrow — the lost tokens are gone forever.

This creates a one-way compaction trap that disproportionately punishes users who strategically use cheaper large-context models for data gathering and then swap to premium reasoning models for judgment.


Problem Detail

The Fallback Chain

DeepSeek V4 Flash (1M tok) → Kimi K2.6 (128K tok) → GPT 5.4 Mini (128K tok) → 
Claude Haiku 4.5 (200K tok) → GPT 5.4 (1M tok) → Claude Sonnet 4.6 (200K tok)

What Happens

  1. ✅ User works happily in DeepSeek with 850K tokens of context (market data, analysis, decision history)
  2. ❌ DeepSeek times out or fails → fallback to Kimi K2.6 (128K)
  3. ❌ OpenClaw compresses 850K → 128K. Structured data, intermediate reasoning, order book snapshots, news classifications — lost
  4. ❌ Kimi completes the turn. Context is now permanently 128K
  5. ❌ Even if the chain later routes to GPT 5.4 (1M window) or Claude Opus (200K), the context never grows back. The prompt sent to GPT 5.4 is still the 128K compacted version

Why This Is Wrong

The current behavior assumes context compaction is monotonic shrinking — once you compact, you stay small. But in a multi-model fallback chain where later models have larger or equal context windows, this assumption is incorrect. The user loses value because:

  • The expensive model never sees the full picture. You pay Opus/GPT 5.4 Pro rates for a model operating on half the context it could handle.
  • Strategic tiered routing is penalized. The sensible workflow (cheap large-ctx gatherer → expensive reasoning model) is actively broken by blind compaction.
  • The loss is silent. No warning, no error, no indication that 85% of the session context was discarded.

Expected Behavior

Option A (Ideal — backward compatible): Each model in the fallback chain should receive context truncated to its maximum window, but the pre-compaction full context should be preserved in the session store. When the chain routes to a model with a larger window, the full context should be restored.

Option B (Minimal fix): Provide a configurable compaction.preserveFullContext: true flag that prevents permanent truncation. When enabled, the session store retains the original uncompressed tokens and re-hydrates them for any subsequent model whose context window supports it.

Option C (Documentation/API): At minimum, emit a diagnostic warning when compaction discards more than 50% of context, including "X tokens discarded — Y% of session context lost due to fallback to [model name]"


Current Workaround (In-House)

Before swapping to a "bigger brain" premium model (higher reasoning, smaller context window), the session context is pre-split into digestible chunks sized to fit the target model's window. The premium model receives structured, self-contained chunks rather than a single compacted blob. All information is preserved — it's divided into pieces the target model can process, rather than blindly compressed into a smaller window.

Downsides: Requires pre-processing logic to split and rejoin cleanly; doesn't cover the automatic fallback chain; adds latency before the swap.


Reproduction Steps

  1. Configure a fallback chain: deepseek/deepseek-v4-flash → moonshot/kimi-k2.6
  2. Start a session in DeepSeek, accumulate ~500K tokens of context
  3. Let DeepSeek fail/error, forcing a fallback to Kimi
  4. After the fallback, check the session context size — it will be ≤128K
  5. If the chain later routes to a 1M model (e.g. GPT 5.4), the context is still ≤128K
  6. The lost context is unrecoverable

Environment

  • OpenClaw version: 2026.6.11 (e085fa1)
  • Fallback models involved: DeepSeek V4 Flash (1M), Kimi K2.6 (128K), GPT 5.4 Mini (128K), Haiku 4.5 (200K), GPT 5.4 (1M), Sonnet 4.6 (200K)
  • Session type: Isolated cron + main dashboard

Suggested Implementation Sketch

// In compaction logic, check if the next model supports more context than the compacted size
interface CompactionConfig {
  preserveFullContext: boolean;  // default false for backward compat
  compactionWarningThreshold: number;  // emit warning if >X% discarded, default 50
}

// When compaction occurs:
if (nextModel.contextWindow > currentCompactSize && preserveFullContext) {
  // Don't permanently discard the full transcript
  // Mark the session with a "truncated" flag but keep the original
  sessionEntry.fullContextAvailable = true;
}

// When a model with sufficient window is selected:
if (model.contextWindow >= originalContextSize && sessionEntry.fullContextAvailable) {
  // Re-hydrate the full context from the pre-compaction snapshot
}

Related

  • In-house workaround documented at: Sentinel workspace → docs/context-handoff-protocol.md
  • GitHub upstream feature request reference (similar concern): apply same principle to multi-model fallback context management

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal backlog priority with limited blast radius.clawsweeper:needs-live-reproClawSweeper needs live local, crabbox, or manual validation to confirm this issue.clawsweeper:needs-maintainer-reviewClawSweeper marked this issue as needing maintainer review before automation.clawsweeper:needs-product-decisionClawSweeper marked this issue as needing a product or behavior decision.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.impact:auth-providerAuth, provider routing, model choice, or SecretRef resolution may break.impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🐚 platinum hermitGood issue quality with a plausible reproduction path needing some confirmation.maturity:stableIssue affects a taxonomy feature currently scored M4/M5.staleMarked as stale due to inactivity

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions