openclaw - 💡(How to fix) Fix Compaction max_tokens exceeds model output limit, crashes gateway [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#61852Fetched 2026-04-08 02:53:31
View on GitHub
Comments
1
Participants
2
Timeline
2
Reactions
0
Author
Participants
Timeline (top)
commented ×1subscribed ×1

Error Message

When compaction is triggered on long conversations, the gateway calculates max_tokens = Math.ceil(targetLength / 2), which can produce values far exceeding the model's maximum output token limit. This causes the API to return a 400 error and crashes the gateway.

Fix Action

Fix / Workaround

Current Workaround

Code Example

max_tokens = Math.min(Math.ceil(targetLength / 2), modelMaxOutputTokens)
RAW_BUFFERClick to expand / collapse

Bug Description

When compaction is triggered on long conversations, the gateway calculates max_tokens = Math.ceil(targetLength / 2), which can produce values far exceeding the model's maximum output token limit. This causes the API to return a 400 error and crashes the gateway.

Steps to Reproduce

  1. Run a bot with a model that has a lower output token limit (e.g., claude-opus-4-6 with 128K output limit)
  2. Have a long conversation that triggers compaction
  3. Gateway calculates max_tokens based on conversation length — e.g., Math.ceil(1360000 / 2) = 680000
  4. API rejects the request: max_tokens: 680000 exceeds maximum output token limit: 128000
  5. Gateway crashes, bot becomes unresponsive

Expected Behavior

max_tokens should be capped at the model's maximum output token limit:

max_tokens = Math.min(Math.ceil(targetLength / 2), modelMaxOutputTokens)

Environment

  • OpenClaw version: 2026.3.2
  • Model: claude-opus-4-6 (max output: 128K)
  • OS: macOS (Darwin 25.3.0)

Current Workaround

Changed the compaction fallback model from opus to sonnet (which has a 200K output limit), but this is a band-aid — any model with a lower output limit will hit the same issue on sufficiently long conversations.

extent analysis

TL;DR

Capping max_tokens at the model's maximum output token limit is likely to fix the gateway crash issue.

Guidance

  • Verify that the max_tokens calculation is the root cause by checking the API error message for the max_tokens limit exceeded error.
  • Update the max_tokens calculation to use Math.min to cap the value at the model's maximum output token limit, as shown in the expected behavior code snippet.
  • Test the updated calculation with different conversation lengths and model output limits to ensure the fix works as expected.
  • Consider adding a check to handle cases where the conversation length is extremely large, to prevent potential issues with the Math.ceil calculation.

Example

max_tokens = Math.min(Math.ceil(targetLength / 2), modelMaxOutputTokens)

Notes

This fix assumes that the modelMaxOutputTokens value is correctly set for each model. If this value is not available or is incorrect, additional work may be needed to retrieve or calculate the correct maximum output token limit.

Recommendation

Apply the workaround by updating the max_tokens calculation to cap the value at the model's maximum output token limit, as this directly addresses the root cause of the issue and prevents the gateway crash.

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 Compaction max_tokens exceeds model output limit, crashes gateway [1 comments, 2 participants]