{"record":{"id":"95e6e0247739d9b0","repo":"FoundationAgents/OpenManus","slug":"request-may-exceed-input-token-limit-current-se","errorCode":null,"errorMessage":"Request may exceed input token limit (Current: {self.total_input_tokens}, Needed: {input_tokens}, Max: {self.max_input_tokens})","messagePattern":"Request may exceed input token limit \\(Current: (.+?), Needed: (.+?), Max: (.+?)\\)","errorType":"exception","errorClass":"TokenLimitExceeded","httpStatus":null,"severity":"error","filePath":"app/llm.py","lineNumber":404,"sourceCode":"        try:\n            # Check if the model supports images\n            supports_images = self.model in MULTIMODAL_MODELS\n\n            # Format system and user messages with image support check\n            if system_msgs:\n                system_msgs = self.format_messages(system_msgs, supports_images)\n                messages = system_msgs + self.format_messages(messages, supports_images)\n            else:\n                messages = self.format_messages(messages, supports_images)\n\n            # Calculate input token count\n            input_tokens = self.count_message_tokens(messages)\n\n            # Check if token limits are exceeded\n            if not self.check_token_limit(input_tokens):\n                error_message = self.get_limit_error_message(input_tokens)\n                # Raise a special exception that won't be retried\n                raise TokenLimitExceeded(error_message)\n\n            params = {\n                \"model\": self.model,\n                \"messages\": messages,\n            }\n\n            if self.model in REASONING_MODELS:\n                params[\"max_completion_tokens\"] = self.max_tokens\n            else:\n                params[\"max_tokens\"] = self.max_tokens\n                params[\"temperature\"] = (\n                    temperature if temperature is not None else self.temperature\n                )\n\n            if not stream:\n                # Non-streaming request\n                response = await self.client.chat.completions.create(\n                    **params, stream=False","sourceCodeStart":386,"sourceCodeEnd":422,"githubUrl":"https://github.com/FoundationAgents/OpenManus/blob/52a13f2a57d8c7f6737eefb02ccf569594d44273/app/llm.py#L386-L422","documentation":"Raised as TokenLimitExceeded from LLM.ask_with_tools() when the estimated input token count of system+user messages exceeds max_input_tokens minus tokens already consumed in the session (total_input_tokens). check_token_limit() compares the projected total against the cap; it is raised before any API call so no tokens are billed. The framework deliberately does not retry this exception.","triggerScenarios":"Very long conversation history accumulated via update_token_count(); large system prompt plus tool schemas; sending a huge document as the user message; small max_input_tokens configured in config.toml relative to actual prompt size.","commonSituations":"Long-running agent loops that never trim memory; pasting large files/logs into the prompt; misconfigured max_input_tokens default that is smaller than the model's real context; history plus tool definitions crossing the budget.","solutions":["Reduce the prompt: trim or summarize older memory messages before the next ask","Raise max_input_tokens in the [llm] config to match the model's context window","Shrink system prompt or tool list, or move large content out of the message into a tool that fetches on demand"],"exampleFix":"# before\n# long unbounded history -> TokenLimitExceeded\nresponse = await llm.ask_with_tools(messages, tools)\n\n# after\nwhile llm.check_token_limit(llm.count_message_tokens(llm.format_messages(messages, False))) is False:\n    messages.pop(0)  # drop oldest non-system message\nresponse = await llm.ask_with_tools(messages, tools)","handlingStrategy":"validation","validationCode":"def fits_budget(llm, messages, system_msgs=None) -> bool:\n    msgs = (system_msgs or []) + messages\n    return llm.check_token_limit(llm.count_message_tokens(llm.format_messages(msgs, False)))","typeGuard":null,"tryCatchPattern":"from app.llm import TokenLimitExceeded\n\ntry:\n    resp = await llm.ask_with_tools(messages, tools)\nexcept TokenLimitExceeded:\n    messages = trim_oldest(messages, keep_system=True)\n    resp = await llm.ask_with_tools(messages, tools)","preventionTips":["Trim/summarize conversation memory each N turns instead of growing it unbounded","Set max_input_tokens to the model's real context window in config","Watch llm.total_input_tokens during long runs and compact before approaching the cap"],"tags":["llm","tokens","context-window","limits"],"backgroundTag":null,"analyzedSha":"52a13f2a57d8c7f6737eefb02ccf569594d44273","analyzedAt":"2026-08-15T02:33:49.993Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}