{"record":{"id":"bf3a1ba24c17d9e6","repo":"Mintplex-Labs/anything-llm","slug":"llm-proxy-context-exceeded-applying-emergency","errorCode":null,"errorMessage":"[llm-proxy] Context exceeded — applying emergency compression and retrying","messagePattern":"\\[llm-proxy\\] Context exceeded — applying emergency compression and retrying","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"open-computer/services/interface-service/llm-proxy/index.js","lineNumber":230,"sourceCode":"      }\n    };\n\n    try {\n      activeLlmRequest = { startedAt: Date.now(), phase: \"connecting\" };\n      broadcast({ type: \"llm_status\", state: \"waiting\" });\n      const msgCount = req.body?.messages?.length || 0;\n      broadcast({ type: \"agent_log\", content: `[llm-proxy] → waiting for response (${msgCount} msgs)` });\n\n      let body = req.body;\n      let { upstream, optimized } = await doRequest(body);\n\n      // ── Context overflow: retry once with emergency compression ──────────\n      if (!upstream.ok && (upstream.status === 400 || upstream.status === 413)) {\n        const errPeek = await upstream.text();\n        const classified = classifyLlmError(errPeek, upstream.status);\n\n        if (classified.type === ERROR_TYPES.CONTEXT_EXCEEDED) {\n          console.warn(`[llm-proxy] Context exceeded — applying emergency compression and retrying`);\n          broadcast({ type: \"agent_log\", content: \"[llm-proxy] Context exceeded — compressing history…\" });\n\n          const compressedMessages = emergencyCompress(body.messages || [], {\n            keepRecent: 6,\n            maxToolChars: 300,\n          });\n          body = { ...body, messages: compressedMessages };\n\n          try {\n            ({ upstream, optimized } = await doRequest(body, true));\n          } catch (retryErr) {\n            const retryClassified = classifyLlmError(retryErr.message);\n            broadcastLlmError(retryClassified, retryErr.message);\n            if (!res.headersSent)\n              res.status(502).json({ error: `LLM proxy error: ${retryErr.message}` });\n            return;\n          }\n","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/3aec848f2885144aa8f1e53b9731a04310d5d558/open-computer/services/interface-service/llm-proxy/index.js#L212-L248","documentation":"The llm-proxy forwarded a request upstream and got 400/413 whose body classified as CONTEXT_EXCEEDED — the conversation (messages plus tool outputs) is larger than the model's context window. The proxy reacts by running emergencyCompress (keeping the 6 most recent messages, truncating tool results to 300 chars) and retrying once; the warning marks that path.","triggerScenarios":"Long agent sessions where accumulated tool outputs exceed the model's window (common with 4k–8k context models); a huge file pasted into the conversation; verbose browser/DOM tool results accumulating over many turns.","commonSituations":"Small-context local models used for computer-use agents; marathon sessions without history trimming; a single enormous tool payload early in the conversation.","solutions":["Switch to a model with a larger context window (32k+) for long agent sessions.","Start a new session/conversation to reset history when this recurs.","Reduce tool verbosity or the size of pasted content entering the prompt.","If it persists after compression, the retry error path will surface — treat that as the signal to change model or trim history."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"// Rough preflight estimate before sending a long conversation:\nconst approxTokens = messages.reduce((n, m) => n + Math.ceil(JSON.stringify(m).length / 4), 0);\nif (approxTokens > MODEL_CONTEXT * 0.8) {\n  messages = emergencyCompress(messages, { keepRecent: 6, maxToolChars: 300 });\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer 32k+ context models for long agent sessions.","Trim tool outputs at the source (limit DOM/result sizes) rather than late.","Restart sessions periodically instead of letting history grow unbounded.","Tune emergencyCompress keepRecent/maxToolChars to the workload."],"tags":["llm","context-window","token-limit","compression","llm-proxy"],"backgroundTag":"llm-context-length-exceeded","analyzedSha":"3aec848f2885144aa8f1e53b9731a04310d5d558","analyzedAt":"2026-08-18T10:02:21.017Z","contentChangedAt":"2026-08-18T10:02:21.017Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}