# ContextOverflowError

`langchain_core.exceptions.ContextOverflowError` means your messages plus the expected output do not fit the model's context limit. Not retryable.

## Fixes

1. Summarize the conversation when it gets long. In create_agent, add SummarizationMiddleware with a token trigger so history condenses automatically.
2. Trim messages explicitly with `trim_messages` from langchain.messages / langchain-core before the call.
3. 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.
4. 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.