zylon-ai/private-gpt · error · ValueError

Condensed chat history exceeds maximum length after applying

Error message

Condensed chat history exceeds maximum length after applying condensation strategy.

What it means

Raised by the condenser after a condensation strategy has run: the resulting chat history is re-tokenized and still exceeds max_length. This is a post-condition check — the strategy (truncation, summarization, etc.) was applied but did not shrink the history enough. It usually means max_length is smaller than the irreducible content (system prompt + preserved last user message + minimal context).

Source

Thrown at private_gpt/components/chat/processors/chat_history/memory/strategies/condenser.py:825

                tokenizer_fn=tokenizer,
                chat_history=chat_history,
                max_length=max_length,
                left_tokens=left_tokens,
                last_user_tokens=last_user_message_tokens,
                right_tokens=right_tokens,
            )

        # After condensation, we ensure that the last user message is preserved
        # and the chat history is within the maximum length
        if not any(msg.role == "user" for msg in chat_history):
            raise ValueError("No user messages found after condensation.")

        if not last_user_message:
            raise ValueError("No last user message found after condensation.")

        tokens = await self._get_messages_tokens(chat_history, tokenizer_fn=tokenizer)
        if tokens > max_length:
            raise ValueError(
                "Condensed chat history exceeds maximum length after applying condensation strategy."
            )

        return chat_history

View on GitHub (pinned to 4a030776a3)

Solutions

  1. Increase max_length so it exceeds system messages + last user message token count
  2. Shrink the system prompt or move static instructions out of the condensed history
  3. Switch to a more aggressive condensation strategy (deeper truncation / summarization)
  4. If the last user message alone exceeds the budget, reject the request earlier with a clear message before condensation runs

Example fix

# before
condensed = await strategy.get_memory(chat_history, max_length=2048)

# after
# ensure budget > system + last-user tokens
condensed = await strategy.get_memory(chat_history, max_length=8192)
Defensive patterns

Strategy: validation

Validate before calling

system_and_last_user = system_messages + [last_user]
baseline = await estimate_token_count(system_and_last_user, tokenizer_fn=tokenizer)
if baseline >= max_length:
    raise ConfigError(f"max_length {max_length} too small; baseline is {baseline}")

Try / catch

try:
    condensed = await strategy.get_memory(chat_history, max_length=max_length)
except ValueError as e:
    if "exceeds maximum length" in str(e):
        # retry once with a larger budget or fall back to hard truncation
        condensed = hard_truncate(chat_history, max_length)
    else:
        raise

Prevention

When it happens

Trigger: Calling condense_strategy.get_memory() (or the wrapper that calls _get_messages_tokens afterwards) with a max_length smaller than system messages + last user message tokens; using a strategy that keeps too much context (e.g. keep-N with large N); an extremely long single user message that is preserved verbatim.

Common situations: Small context-window models configured via settings; a very large system prompt eating the budget; tokenizer_fn mismatch between what the strategy estimated and the final count; strategy 'retain-all' style configs.

Related errors


AI-assisted analysis of zylon-ai/private-gpt@4a030776a3 (2026-08-15). Data as JSON: /api/errors/28051f3eac78dee6. Report an issue: GitHub.