{"record":{"id":"8fb5e640ac42c850","repo":"aaif-goose/goose","slug":"failed-to-compact-context-limit-exceeded-even-aft","errorCode":null,"errorMessage":"Failed to compact: context limit exceeded even after removing all tool responses","messagePattern":"Failed to compact: context limit exceeded even after removing all tool responses","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/goose-context-management/src/summarize.rs","lineNumber":164,"sourceCode":"                // Usage must reflect the raw model output (billable tokens),\n                // so estimate before the response is rewritten to the smaller\n                // rendered summary.\n                if let Some(estimator) = estimator {\n                    ensure_usage_tokens(&mut usage, estimator, &system_prompt, &request, &response)\n                        .await;\n                }\n\n                apply_structured_summary(&mut response, &templates.summary);\n\n                return Ok(Summary {\n                    message: response,\n                    usage,\n                });\n            }\n            Err(ProviderError::ContextLengthExceeded(_))\n                if attempt < REMOVAL_PERCENTAGES.len() - 1 => {}\n            Err(ProviderError::ContextLengthExceeded(_)) => {\n                return Err(anyhow::anyhow!(\n                    \"Failed to compact: context limit exceeded even after removing all tool responses\"\n                ));\n            }\n            Err(e) => return Err(e.into()),\n        }\n    }\n\n    Err(anyhow::anyhow!(\n        \"Unexpected: exhausted all attempts without returning\"\n    ))\n}\n","sourceCodeStart":146,"sourceCodeEnd":176,"githubUrl":"https://github.com/aaif-goose/goose/blob/3810898a7447ec3299be72e223d3570a7aabf0ab/crates/goose-context-management/src/summarize.rs#L146-L176","documentation":"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.","triggerScenarios":"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).","commonSituations":"Long sessions with huge pasted text on a small-context model; planner context limit tuned down; reasoning-heavy transcripts where hidden content dominates.","solutions":["Switch to a model with a larger context window","Raise the context limit (GOOSE_PLANNER_CONTEXT_LIMIT or the model config)","Start a new session or trim the conversation before compacting","Avoid pasting very large payloads that dominate non-tool content"],"exampleFix":"# before\nexport GOOSE_PLANNER_CONTEXT_LIMIT=4096\n# after\nexport GOOSE_PLANNER_CONTEXT_LIMIT=128000","handlingStrategy":"fallback","validationCode":"let non_tool_tokens = estimate_non_tool_tokens(&messages);\nif non_tool_tokens >= context_limit {\n    // compaction cannot succeed: trim or switch model before trying\n    trim_oldest_messages(&mut messages, non_tool_tokens - context_limit + slack);\n}","typeGuard":null,"tryCatchPattern":"match compact(&messages).await {\n    Err(e) if e.to_string().contains(\"context limit exceeded even after removing\") => {\n        // fallback: start a fresh session with a summary handoff on a larger-context model\n        restart_with_summary(&model_with_bigger_context, &messages).await\n    }\n    other => other,\n}","preventionTips":["Match the model's context window to expected conversation size","Avoid pasting huge payloads into long-lived sessions","Compact early, before the conversation approaches the limit"],"tags":["context-management","compaction","token-limit","llm"],"backgroundTag":null,"analyzedSha":"3810898a7447ec3299be72e223d3570a7aabf0ab","analyzedAt":"2026-08-16T10:14:26.282Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}