Problem
handle_chat accepts any length query and passes it directly to Gemini with no cap. Pasting a full paper or very long text will hit token limits and return a cryptic error.
Fix
Add a simple guard at the top of handle_chat:
MAX_QUERY_LENGTH = int(os.getenv("MAX_QUERY_LENGTH", "2000"))
if len(query) > MAX_QUERY_LENGTH:
return f"Query too long ({len(query)} chars). Please keep it under {MAX_QUERY_LENGTH} characters."
Add MAX_QUERY_LENGTH to .env.template.