Bug: compress_research removes recent messages instead of older ones on token limit

Author: ZG-666666Created Mar 9, 2026Updated Mar 9, 2026

I found a possible inconsistency in the token-limit recovery logic used by compress_research.

In deep_researcher.py, when token limit is exceeded, the comment says this is handled by removing older messages:

python
except Exception as e:
    synthesis_attempts += 1

    # Handle token limit exceeded by removing older messages
    if is_token_limit_exceeded(e, configurable.research_model):
        researcher_messages = remove_up_to_last_ai_message(researcher_messages)
        continue

    # For other errors, continue retrying
    continue

However, remove_up_to_last_ai_message() in utils.py seems to do the opposite:

def remove_up_to_last_ai_message(messages: list[MessageLikeRepresentation]) -> list[MessageLikeRepresentation]:
    """Truncate message history by removing up to the last AI message.

    This is useful for handling token limit exceeded errors by removing recent context.

    Args:
        messages: List of message objects to truncate

    Returns:
        Truncated message list up to (but not including) the last AI message
    """
    # Search backwards through messages to find the last AI message
    for i in range(len(messages) - 1, -1, -1):
        if isinstance(messages[i], AIMessage):
            # Return everything up to (but not including) the last AI message
            return messages[:i]

    # No AI messages found, return original list
    return messages

This removes the last AI message and everything after it, which looks like removing the most recent context, not the older history.

If the intent here is to free context space while preserving the latest research progress, then this may be trimming the wrong side of the message history.

Could you confirm whether the intended behavior is:

  1. remove older messages and keep newer context, or
  2. roll back the most recent AI turn?

If the intent is the former, this may be a bug.

Source: langchain-ai/open_deep_research