## The problem

Issue run-llama/llama_index#16235 (closed, 16 comments): ### Question Validation - [X] I have searched both the documentation and discord for an answer. ### Question Hi, I'm having this issue whenever I reach to the point that my chat exceeds X amount of tokens. I was assuming `as_chat_engine()` set up with `max_prompt` which preserves chat history, would automatically handle this situation. What am I missing ?

## The verified fix

Collaborator logan-markewich diagnosed: the 'Calculated available context size -N' error appears when a custom LLM does not declare an accurate context_window in its LLMMetadata  -  set context_window to the real model size (e.g. LLMMetadata(context_window=128000, model_name=...)) and num_output. If the chat history still overflows, lower the ChatMemoryBuffer token limit, e.g. as_chat_engine(..., memory=ChatMemoryBuffer.from_defaults(token_limit=50000)). The chat engine cannot auto-truncate when it doesn't know what the real window is.