FoundationAgents/OpenManus · error · TokenLimitExceeded

Request may exceed input token limit (Current: {self.total_i

Error message

Request may exceed input token limit (Current: {self.total_input_tokens}, Needed: {input_tokens}, Max: {self.max_input_tokens})

What it means

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.

Source

Thrown at app/llm.py:404

        try:
            # Check if the model supports images
            supports_images = self.model in MULTIMODAL_MODELS

            # Format system and user messages with image support check
            if system_msgs:
                system_msgs = self.format_messages(system_msgs, supports_images)
                messages = system_msgs + self.format_messages(messages, supports_images)
            else:
                messages = self.format_messages(messages, supports_images)

            # Calculate input token count
            input_tokens = self.count_message_tokens(messages)

            # Check if token limits are exceeded
            if not self.check_token_limit(input_tokens):
                error_message = self.get_limit_error_message(input_tokens)
                # Raise a special exception that won't be retried
                raise TokenLimitExceeded(error_message)

            params = {
                "model": self.model,
                "messages": messages,
            }

            if self.model in REASONING_MODELS:
                params["max_completion_tokens"] = self.max_tokens
            else:
                params["max_tokens"] = self.max_tokens
                params["temperature"] = (
                    temperature if temperature is not None else self.temperature
                )

            if not stream:
                # Non-streaming request
                response = await self.client.chat.completions.create(
                    **params, stream=False

View on GitHub (pinned to 52a13f2a57)

Solutions

  1. Reduce the prompt: trim or summarize older memory messages before the next ask
  2. Raise max_input_tokens in the [llm] config to match the model's context window
  3. Shrink system prompt or tool list, or move large content out of the message into a tool that fetches on demand

Example fix

# before
# long unbounded history -> TokenLimitExceeded
response = await llm.ask_with_tools(messages, tools)

# after
while llm.check_token_limit(llm.count_message_tokens(llm.format_messages(messages, False))) is False:
    messages.pop(0)  # drop oldest non-system message
response = await llm.ask_with_tools(messages, tools)
Defensive patterns

Strategy: validation

Validate before calling

def fits_budget(llm, messages, system_msgs=None) -> bool:
    msgs = (system_msgs or []) + messages
    return llm.check_token_limit(llm.count_message_tokens(llm.format_messages(msgs, False)))

Try / catch

from app.llm import TokenLimitExceeded

try:
    resp = await llm.ask_with_tools(messages, tools)
except TokenLimitExceeded:
    messages = trim_oldest(messages, keep_system=True)
    resp = await llm.ask_with_tools(messages, tools)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of FoundationAgents/OpenManus@52a13f2a57 (2026-08-15). Data as JSON: /api/errors/95e6e0247739d9b0. Report an issue: GitHub.