kmail.at
← learning

langchain · difficulty ◆◆

Handling ContextWindowExceededError with BadRequestError in langchain-openai

A known error type you can catch, instead of a confusing generic failure.

A support bot after 50 exchanges is one bad request away from a crash - unless you catch BadRequestError.

2026-08-10 · 8 min read

$ pip install -U langchain-openai==1.4.2

What it does

When you send a conversation to OpenAI that exceeds the model\u2019s context window, OpenAI returns a ContextWindowExceededError. In langchain-openai v1.4.2 the SDK properly surfaces and handles this error instead of silently swallowing it or throwing a generic one. You can catch it explicitly with openai.BadRequestError and take action - truncating the conversation or switching to a model with a larger context window.

Why it matters

Context window errors are among the most common runtime failures in production LLM apps - multi-turn chatbots, summarization pipelines, or any system where history grows unboundedly. Before this fix you might have seen a confusing generic error or unexpected behavior. Now you get a clear, catchable error with a known type, making error handling explicit and maintainable.

Example

$ A 200-exchange chat loop that eventually overflows
Context window exceeded — error caught explicitly: BadRequestError: 400
Context window exceeded — message: This model's maximum context window is 128000 tokens.
Action: truncate or summarise conversation history.

The key is the explicit BadRequestError is raised instead of a generic exception.

$ Recover by checking the error message for context_window
try:
    response = chat_model.invoke(messages)
except openai.BadRequestError as e:
    if "context_window" in str(e).lower():
        messages = summarise_history(messages)
        response = chat_model.invoke(messages)

Common flags

invoke
Primary method to send messages and get a response.
BadRequestError
Exception raised when the request is rejected, including context window.
MessagesPlaceholder
Dynamically inject trimmed conversation history.

History

A support-bot reality

A customer support bot appending every message to history hits the ceiling after roughly 30-50 exchanges on GPT-4o\u2019s 128k context. The fix gives you a clean seam to summarize or trim at that boundary, turning an inevitable failure into a handled one.

Fun facts

Pros & cons

pros

  • + Known, catchable error type
  • + Explicit recovery branches
  • + Straightforward to test

cons

  • − Still requires your truncation logic
  • − Message-string matching is brittle

Takeaways

  1. 1Catch BadRequestError around invoke calls.
  2. 2Check for context_window in the message to branch precisely.
  3. 3Implement truncation or summarization inside the except block.

Related commands

← all learning