From 4aec14152fd09fbbfabb8bbd8a540f81da82fa7e Mon Sep 17 00:00:00 2001 From: Javier Martinez Date: Thu, 16 Jul 2026 15:48:08 +0200 Subject: [PATCH] fix: add buffer to the condensation --- .../components/chat/models/chat_config_models.py | 8 ++++++++ .../chat/interceptors/condensation_interceptor.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/private_gpt/components/chat/models/chat_config_models.py b/private_gpt/components/chat/models/chat_config_models.py index 4cd33662..47908c07 100644 --- a/private_gpt/components/chat/models/chat_config_models.py +++ b/private_gpt/components/chat/models/chat_config_models.py @@ -468,6 +468,14 @@ class CondensationConfig(BaseModel): default=True, description="Whether to enable condensation in the chat.", ) + token_buffer: float = Field( + default=0.05, + ge=0.0, + lt=1.0, + description=( + "Fraction of the model token limit to reserve when condensing chat history." + ), + ) class ThinkingConfig(BaseModel): diff --git a/private_gpt/server/chat/interceptors/condensation_interceptor.py b/private_gpt/server/chat/interceptors/condensation_interceptor.py index 32739a40..2b1c6e88 100644 --- a/private_gpt/server/chat/interceptors/condensation_interceptor.py +++ b/private_gpt/server/chat/interceptors/condensation_interceptor.py @@ -37,6 +37,10 @@ logger = logging.getLogger(__name__) _SENTINEL: object = object() +def _token_limit_with_buffer(token_limit: int, token_buffer: float) -> int: + return max(1, int(token_limit * (1 - token_buffer))) + + class _CondensationResult(BaseModel): chat_history: list[ChatMessage] | None = None condensed: bool = False @@ -166,12 +170,17 @@ class CondensationRequestInterceptor(ChatRequestLoopInterceptor): if not self._enabled or not history: return + max_length = _token_limit_with_buffer( + context.state.runtime.effective_token_limit, + state.input.request.condensation.token_buffer, + ) + generator = condense_chat_history( **state.input.llm_kwargs, chat_history=history, tools=state.input.context_stack.all_tools(), strategy_type=self._strategy_type, - max_length=context.state.runtime.effective_token_limit, + max_length=max_length, tokenizer_fn=context.state.runtime.tokenizer_fn, message_to_input=context.llm.messages_to_prompt, condensation_timeout=self._condensation_timeout,