{"record":{"id":"1e4114e19a228341","repo":"zylon-ai/private-gpt","slug":"the-message-length-user-message-tokens-and-syste","errorCode":null,"errorMessage":"The message length {user_message_tokens} and system prompt length {system_tokens} exceed the maximum token limit {token_limit}.","messagePattern":"The message length (.+?) and system prompt length (.+?) exceed the maximum token limit (.+?)\\.","errorType":"http","errorClass":"Errors.RequestTooLarge","httpStatus":413,"severity":"error","filePath":"private_gpt/server/chat/interceptors/validator_request_interceptor.py","lineNumber":160,"sourceCode":"                else []\n            )\n            if system_prompt_block\n            else None\n        )\n        system_tokens = (\n            len(await async_tokenizer(texts=system_prompt, tokenizer_fn=tokenize))\n            if system_prompt\n            else 0\n        )\n        if system_tokens > token_limit:\n            raise Errors.RequestTooLarge(\n                f\"The system prompt length {system_tokens} exceeds the maximum token limit {token_limit}.\",\n                Errors.Codes.REQUEST_TOO_LARGE_SYSTEM_MSG,\n            )\n\n        combined = user_message_tokens + system_tokens\n        if combined > token_limit:\n            raise Errors.RequestTooLarge(\n                f\"The message length {user_message_tokens} and system prompt length {system_tokens} \"\n                f\"exceed the maximum token limit {token_limit}.\"\n            )\n\n        return\n\n    @staticmethod\n    def _extract_text(message: ChatMessage) -> str:\n        \"\"\"Extract normalized text from message blocks.\"\"\"\n        parts = [\n            block.text.strip()\n            for block in message.blocks\n            if isinstance(block, TextBlock) and block.text and block.text.strip()\n        ]\n        return \"\\n\".join(parts)\n\n    @staticmethod\n    def _last_user_message(messages: list[ChatMessage]) -> ChatMessage:","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/zylon-ai/private-gpt/blob/4a030776a31a901ad80b1bf4d7faa2c1a367efbb/private_gpt/server/chat/interceptors/validator_request_interceptor.py#L142-L178","documentation":"Raised when user_message_tokens + system_tokens together exceed effective_token_limit, even though each individually passes (the per-checks at REQUEST_TOO_LARGE_USER_MSG and REQUEST_TOO_LARGE_SYSTEM_MSG did not fire). Raised as Errors.RequestTooLarge without an explicit code. It is the combined-budget guard run last in the interceptor.","triggerScenarios":"A moderately long user message plus a moderately long system prompt whose sum crosses the limit; e.g. 5k-token system prompt + 4k-token user message against an 8k effective limit.","commonSituations":"Growing system prompts (context layers) leaving shrinking headroom for user input across a session; per-component validation client-side that forgets to sum both parts.","solutions":["Trim the user message, the system prompt, or both so their combined token count is under effective_token_limit.","Raise effective_token_limit if the underlying model supports it.","Reserve a token budget for the system prompt and validate user text against (token_limit - system_tokens) before sending.","Compact or prune context-stack layers to free budget."],"exampleFix":"# before\nbudget = effective_token_limit  # validated user text alone against full limit\n\n# after\nsystem_tokens = len(await async_tokenizer(texts=system_prompt, tokenizer_fn=tokenize)) if system_prompt else 0\nbudget = effective_token_limit - system_tokens  # validate user text against remaining budget","handlingStrategy":"validation","validationCode":"budget = effective_token_limit - system_tokens\nuser_tokens = len(await async_tokenizer(texts=user_text, tokenizer_fn=tokenize))\nif user_tokens > budget:\n    user_text = user_text[:int(len(user_text) * budget / user_tokens)]","typeGuard":null,"tryCatchPattern":"try:\n    await chat_facade.create_chat_event_generator(request=request)\nexcept Errors.RequestTooLarge:\n    # reduce user text to the remaining budget and retry once","preventionTips":["Always validate against (limit - system_tokens), not the raw limit","Monitor combined size as prompts grow over a session","Compact context layers periodically"],"tags":["validation","tokens","context-window","system-prompt"],"backgroundTag":null,"analyzedSha":"4a030776a31a901ad80b1bf4d7faa2c1a367efbb","analyzedAt":"2026-08-15T03:51:26.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}