MongoDB stores conversation history for each AI agent, enabling context-aware responses across multiple customer interactions.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/KevinhosUTP/Automatizacion-Lurwis/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The system uses MongoDB Chat Memory via LangChain to maintain separate conversation histories for different agent types:- Classifier Agent - Intent classification history
- Order Agent - Order conversation context
- Reservation Agent - Table reservation conversations
- Events Agent - Venue rental discussions
- General Agent - General inquiry history
- Detector Agent - Order modification detection
MongoDB Setup
Create MongoDB Atlas cluster
- Sign up at MongoDB Atlas
- Create a free M0 cluster or paid tier
- Set up database user with read/write permissions
- Configure network access (whitelist IP addresses)
Connection Configuration
n8n Credentials
Store MongoDB credentials in n8n:Memory Configuration by Agent
Each agent has its own memory configuration with specific session keys and context window sizes:Classifier Agent Memory
Tracks intent classification to maintain conversation flow continuity.Order Agent Memory
Longest context window for complex order conversations.Reservation Agent Memory
Medium context for table reservation details.Events Agent Memory
Handles venue rental and event planning.General Agent Memory
Short context for quick inquiries.Detector Agent Memory
Tracks whether customer wants to modify or query existing orders.Session Keys
All agents use the customer’s phone number as the session key:- Each customer has isolated conversation history
- Context persists across multiple interactions
- No data leakage between customers
Memory Document Structure
MongoDB stores conversations in this format:Context Window Management
ThecontextWindowLength parameter controls how many previous messages are included:
| Agent | Window Size | Reason |
|---|---|---|
| Orders | 25 messages | Complex conversations with menu selections, modifications, confirmations |
| Reservations | 15 messages | Moderate complexity (date, time, party size, contact info) |
| Events | 15 messages | Event details, catering, special requests |
| Classifier | 10 messages | Quick intent detection |
| General | 10 messages | Simple Q&A |
| Detector | 10 messages | Binary decision (modify vs query) |
Larger context windows consume more tokens per request. Balance between conversation continuity and cost.
Memory Cleanup
Implement periodic cleanup for old conversations:Monitoring Memory Usage
Query memory statistics:Performance Optimization
Indexes
Create indexes for faster session lookups:Connection Pooling
Configure connection pool size based on traffic:Troubleshooting
Connection failures
Connection failures
- Verify network access list includes your server IP
- Check database user has correct permissions
- Ensure connection string includes database name
- Test connection:
mongosh "mongodb+srv://..."
Memory not persisting
Memory not persisting
- Verify collection names match exactly
- Check session key is consistent (phone number format)
- Ensure database name is correct:
picanteria_db - Review n8n node execution logs for errors
Context window too short/long
Context window too short/long
Adjust
contextWindowLength based on needs:- Too short: Agent forgets important details
- Too long: High token usage, slower responses
- Optimal: Test with 10-25 messages depending on complexity
High storage usage
High storage usage
- Implement conversation archiving (30+ days old)
- Reduce context window length if acceptable
- Monitor message count per session
- Consider summarization for long conversations
Security Best Practices
- Authentication: Always use username/password or certificate auth
- Network Access: Whitelist only necessary IP addresses
- Encryption: Use TLS/SSL for connections (
mongodb+srv://) - Least Privilege: Grant only read/write on
picanteria_db - Credential Rotation: Update passwords quarterly
- Audit Logs: Enable Atlas audit logs for production
Related Resources
LangChain Memory
Official LangChain memory documentation
Order Service
How memory is used in order processing