openclaw - 💡(How to fix) Fix sessions_spawn bloats subagent context even with lightContext:true [1 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#68767Fetched 2026-04-19 15:07:53
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

Root Cause

OpenClaw's sessions_spawn forks parent context instead of starting fresh:

  1. Parent context inheritance: Loads full conversation buffer (15-25 turns)
  2. Memory auto-recall: Injects 2-7k tokens from MEMORY.md
  3. Environment dump: Adds file tree, canvas state (~1.5k tokens)
  4. Full tool manifest: Sends 30+ tools with schemas (12-14k tokens)
  5. lightContext applied too late: Checked AFTER context is built

Pseudocode of current behavior:

ctx = buildFullContext(parent) // already 15k
if (lightContext) {
  ctx.tools = minimalTools // removes 5k
}
// Still has 10k+ from history/memory/system

Fix Action

Workaround

Added max_history_tokens: 3000 to agent config, but this only partially mitigates the issue. Root cause remains.

Code Example

ctx = buildFullContext(parent) // already 15k
if (lightContext) {
  ctx.tools = minimalTools // removes 5k
}
// Still has 10k+ from history/memory/system

---

if (lightContext) {
  ctx = buildMinimalContext(task)
} else {
  ctx = buildFullContext(parent)
}

---

// For subagent spawns:
inheritParentMessages: false,
skipMemorySearch: true,
minimalEnvironment: true

---

if (agentId === 'qwen-coder') {
  tools = ['read', 'write', 'exec'] // Only essential
}
RAW_BUFFERClick to expand / collapse

Bug Description

When spawning subagents via sessions_spawn with lightContext: true, the subagent still receives 11-32k tokens of context instead of the expected 1-2k tokens. This causes performance degradation for models with smaller context windows (e.g., Qwen 3.6 Plus).

Environment

  • OpenClaw version: 2026.4.15
  • Model: Qwen 3.6 Plus
  • Spawn config: lightContext: false initially, then true after investigation

Evidence

Token counts observed:

  • Simple task (lightContext: true): 11.5k input tokens
  • Complex task (lightContext: true): 21.6k - 32.9k input tokens
  • Expected with lightContext: 1-2k tokens

Comparison:

  • Claude Code CLI + OmniRoute + Qwen: Works perfectly (~2k tokens)
  • OpenClaw + OmniRoute + Qwen: Fails (11-32k tokens)

This isolates the issue to OpenClaw's sessions_spawn implementation, not the model or router.

Root Cause

OpenClaw's sessions_spawn forks parent context instead of starting fresh:

  1. Parent context inheritance: Loads full conversation buffer (15-25 turns)
  2. Memory auto-recall: Injects 2-7k tokens from MEMORY.md
  3. Environment dump: Adds file tree, canvas state (~1.5k tokens)
  4. Full tool manifest: Sends 30+ tools with schemas (12-14k tokens)
  5. lightContext applied too late: Checked AFTER context is built

Pseudocode of current behavior:

ctx = buildFullContext(parent) // already 15k
if (lightContext) {
  ctx.tools = minimalTools // removes 5k
}
// Still has 10k+ from history/memory/system

Proposed Fix

1. Apply lightContext BEFORE building context

if (lightContext) {
  ctx = buildMinimalContext(task)
} else {
  ctx = buildFullContext(parent)
}

2. Disable parent context inheritance for subagents

// For subagent spawns:
inheritParentMessages: false,
skipMemorySearch: true,
minimalEnvironment: true

3. Filter tools per agent

if (agentId === 'qwen-coder') {
  tools = ['read', 'write', 'exec'] // Only essential
}

Reproduction Steps

  1. Spawn qwen-coder with lightContext: true
  2. Provide complex task (e.g., generate 150-line Python file)
  3. Observe token count in logs: 11-32k instead of 1-2k
  4. Compare with Claude Code CLI using same model/router: works with ~2k tokens

Impact

  • Subagents with smaller context windows fail or underperform
  • Increased API costs due to bloated context
  • Inconsistent behavior vs other tools (Claude Code CLI)

Workaround

Added max_history_tokens: 3000 to agent config, but this only partially mitigates the issue. Root cause remains.

Files to Edit (for contributors)

Likely locations:

  • src/sessions/spawn.ts - Context building logic
  • src/context/builder.ts - lightContext implementation
  • src/tools/manifest.ts - Tool filtering

Additional Context

Investigation conducted with muse-planner deep analysis. Token counts verified across multiple spawn attempts.

Expected behavior: lightContext:true should result in ~1-2k token context (system prompt + task + minimal tools), matching Claude Code CLI behavior.

Actual behavior: lightContext:true still results in 11-32k token context due to inherited parent state.

extent analysis

TL;DR

Apply lightContext before building the context and disable parent context inheritance for subagents to reduce token count.

Guidance

  • Review the sessions_spawn implementation in src/sessions/spawn.ts to ensure lightContext is applied before building the context.
  • Consider disabling parent context inheritance for subagents by setting inheritParentMessages: false, skipMemorySearch: true, and minimalEnvironment: true.
  • Filter tools per agent to only include essential tools, as shown in the proposed fix.
  • Verify the token count in logs after applying these changes to ensure it falls within the expected range of 1-2k tokens.

Example

if (lightContext) {
  ctx = buildMinimalContext(task)
} else {
  ctx = buildFullContext(parent)
}

This example demonstrates how to apply lightContext before building the context.

Notes

The proposed fix suggests modifying the sessions_spawn implementation, but the actual changes may vary depending on the specific codebase. Additionally, the max_history_tokens workaround may not completely resolve the issue, and the root cause should be addressed for a permanent fix.

Recommendation

Apply the proposed fix by modifying the sessions_spawn implementation to apply lightContext before building the context and disable parent context inheritance for subagents. This should reduce the token count and improve performance for models with smaller context windows.

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

openclaw - 💡(How to fix) Fix sessions_spawn bloats subagent context even with lightContext:true [1 participants]