Bug: Sudden Context Overflow from Large Tool Results (no preflight guard)
Summary
OpenClaw can exceed model context in a single step when a tool returns a very large payload.
After that, requests fail repeatedly with:
prompt is too long: ... > 200000
input length and max_tokens exceed context limit: ... + 32000 > 200000
In several cases, no automatic compaction happened before the next request, so sessions got stuck in a failure loop.
Environment
- OpenClaw:
2026.1.30
- Model:
anthropic/claude-sonnet-4-20250514 and anthropic/claude-opus-4-5
- Context window:
200000
What happened
Large single toolResult messages were appended directly to session transcript and immediately pushed next prompt over limit.
Concrete examples (from local transcripts)
gateway config.schema produced a huge payload:
- transcript:
sessions/d3a0a84a-ab0f-4597-9de4-8353a67acfec.jsonl
- tool call line:
1402 (gateway {"action":"config.schema"})
- tool result line:
1403 (~560k chars JSON line)
- immediate error: line
1404 (prompt is too long: 205607 tokens > 200000)
- Grepping session logs with
tail ... ~/.openclaw/agents/main/sessions/*.jsonl produced huge outputs:
- transcript:
sessions/d3a0a84a-ab0f-4597-9de4-8353a67acfec.jsonl
- tool calls: lines
1857, 1869
- tool results: lines
1858 (~400k chars), 1870 (~409k chars)
- immediate failures: lines
1859, 1871, 1874, 1877
- Same pattern in another session:
- transcript:
sessions/276d664b-06c9-4a1a-80ad-562972d25beb.jsonl
- huge tool results on lines
32 and 34 (~416k chars each)
- repeated context-limit errors after that
- Repeated failures at high context without successful recovery:
- transcript:
sessions/7bff962a-ac31-44f3-9c17-300507453484.jsonl
- repeated errors on lines
955, 958, 961, 964, 967, 970
- message:
input length and max_tokens exceed context limit: 173629 + 32000 > 200000
Expected behavior
Before every model request, OpenClaw should guarantee:
estimated_input_tokens + requested_max_tokens <= model_context_limit
If not true, OpenClaw should automatically do one or more of:
- trigger compaction preflight,
- reduce
max_tokens dynamically,
- truncate/summarize newest oversized tool results,
- retry automatically with safe budget.
The session should not enter a repeated hard-failure loop.
Suggested fixes
- Preflight context budget check before each LLM call, based on current transcript + planned
max_tokens.
- Hard output cap per tool result inserted into transcript (character and token budget).
- Auto-summarize oversized tool outputs (store full output externally/log file, keep summary in context).
- Guardrails for high-risk commands that can return giant transcript chunks (e.g.
tail ~/.openclaw/agents/main/sessions/*.jsonl).
- Fallback retry policy:
- on
input + max_tokens error: reduce max_tokens and retry,
- if still too large: force compaction and retry once.
- Optional warning mode when any single toolResult exceeds configured threshold (e.g. 10k/20k tokens).
Why this matters
This causes:
- session stalls,
- 5x+ token usage spikes (many failed retries),
- broken automation loops until manual intervention.
Bug: Sudden Context Overflow from Large Tool Results (no preflight guard)
Summary
OpenClaw can exceed model context in a single step when a tool returns a very large payload.
After that, requests fail repeatedly with:
prompt is too long: ... > 200000input length and max_tokens exceed context limit: ... + 32000 > 200000In several cases, no automatic compaction happened before the next request, so sessions got stuck in a failure loop.
Environment
2026.1.30anthropic/claude-sonnet-4-20250514andanthropic/claude-opus-4-5200000What happened
Large single
toolResultmessages were appended directly to session transcript and immediately pushed next prompt over limit.Concrete examples (from local transcripts)
gateway config.schemaproduced a huge payload:sessions/d3a0a84a-ab0f-4597-9de4-8353a67acfec.jsonl1402(gateway {"action":"config.schema"})1403(~560k chars JSON line)1404(prompt is too long: 205607 tokens > 200000)tail ... ~/.openclaw/agents/main/sessions/*.jsonlproduced huge outputs:sessions/d3a0a84a-ab0f-4597-9de4-8353a67acfec.jsonl1857,18691858(~400k chars),1870(~409k chars)1859,1871,1874,1877sessions/276d664b-06c9-4a1a-80ad-562972d25beb.jsonl32and34(~416k chars each)sessions/7bff962a-ac31-44f3-9c17-300507453484.jsonl955,958,961,964,967,970input length and max_tokens exceed context limit: 173629 + 32000 > 200000Expected behavior
Before every model request, OpenClaw should guarantee:
estimated_input_tokens + requested_max_tokens <= model_context_limitIf not true, OpenClaw should automatically do one or more of:
max_tokensdynamically,The session should not enter a repeated hard-failure loop.
Suggested fixes
max_tokens.tail ~/.openclaw/agents/main/sessions/*.jsonl).input + max_tokenserror: reducemax_tokensand retry,Why this matters
This causes: