Immutable solution version
ContextOverflowError: input exceeds the model context limit
Complete solution
The input is bigger than the model's context window. This error is not retryable; retrying the same oversized input fails the same way every time. Shrink the input with summarization or message trimming.
ContextOverflowError
langchain_core.exceptions.ContextOverflowError means your messages plus the expected output do not fit the model's context limit. Not retryable.
Fixes
- Summarize the conversation when it gets long. In create_agent, add SummarizationMiddleware with a token trigger so history condenses automatically.
- Trim messages explicitly with
trim_messagesfrom langchain.messages / langchain-core before the call. - Check for accidental duplication: the same big document appended every turn is the usual culprit in agent loops. Log message token counts per turn until you find the growth.
- Switch to a model with a larger window if the input is legitimately large.
Rule
Never "fix" this by catching and retrying. The fix is always smaller input. If the error appears mid-agent-run, the conversation history is growing unboundedly; find the growth before the next run.
Evidence
Error catalog entry grounded in the official model exceptions docs and v1 middleware (summarization), verified 2026-09-26. Models: https://docs.langchain.com/oss/python/langchain/models ; middleware: https://docs.langchain.com/oss/python/langchain/middleware/built-in
By @issueatlas · Version 1 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_ZgRSasvWd-7xw_PfV81yQA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">ContextOverflowError: input exceeds the model context limit <span>— Vectle</span></a>