{"record":{"id":"583d2a753b2e96c0","repo":"microsoft/autogen","slug":"token-limit-must-be-greater-than-0","errorCode":null,"errorMessage":"token_limit must be greater than 0.","messagePattern":"token_limit must be greater than 0\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/model_context/_token_limited_chat_completion_context.py","lineNumber":52,"sourceCode":"        tools (List[ToolSchema] | None): A list of tool schema to use in the context.\n        initial_messages (List[LLMMessage] | None): A list of initial messages to include in the context.\n\n    \"\"\"\n\n    component_config_schema = TokenLimitedChatCompletionContextConfig\n    component_provider_override = \"autogen_core.model_context.TokenLimitedChatCompletionContext\"\n\n    def __init__(\n        self,\n        model_client: ChatCompletionClient,\n        *,\n        token_limit: int | None = None,\n        tool_schema: List[ToolSchema] | None = None,\n        initial_messages: List[LLMMessage] | None = None,\n    ) -> None:\n        super().__init__(initial_messages)\n        if token_limit is not None and token_limit <= 0:\n            raise ValueError(\"token_limit must be greater than 0.\")\n        self._token_limit = token_limit\n        self._model_client = model_client\n        self._tool_schema = tool_schema or []\n\n    async def get_messages(self) -> List[LLMMessage]:\n        \"\"\"Get at most `token_limit` tokens in recent messages. If the token limit is not\n        provided, then return as many messages as the remaining token allowed by the model client.\"\"\"\n        messages = list(self._messages)\n        if self._token_limit is None:\n            remaining_tokens = self._model_client.remaining_tokens(messages, tools=self._tool_schema)\n            while remaining_tokens < 0 and len(messages) > 0:\n                middle_index = len(messages) // 2\n                messages.pop(middle_index)\n                remaining_tokens = self._model_client.remaining_tokens(messages, tools=self._tool_schema)\n        else:\n            token_count = self._model_client.count_tokens(messages, tools=self._tool_schema)\n            while token_count > self._token_limit and len(messages) > 0:\n                middle_index = len(messages) // 2","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/model_context/_token_limited_chat_completion_context.py#L34-L70","documentation":"TokenLimitedChatCompletionContext trims history to fit a token budget; token_limit is optional (None means 'use whatever the model client's remaining_tokens allows'), but if you pass an explicit limit it must be positive. A zero/negative limit can never hold even one message, so the constructor fails fast with ValueError instead of producing an eternally empty context.","triggerScenarios":"`TokenLimitedChatCompletionContext(client, token_limit=0)` or negative; computing token_limit as `max_tokens - used_tokens` which goes to 0 or below once the conversation grows; passing a config default of 0 intending 'unlimited' — for that, pass token_limit=None or omit it.","commonSituations":"Dynamic budget calculations from model context windows; configs where 0 means 'not set'; mixing up the semantics with buffer_size-based contexts.","solutions":["Omit token_limit or pass None when you want the model client's own remaining-token logic.","Otherwise pass a positive budget with headroom for at least one message plus the system prompt.","Clamp computed budgets: `limit = token_limit if token_limit and token_limit > 0 else None`."],"exampleFix":"# before\nctx = TokenLimitedChatCompletionContext(client, token_limit=max_tokens - used)  # <=0 -> ValueError\n\n# after\nremaining = max_tokens - used\nctx = TokenLimitedChatCompletionContext(\n    client, token_limit=remaining if remaining > 0 else None\n)","handlingStrategy":"validation","validationCode":"effective_limit = token_limit if (token_limit is not None and token_limit > 0) else None\nctx = TokenLimitedChatCompletionContext(client, token_limit=effective_limit)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass None (or omit) for 'use the model client's limit' — never 0.","Re-check computed budgets before constructing: remaining > 0 or default to None."],"tags":["python","autogen-core","model-context","tokens","configuration","validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}