{"record":{"id":"5f6ddbc32c7a18fe","repo":"zed-industries/zed","slug":"the-agent-is-nearing-the-end-of-its-context-window","errorCode":null,"errorMessage":"The agent is nearing the end of its context window and has been stopped. You can prompt the thread again to have the agent wrap up or hand off its work.","messagePattern":"The agent is nearing the end of its context window and has been stopped\\. You can prompt the thread again to have the agent wrap up or hand off its work\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/agent/src/agent.rs","lineNumber":3423,"sourceCode":"                                .iter()\n                                .filter_map(|c| match c {\n                                    AgentMessageContent::Text(text) => Some(text.as_str()),\n                                    _ => None,\n                                })\n                                .join(\"\\n\\n\");\n                            if content.is_empty() {\n                                None\n                            } else {\n                                Some( content)\n                            }\n                        })\n                        .context(\"No response from subagent\")\n                }),\n                SubagentPromptResult::Cancelled => Err(anyhow!(\"User canceled\")),\n                SubagentPromptResult::Error(message) => Err(anyhow!(\"{message}\")),\n                SubagentPromptResult::ContextWindowWarning => {\n                    thread.update(cx, |thread, cx| thread.cancel(cx)).await;\n                    Err(anyhow!(\n                        \"The agent is nearing the end of its context window and has been \\\n                         stopped. You can prompt the thread again to have the agent wrap up \\\n                         or hand off its work.\"\n                    ))\n                }\n            };\n\n            parent_thread\n                .update(cx, |parent_thread, cx| {\n                    parent_thread.unregister_running_subagent(&subagent_session_id, cx)\n                })\n                .ok();\n\n            result\n        })\n    }\n}\n","sourceCodeStart":3405,"sourceCodeEnd":3441,"githubUrl":"https://github.com/zed-industries/zed/blob/bc538def4545534201bbfcac4e95ac34ea6501b6/crates/agent/src/agent.rs#L3405-L3441","documentation":"When a subagent nears its context-window limit, the run is cancelled via thread.cancel and this error is returned so the parent knows work stopped early. The message tells the user they can re-prompt the thread so the agent wraps up or hands off its work.","triggerScenarios":"Subagent context usage crossing the warning threshold during a run, triggering the built-in stop-before-overflow behavior.","commonSituations":"Large-file analysis, long tool-heavy tasks, or subagents running on small-context models.","solutions":["Re-prompt the thread so the agent summarizes and hands off its state, as the message suggests.","Split the task into smaller subagent runs and hand in less context (fewer files, tighter prompts).","Use a model with a larger context window for subagents that routinely run long."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match subagent_prompt_result {\n    Err(e) if e.to_string().contains(\"nearing the end of its context window\") => {\n        // offer the user a one-click re-prompt so the agent wraps up\n    }\n    Err(e) => return Err(e),\n    Ok(output) => { /* use output */ }\n}","preventionTips":["Give subagents narrower tasks and less pasted context.","Pick larger-context models for tool-heavy subagent work.","Watch token usage and hand off before the threshold trips."],"tags":["rust","zed","agent","subagent","context-window","cancellation"],"backgroundTag":null,"analyzedSha":"bc538def4545534201bbfcac4e95ac34ea6501b6","analyzedAt":"2026-08-16T07:30:46.435Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}