Skip to main content

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.

MongoDB stores conversation history for each AI agent, enabling context-aware responses across multiple customer interactions.

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

1

Create MongoDB Atlas cluster

  1. Sign up at MongoDB Atlas
  2. Create a free M0 cluster or paid tier
  3. Set up database user with read/write permissions
  4. Configure network access (whitelist IP addresses)
2

Create database and collections

// Database name
picanteria_db

// Collections (created automatically by agents)
- historial_clasificador
- historial_pedidos
- historial_reservas
- historial_eventos
- historial_general
- historial_detector
3

Get connection string

From Atlas dashboard:
  1. Click ConnectConnect your application
  2. Select Node.js driver
  3. Copy connection string
mongodb+srv://username:password@cluster.mongodb.net/picanteria_db?retryWrites=true&w=majority

Connection Configuration

n8n Credentials

Store MongoDB credentials in n8n:
{
  "id": "rEzqy3CH5hFmOQ2S",
  "name": "Memoria chats Lurwis",
  "type": "mongoDb",
  "data": {
    "connectionString": "mongodb+srv://user:pass@cluster.mongodb.net/",
    "database": "picanteria_db",
    "options": {
      "retryWrites": true,
      "w": "majority"
    }
  }
}

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.
{
  "sessionIdType": "customKey",
  "sessionKey": "{{ $('Extractormensajes').first().json.from }}",
  "collectionName": "historial_clasificador",
  "databaseName": "picanteria_db",
  "contextWindowLength": 10  // Default
}

Order Agent Memory

Longest context window for complex order conversations.
{
  "sessionIdType": "customKey",
  "sessionKey": "{{ $('Extractormensajes').first().json.from }}",
  "collectionName": "historial_pedidos",
  "databaseName": "picanteria_db",
  "contextWindowLength": 25  // Extended for order details
}

Reservation Agent Memory

Medium context for table reservation details.
{
  "sessionIdType": "customKey",
  "sessionKey": "{{ $('Extractormensajes').first().json.from }}",
  "collectionName": "historial_reservas",
  "databaseName": "picanteria_db",
  "contextWindowLength": 15
}

Events Agent Memory

Handles venue rental and event planning.
{
  "sessionIdType": "customKey",
  "sessionKey": "{{ $('Extractormensajes').first().json.from }}",
  "collectionName": "historial_eventos",
  "databaseName": "picanteria_db",
  "contextWindowLength": 15
}

General Agent Memory

Short context for quick inquiries.
{
  "sessionIdType": "customKey",
  "sessionKey": "{{ $('Extractormensajes').first().json.from }}",
  "collectionName": "historial_general",
  "databaseName": "picanteria_db",
  "contextWindowLength": 10
}

Detector Agent Memory

Tracks whether customer wants to modify or query existing orders.
{
  "sessionIdType": "customKey",
  "sessionKey": "{{ $('Extractormensajes').first().json.from }}",
  "collectionName": "historial_detector",
  "databaseName": "picanteria_db",
  "contextWindowLength": 10
}

Session Keys

All agents use the customer’s phone number as the session key:
// Session key from WhatsApp message
const sessionKey = extractedMessage.from; // e.g., "51900769907"
This ensures:
  • 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:
{
  "_id": ObjectId("..."),
  "sessionId": "51900769907",
  "messages": [
    {
      "type": "human",
      "data": {
        "content": "Quiero hacer un pedido",
        "additional_kwargs": {},
        "response_metadata": {}
      }
    },
    {
      "type": "ai",
      "data": {
        "content": "¡Perfecto! ¿Qué te gustaría ordenar hoy? 🦐",
        "additional_kwargs": {},
        "response_metadata": {
          "tokenUsage": {
            "totalTokens": 45
          }
        }
      }
    }
  ],
  "createdAt": ISODate("2026-03-05T03:00:00Z"),
  "updatedAt": ISODate("2026-03-05T03:05:00Z")
}

Context Window Management

The contextWindowLength parameter controls how many previous messages are included:
AgentWindow SizeReason
Orders25 messagesComplex conversations with menu selections, modifications, confirmations
Reservations15 messagesModerate complexity (date, time, party size, contact info)
Events15 messagesEvent details, catering, special requests
Classifier10 messagesQuick intent detection
General10 messagesSimple Q&A
Detector10 messagesBinary 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:
// Delete conversations older than 30 days
db.historial_pedidos.deleteMany({
  updatedAt: { $lt: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }
});

// Archive instead of delete (recommended)
db.historial_pedidos_archive.insertMany(
  db.historial_pedidos.find({
    updatedAt: { $lt: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }
  }).toArray()
);

Monitoring Memory Usage

Query memory statistics:
db.historial_pedidos.countDocuments({})
db.historial_reservas.countDocuments({})
db.historial_general.countDocuments({})

Performance Optimization

Indexes

Create indexes for faster session lookups:
db.historial_pedidos.createIndex({ sessionId: 1 })
db.historial_pedidos.createIndex({ updatedAt: -1 })
db.historial_clasificador.createIndex({ sessionId: 1 })
db.historial_reservas.createIndex({ sessionId: 1 })
db.historial_eventos.createIndex({ sessionId: 1 })
db.historial_general.createIndex({ sessionId: 1 })
db.historial_detector.createIndex({ sessionId: 1 })

Connection Pooling

Configure connection pool size based on traffic:
{
  "connectionString": "mongodb+srv://...",
  "options": {
    "maxPoolSize": 50,
    "minPoolSize": 5,
    "maxIdleTimeMS": 30000
  }
}

Troubleshooting

  • 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://..."
  • 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
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
  • 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

LangChain Memory

Official LangChain memory documentation

Order Service

How memory is used in order processing

Build docs developers (and LLMs) love