{"record":{"id":"cee4ac74b48a1a38","repo":"run-llama/llama_index","slug":"token-limit-must-be-set-and-greater-than-0","errorCode":null,"errorMessage":"Token limit must be set and greater than 0.","messagePattern":"Token limit must be set and greater than 0\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/memory/chat_memory_buffer.py","lineNumber":43,"sourceCode":"\n    token_limit: int\n    tokenizer_fn: Callable[[str], List] = Field(\n        default_factory=get_tokenizer,\n        exclude=True,\n    )\n\n    @classmethod\n    def class_name(cls) -> str:\n        \"\"\"Get class name.\"\"\"\n        return \"ChatMemoryBuffer\"\n\n    @model_validator(mode=\"before\")\n    @classmethod\n    def validate_memory(cls, values: dict) -> dict:\n        # Validate token limit\n        token_limit = values.get(\"token_limit\", -1)\n        if token_limit < 1:\n            raise ValueError(\"Token limit must be set and greater than 0.\")\n\n        # Validate tokenizer -- this avoids errors when loading from json/dict\n        tokenizer_fn = values.get(\"tokenizer_fn\")\n        if tokenizer_fn is None:\n            values[\"tokenizer_fn\"] = get_tokenizer()\n\n        return values\n\n    @classmethod\n    def from_defaults(\n        cls,\n        chat_history: Optional[List[ChatMessage]] = None,\n        llm: Optional[LLM] = None,\n        chat_store: Optional[BaseChatStore] = None,\n        chat_store_key: str = DEFAULT_CHAT_STORE_KEY,\n        token_limit: Optional[int] = None,\n        tokenizer_fn: Optional[Callable[[str], List]] = None,\n        **kwargs: Any,","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/memory/chat_memory_buffer.py#L25-L61","documentation":"ChatMemoryBuffer enforces a positive token_limit via a pydantic model_validator: values default to -1 when absent, and anything below 1 (unset, 0, negative) fails with this ValueError. The limit is essential because the buffer's whole job is evicting messages once the token count exceeds it.","triggerScenarios":"Constructing ChatMemoryBuffer() directly (not via from_defaults) without token_limit; passing token_limit=0 or a negative number; deserializing a dict/JSON that lacks the token_limit field.","commonSituations":"Copying examples that use ChatMemoryBuffer.from_defaults but switching to the raw constructor; persisting memory to JSON with a serializer that drops defaults; passing token_limit=None explicitly.","solutions":["Use ChatMemoryBuffer.from_defaults(llm=llm) — it derives token_limit from the LLM context window or DEFAULT_TOKEN_LIMIT.","Pass an explicit positive value: ChatMemoryBuffer(token_limit=3000).","When loading from a dict, ensure token_limit survived serialization; re-add it before constructing."],"exampleFix":"# before\nmemory = ChatMemoryBuffer()  # ValueError: unset token_limit\n# after\nmemory = ChatMemoryBuffer.from_defaults(llm=llm)\n# or\nmemory = ChatMemoryBuffer(token_limit=3000)","handlingStrategy":"validation","validationCode":"def is_valid_memory_config(token_limit) -> bool:\n    return isinstance(token_limit, int) and token_limit >= 1\n\nassert is_valid_memory_config(kwargs.get(\"token_limit\", -1)), \"token_limit must be a positive int\"","typeGuard":"def has_valid_token_limit(memory_dict: dict) -> bool:\n    tl = memory_dict.get(\"token_limit\", -1)\n    return isinstance(tl, int) and tl >= 1","tryCatchPattern":"try:\n    memory = ChatMemoryBuffer(**data)\nexcept ValueError as e:\n    if \"Token limit\" in str(e):\n        data[\"token_limit\"] = 3000  # sensible default\n        memory = ChatMemoryBuffer(**data)\n    else:\n        raise","preventionTips":["Prefer from_defaults() which fills a token limit automatically.","Validate persisted memory dicts contain a positive token_limit before reload.","Treat a missing token_limit in config as a load error, not a default."],"tags":["llama-index","memory","chat-history","validation","configuration"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}