The AI email assistant exhibited complete memory loss between user messages, treating each email as an isolated interaction. Users attempting multi-step workflows (e.g., requesting a service followed by scheduling details) were forced to repeat context in every message, as the assistant had no memory of previous exchanges.
Issue
- Stateless architecture: Only the current email was passed to the AI model, with no reference to previous messages in the conversation thread.
- Context loss: The assistant treated each email as isolated, forgetting previous interactions and asking for redundant information.
- User frustration: Users had to repeat context in every message, creating a poor user experience.
Culprit
Three architectural gaps in the email processing pipeline caused the context loss:
- Database query limitation: The system only fetched single pending emails without retrieving conversation history.
- Tool call isolation: Only the current email was passed to the AI tool, without including previous messages in the conversation.
- Single-turn design: The AI model received only the current prompt, with no integration of past exchanges.
Solution
A user-isolated conversation history system was implemented with token budget protection:
1. User-Specific History Fetching
- Query enhancement: Modified database queries to fetch conversation history specific to each user, ensuring context isolation.
- Chronological ordering: Retrieved previous messages in reverse chronological order to maintain context flow.
2. Token Budget Protection
- Dynamic truncation: Implemented a system to dynamically truncate conversation history based on token usage, ensuring the AI model stays within token limits.
- Safety margin: Enforced an 80% safety margin to prevent token overflow and ensure robust operation.
3. Multi-Turn Integration
- History inclusion: Updated the AI model input to include both previous conversation history and the current prompt.
- Message construction: Structured messages to integrate past exchanges with the current user prompt for comprehensive context.
4. Architecture Enhancements
- User isolation: Ensured conversation history was scoped to individual users, preventing cross-user context leaks.
- Graceful fallback: Implemented error handling to maintain operation even if history retrieval fails.
This solution enabled the AI assistant to maintain conversation context across multiple user messages, improving user experience and workflow continuity while adhering to token budget constraints.