run-llama/llama_index · error · ValueError
Token limit must be set and greater than 0.
Error message
Token limit must be set and greater than 0.
What it means
Error "Token limit must be set and greater than 0." thrown in run-llama/llama_index.
Source
Thrown at llama-index-core/llama_index/core/memory/memory.py:270
exclude=True,
description="The chat store to use for storing messages.",
)
session_id: str = Field(
default_factory=generate_chat_store_key,
description="The key to use for storing messages in the chat store.",
)
@classmethod
def class_name(cls) -> str:
return "Memory"
@model_validator(mode="before")
@classmethod
def validate_memory(cls, values: dict) -> dict:
# Validate token limit
token_limit = values.get("token_limit", -1)
if token_limit < 1:
raise ValueError("Token limit must be set and greater than 0.")
tokenizer_fn = values.get("tokenizer_fn")
if tokenizer_fn is None:
values["tokenizer_fn"] = get_tokenizer()
if values.get("token_flush_size", -1) < 1:
values["token_flush_size"] = int(token_limit * 0.1)
elif values.get("token_flush_size", -1) > token_limit:
values["token_flush_size"] = int(token_limit * 0.1)
# validate all blocks have unique names
block_names = [block.name for block in values.get("memory_blocks", [])]
if len(block_names) != len(set(block_names)):
raise ValueError("All memory blocks must have unique names.")
return values
@classmethodView on GitHub (pinned to afd0fef371)
Solutions
- Pass a positive token_limit when constructing the Memory instance.
- Set token_limit to a value greater than 0, e.g. Memory.from_defaults(session_id=..., token_limit=40000).
When it happens
Trigger: Thrown at llama-index-core/llama_index/core/memory/memory.py:270 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15).
Data as JSON: /api/errors/f26e32875ab2ce03.
Report an issue: GitHub.