N9

N9 Agents

AI Agent Platform

N9 Agents Platform

A production-ready AI Agent as a Service platform with persistent memory, self-learning capabilities, and workflow automation.

Persistent Memory

Agents remember user preferences across sessions

Self-Learning

Automatic knowledge acquisition from conversations

RAG System

Semantic search over documents using embeddings

System Architecture

The platform follows a layered architecture with clear separation of concerns.

┌─────────────────────────────────────────────────────────────────┐
│                        PRESENTATION LAYER                        │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐             │
│  │  Dashboard  │  │  Chat UI    │  │  Workflow   │             │
│  │  (Admin)    │  │  (Widget)   │  │  Builder    │             │
│  └─────────────┘  └─────────────┘  └─────────────┘             │
└─────────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────────┐
│                          API LAYER                               │
│   /api/chat    /api/agents    /api/brain    /api/workflows      │
└─────────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────────┐
│                        SERVICE LAYER                             │
│   ChatService    MemoryService    KnowledgeService    RAGService │
└─────────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────────┐
│                      REPOSITORY LAYER                            │
│   AgentRepo    ConversationRepo    MemoryRepo    KnowledgeRepo  │
└─────────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────────┐
│                        DATA LAYER                                │
│   MySQL (ai_agents_db)    MySQL (buildhtml)    Cloud Storage    │
└─────────────────────────────────────────────────────────────────┘

Tech Stack

Component Technology Rationale
RuntimeNode.jsNon-blocking I/O for real-time chat
FrameworkExpress.jsLightweight, flexible routing
DatabaseMySQLACID compliance, JSON support
AI ModelGPT-4.1-miniBalance of cost and capability
Embeddingstext-embedding-3-small1536 dims, cost-effective

Memory System

The three-tier memory architecture enables agents to maintain context and learn from interactions.

┌─────────────────────────────────────────────────────────────────────────┐
│                         AGENT INTELLIGENCE                               │
├─────────────────────────────────────────────────────────────────────────┤
│                                                                          │
│   ┌─────────────────┐   ┌─────────────────┐   ┌─────────────────┐      │
│   │                 │   │                 │   │                 │      │
│   │  SHORT-TERM     │   │   LONG-TERM     │   │   KNOWLEDGE     │      │
│   │  MEMORY         │   │   MEMORY        │   │   BASE          │      │
│   │                 │   │                 │   │                 │      │
│   │  Conversation   │   │  User Profile   │   │  Q&A Pairs      │      │
│   │  History        │   │  Preferences    │   │  Documents      │      │
│   │  (Last N msgs)  │   │  Facts          │   │  (RAG)          │      │
│   │                 │   │                 │   │                 │      │
│   │  Scope: Session │   │  Scope: User    │   │  Scope: Agent   │      │
│   │  TTL: ~30 min   │   │  TTL: Permanent │   │  TTL: Permanent │      │
│   │                 │   │                 │   │                 │      │
│   └────────┬────────┘   └────────┬────────┘   └────────┬────────┘      │
│            │                     │                     │               │
│            └─────────────────────┼─────────────────────┘               │
│                                  │                                      │
│                                  ▼                                      │
│                     ┌────────────────────────┐                         │
│                     │     CONTEXT BUILDER    │                         │
│                     │  Merges all sources    │                         │
│                     └────────────────────────┘                         │
│                                  │                                      │
│                                  ▼                                      │
│                     ┌────────────────────────┐                         │
│                     │      OPENAI GPT        │                         │
│                     └────────────────────────┘                         │
│                                                                          │
└─────────────────────────────────────────────────────────────────────────┘

1. Short-Term Memory

Maintains conversation continuity within a session by storing the last N messages.

How It Works

  1. 1 User sends message
  2. 2 Message saved to messages table with conversation_id
  3. 3 On next request, last N messages retrieved
  4. 4 History included in OpenAI API call

2. Long-Term Memory

Remembers user-specific information across sessions using a key-value store.

User: "Remember that I prefer morning appointments"
                │
                ▼
┌────────────────────────────────────────────────────────────────┐
│                        AI RESPONSE                              │
│                                                                 │
│  Detects memory request and outputs:                           │
│  {"action":"save","type":"memory",                              │
│   "title":"appointment_preference",                             │
│   "content":"prefers morning appointments"}                     │
└───────────────────────────┬────────────────────────────────────┘
                            │
                            ▼
┌────────────────────────────────────────────────────────────────┐
│                     MEMORY REPOSITORY                           │
│                                                                 │
│  INSERT INTO memories                                          │
│  (agent_id, session_id, memory_key, memory_value)              │
│  VALUES (1, 'user_abc', 'appointment_preference',              │
│          'prefers morning appointments')                       │
└────────────────────────────────────────────────────────────────┘
                            │
                            ▼
