๐Debugging Missionยทbeginnerยทโฑ๏ธ 20โ35mยทโญ 125 XP
M-002Fix the Context Window Overflow
Description
Nebula Corp's chatbot keeps crashing with 'context length exceeded' errors in production. The conversation manager is supposed to trim old messages when the token count approaches the model's limit โ but the trimming logic has bugs. Some conversations never get trimmed, others lose the system prompt entirely. Debug the conversation manager and make it handle long conversations gracefully.
Symptoms
wrong-outputThe system prompt is not counted toward the token budget, so conversations overflow the context window
wrong-outputThe most recent messages are removed instead of the oldest ones, causing the chatbot to lose the latest context
wrong-outputThe system message is dropped from the returned array, so the chatbot loses its personality and instructions
Expected Behavior (3)
System message is preserved
The returned messages must include the system message
Input:trimConversation([{role:'system',content:'You are helpful.'},{role:'user',content:'Hi'},{role:'assistant',content:'Hello!'}], 100)
Expected:CONTAINS:system
Oldest messages are removed first
When trimming, the most recent messages should be kept
Input:trimConversation([{role:'system',content:'Be helpful.'},{role:'user',content:'First message'},{role:'user',content:'Second message'},{role:'user',content:'Latest message'}], 50)
Expected:CONTAINS:Latest message
System prompt tokens are counted
A 400-char system prompt is ~100 tokens, which nearly fills a 120-token budget but both messages fit
Input:trimConversation([{role:'system',content:'A'.repeat(400)},{role:'user',content:'Hi'}], 120)
Expected:LENGTH:2
Related Lessons
Click Run / Check to validate your solution