{"record":{"id":"8336a91ce51de73f","repo":"zylon-ai/private-gpt","slug":"request-too-large-user-msg","errorCode":"REQUEST_TOO_LARGE_USER_MSG","errorMessage":"The message length {user_message_tokens} exceeds the maximum token limit {token_limit}.","messagePattern":"The message length (.+?) exceeds the maximum token limit (.+?)\\.","errorType":"http","errorClass":"Errors.RequestTooLarge","httpStatus":413,"severity":"error","filePath":"private_gpt/server/chat/interceptors/validator_request_interceptor.py","lineNumber":118,"sourceCode":"                    Errors.Codes.INVALID_REQUEST_AUDIO_SUPPORT_ERROR,\n                )\n            max_num_audios = max_audios_supported(llm, model_config)\n            if len(audios) > max_num_audios:\n                raise Errors.InvalidRequest(\n                    f\"The LLM supports a maximum of {max_num_audios} audios, but the message contains {len(audios)}\",\n                    Errors.Codes.INVALID_REQUEST_AUDIO_MAX_NUM_ERROR,\n                )\n\n        token_limit = context.state.runtime.effective_token_limit\n        tokenize = context.state.runtime.tokenizer_fn\n        if token_limit is None or tokenize is None:\n            return\n\n        user_message_tokens = len(\n            await async_tokenizer(texts=user_text, tokenizer_fn=tokenize)\n        )\n        if user_message_tokens > token_limit:\n            raise Errors.RequestTooLarge(\n                f\"The message length {user_message_tokens} exceeds the maximum token limit {token_limit}.\",\n                Errors.Codes.REQUEST_TOO_LARGE_USER_MSG,\n            )\n\n        # If a system message is present in the request messages, it's a misuse\n        potential_system_message = self._system_message_text(\n            context.state.input.request.messages\n        )\n        if potential_system_message:\n            raise RuntimeError(\n                \"System messages should be as layer in the context stack.\"\n            )\n\n        # Prefer system prompt from the context stack, fall back to prompt\n        system_prompt_block = (\n            context.state.input.context_stack.to_system_prompt()\n            or request.system.get_prompt()\n            or None","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/zylon-ai/private-gpt/blob/4a030776a31a901ad80b1bf4d7faa2c1a367efbb/private_gpt/server/chat/interceptors/validator_request_interceptor.py#L100-L136","documentation":"Raised when the tokenized length of the last user message's text exceeds context.state.runtime.effective_token_limit. The interceptor uses the runtime tokenizer_fn via async_tokenizer and raises Errors.RequestTooLarge (code REQUEST_TOO_LARGE_USER_MSG) before the request reaches the LLM. This is a hard pre-flight guard against oversized prompts.","triggerScenarios":"Sending a user message whose extracted text (all TextBlocks joined) tokenizes to more tokens than effective_token_limit; e.g. pasting a huge document into the chat input when the effective limit is small (embedding/model limits, configured lngcs context, or reduced limits from context-stack layers).","commonSituations":"Long document pasted into chat; effective_token_limit lowered by model config or context-stack system prompt reservation; using a tokenizer_fn that does not match the model so counts are inflated; RAG context injected into the user message rather than the context stack.","solutions":["Trim or summarize the user message text so its token count is under effective_token_limit.","Raise effective_token_limit in settings/model config if the target model supports a larger context.","Move large content into attachments/context-stack layers instead of inline user text.","Pre-count tokens with the same runtime tokenizer_fn before sending and chunk the request."],"exampleFix":"// before\nawait chat_facade.create_chat_event_generator(request=huge_text_request)  # 200k tokens vs 8k limit\n\n// after\ntokens = await async_tokenizer(texts=user_text, tokenizer_fn=tokenize)\nif tokens and len(tokens) > effective_token_limit:\n    user_text = user_text[: int(len(user_text) * effective_token_limit / len(tokens))]","handlingStrategy":"validation","validationCode":"user_text = ValidatorRequestInterceptor._extract_text(last_user_message)\ntokens = len(await async_tokenizer(texts=user_text, tokenizer_fn=tokenize))\nif token_limit is not None and tokens > token_limit:\n    raise ValueError('trim before sending')","typeGuard":null,"tryCatchPattern":"try:\n    await chat_facade.create_chat_event_generator(request=request)\nexcept Errors.RequestTooLarge:\n    # truncate to a safe character estimate and retry once\n    request.messages[-1] = shorten(request.messages[-1], token_limit)","preventionTips":["Count tokens with the same tokenizer_fn before sending","Stream long documents through ingestion/RAG instead of pasting","Watch effective_token_limit — it can shrink with context layers"],"tags":["validation","tokens","context-window","chat","request"],"backgroundTag":null,"analyzedSha":"4a030776a31a901ad80b1bf4d7faa2c1a367efbb","analyzedAt":"2026-08-15T03:51:26.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}