┌────────────────────────────────────────────────────────────────┐
│                     FUTURE CONVERSATIONS                        │
│                                                                 │
│  Context includes:                                             │
│  ## User Information:                                          │
│  - appointment_preference: prefers morning appointments        │
└────────────────────────────────────────────────────────────────┘

3. Knowledge Base (Self-Learning)

Builds institutional knowledge from conversations through a training loop.

┌─────────────────────────────────────────────────────────────────────────┐
│                        SELF-LEARNING CYCLE                               │
└─────────────────────────────────────────────────────────────────────────┘

Step 1: User Asks Unknown Question
───────────────────────────────────
User: "What are your business hours?"
        │
        ▼
┌───────────────┐
│ Agent doesn't │────► Log to unanswered_questions
│ know answer   │      (question, asked_count)
└───────────────┘


Step 2: Owner Trains Agent
──────────────────────────
┌─────────────────────────────┐
│     TRAINING DASHBOARD      │
│                             │
│  Pending Questions:         │
│  "What are your hours?"     │
│  Asked: 15 times            │
│                             │
│  Owner provides answer:     │
│  "Mon-Fri, 9 AM to 5 PM"   │
└──────────────┬──────────────┘
               │
               ▼
┌─────────────────────────────┐
│  Add to knowledge base      │
│  Q: What are your hours?    │
│  A: Mon-Fri, 9 AM to 5 PM  │
└─────────────────────────────┘


Step 3: Agent Uses Knowledge
────────────────────────────
Future questions get accurate answers automatically

RAG Pipeline

Retrieval Augmented Generation enables semantic search over uploaded documents.

                    INGESTION PHASE
                    ───────────────

Document Upload              Chunking                    Embedding
     │                          │                            │
     ▼                          ▼                            ▼
┌─────────┐            ┌─────────────────┐           ┌─────────────────┐
│  PDF    │            │  Split into     │           │   OpenAI API    │
│  CSV    │ ────────►  │  800-char       │ ───────►  │   Embeddings    │
│  TXT    │            │  chunks         │           │   (1536 dims)   │
└─────────┘            └─────────────────┘           └────────┬────────┘
                                                              │
                                                              ▼
                                                  ┌───────────────────┐
                                                  │  Store in MySQL   │
                                                  └───────────────────┘


                    RETRIEVAL PHASE
                    ────────────────

User Query              Query Embedding              Similarity Search
     │                        │                            │
     ▼                        ▼                            ▼
┌─────────────┐       ┌─────────────────┐         ┌─────────────────┐
│ "What is    │       │   Embed query   │         │ Cosine          │
│ the refund  │ ────► │   using same    │ ─────►  │ similarity      │
│ policy?"    │       │   model         │         │ search          │
└─────────────┘       └─────────────────┘         └────────┬────────┘
                                                           │
                                                           ▼
                                                  ┌─────────────────┐
                                                  │ Inject into     │
                                                  │ AI context      │
                                                  └─────────────────┘

Document Chunking

Documents are split into overlapping chunks to preserve context across boundaries.

800
Chunk Size (chars)
200
Overlap (chars)
1536
Embedding Dims

Overlap ensures concepts spanning chunk boundaries aren't lost during retrieval.

Vector Embeddings

Text is converted to 1536-dimensional vectors that capture semantic meaning.

Input Text:
"We offer a 30-day money-back guarantee on all products"

                              │
                              ▼
                   ┌──────────────────┐
                   │  OpenAI API      │
                   │  text-embedding- │
                   │  3-small         │
                   └────────┬─────────┘
                            │
                            ▼

Output Vector (1536 dimensions):
[0.0234, -0.0891, 0.0123, 0.0567, -0.0345, ..., 0.0456]
    │        │        │        │        │           │
   Dim 1   Dim 2    Dim 3    Dim 4    Dim 5     Dim 1536

Similar texts will have similar vectors (high cosine similarity)

Cosine Similarity Search

Queries are matched against stored chunks using cosine similarity.

Query: "What is your refund policy?"
Chunk about refunds and returns 0.92
Chunk about shipping policies 0.45
Chunk about contact information 0.38

Top matching chunks are injected into the AI prompt as context.

API Reference

Chat Endpoints

POST /api/chat/:agentSlug/message
{
  "session_id": "user_abc123",
  "message": "What are your business hours?"
}

Agent Management

GET /api/agents List all agents
POST /api/agents Create agent
PUT /api/agents/:id Update agent

Workflows

POST /api/workflows/ai-build

Build workflows from natural language

{
  "message": "When someone submits a form, send me an email"
}