aaif-goose/goose · error

Failed to compact: context limit exceeded even after removin

Error message

Failed to compact: context limit exceeded even after removing all tool responses

What it means

Compaction summarizes the conversation to shrink context, retrying with progressively larger percentages of tool-response content removed (REMOVAL_PERCENTAGES). If the summarize call still returns ContextLengthExceeded on the final attempt — with effectively all tool responses removed — the loop gives up with this error instead of spinning forever.

Source

Thrown at crates/goose-context-management/src/summarize.rs:164

                // Usage must reflect the raw model output (billable tokens),
                // so estimate before the response is rewritten to the smaller
                // rendered summary.
                if let Some(estimator) = estimator {
                    ensure_usage_tokens(&mut usage, estimator, &system_prompt, &request, &response)
                        .await;
                }

                apply_structured_summary(&mut response, &templates.summary);

                return Ok(Summary {
                    message: response,
                    usage,
                });
            }
            Err(ProviderError::ContextLengthExceeded(_))
                if attempt < REMOVAL_PERCENTAGES.len() - 1 => {}
            Err(ProviderError::ContextLengthExceeded(_)) => {
                return Err(anyhow::anyhow!(
                    "Failed to compact: context limit exceeded even after removing all tool responses"
                ));
            }
            Err(e) => return Err(e.into()),
        }
    }

    Err(anyhow::anyhow!(
        "Unexpected: exhausted all attempts without returning"
    ))
}

View on GitHub (pinned to 3810898a74)

Solutions

  1. Switch to a model with a larger context window
  2. Raise the context limit (GOOSE_PLANNER_CONTEXT_LIMIT or the model config)
  3. Start a new session or trim the conversation before compacting
  4. Avoid pasting very large payloads that dominate non-tool content

Example fix

# before
export GOOSE_PLANNER_CONTEXT_LIMIT=4096
# after
export GOOSE_PLANNER_CONTEXT_LIMIT=128000
Defensive patterns

Strategy: fallback

Validate before calling

let non_tool_tokens = estimate_non_tool_tokens(&messages);
if non_tool_tokens >= context_limit {
    // compaction cannot succeed: trim or switch model before trying
    trim_oldest_messages(&mut messages, non_tool_tokens - context_limit + slack);
}

Try / catch

match compact(&messages).await {
    Err(e) if e.to_string().contains("context limit exceeded even after removing") => {
        // fallback: start a fresh session with a summary handoff on a larger-context model
        restart_with_summary(&model_with_bigger_context, &messages).await
    }
    other => other,
}

Prevention

When it happens

Trigger: Compacting a session whose non-tool content alone exceeds the model's context limit, or a context limit configured very low (for example GOOSE_PLANNER_CONTEXT_LIMIT near the 4096 floor with a large conversation).

Common situations: Long sessions with huge pasted text on a small-context model; planner context limit tuned down; reasoning-heavy transcripts where hidden content dominates.

Related errors


AI-assisted analysis of aaif-goose/goose@3810898a74 (2026-08-16). Data as JSON: /api/errors/8fb5e640ac42c850. Report an issue: GitHub.