Development Notes - AI Agent Server
Documents the development process, bugs, agent routing logic, and technical highlights of an AI agent server built with Express.js.
What this file does
Documents the development process, bugs, agent routing logic, and technical highlights of an AI agent server built with Express.js.
When to use it
- Learning from real-world bugs and solutions in an AI agent project
- Understanding a custom embedding and RAG retrieval implementation
- Reviewing a plugin-based architecture for intent detection and execution
- Evaluating deployment options for an Express.js AI server
Assumes this stack
Development Notes - AI Agent Server
๐ค AI-Generated vs. Manual Implementation
AI-Generated Content:
- Initial project structure and boilerplate: Used AI to scaffold the basic Express.js server setup
- TypeScript configurations: AI helped with tsconfig.json and package.json setup
- Basic prompt engineering: Initial system prompt structure was AI-assisted
- Documentation: README.md structure and API examples were AI-generated
- Error handling patterns: Standard try-catch patterns were AI-suggested
Manual Implementation:
- Custom embedding system: Built from scratch using word frequency and semantic features
- Cosine similarity algorithm: Implemented custom vector similarity calculation
- Plugin detection logic: Designed custom intent detection patterns
- Memory management: Built session-based conversation memory system
- RAG retrieval logic: Custom implementation for document chunking and retrieval
- Weather API integration: Manual OpenWeather API integration with city extraction
- Math plugin security: Implemented mathjs for safe mathematical evaluation
๐ Bugs Faced and Solutions
1. ES Module Import Issues
Problem: TypeScript ES module imports with .js extensions
Solution: Added "type": "module" to package.json and used .js extensions in imports
2. Vector Similarity Performance
Problem: Initial embedding system was too simplistic Solution: Enhanced with semantic features (markdown detection, code blocks, links) and word hashing
3. Plugin Intent Detection
Problem: Basic keyword matching missed complex queries Solution: Implemented regex patterns for math detection and comprehensive weather keywords
4. Memory Persistence
Problem: In-memory storage lost data on server restart Solution: Designed for session-based memory (can be extended to database storage)
5. Document Chunking
Problem: Large documents created poor retrieval results Solution: Implemented paragraph-based chunking with size limits (500 chars)
๐ Agent Routing Logic
Message Flow:
- Input Validation: Check for required
session_idandmessage - Memory Retrieval: Get last 2 conversation turns for context
- RAG Processing:
- Embed user message using custom embedding function
- Calculate cosine similarity with all document chunks
- Select top 3 most relevant chunks with diversity
- Plugin Detection:
- Weather: Check for weather-related keywords
- Math: Use regex to detect mathematical expressions
- Plugin Execution: Run detected plugins and capture output
- Prompt Assembly: Combine memory, context, and plugin outputs
- LLM Call: Send to Groq API with structured prompt
- Response Processing: Save to memory and return to user
Key Design Decisions:
- Custom Embeddings: Built lightweight embedding system for better control
- Session Memory: Simple in-memory storage for conversation continuity
- Plugin Architecture: Extensible system for adding new capabilities
- Error Resilience: Graceful fallbacks when plugins or LLM calls fail
๐ฏ Technical Highlights
Vector Search Implementation:
- Custom 100-dimensional embeddings using word frequency and semantic features
- Cosine similarity for document retrieval
- Diversity-based selection to avoid similar chunks
Plugin System:
- Intent detection using keyword matching and regex patterns
- Extensible architecture for adding new plugins
- Secure math evaluation using mathjs library
Memory Management:
- Session-based conversation history
- Automatic cleanup of old sessions (can be extended)
- Context injection for conversation continuity
๐ Deployment Considerations
The project is ready for deployment on:
- Render: Add
startscript and environment variables - Railway: Direct deployment from GitHub
- Vercel: Serverless function deployment
- Replit: Direct deployment with environment setup
Environment variables needed:
GROQ_API_KEY: For LLM API accessOPENWEATHER_API_KEY: For weather pluginPORT: Server port (optional, defaults to 3000)
What's inside
7 sections covering AI vs manual implementation, 5 bugs, routing flow, technical highlights, and deployment considerations.
Change this for your project
- Replace
GROQ_API_KEYwith your own LLM API key - Replace
OPENWEATHER_API_KEYwith your own weather API key - Replace
pratham-developer/ai-agent-serverwith your own repository name
Where it goes
Reference documentation for a retrieval pipeline. Keep with the ingestion or retrieval code it describes.
Worth borrowing
- Diversity-based chunk selection to avoid retrieving similar content
- Session-based memory with automatic cleanup for conversation continuity
- Plugin detection using keyword matching and regex for extensibility
Related Documents
SUMMARY
Proposes three on-prem AI architectures, modular, hybrid, and fully local RAG, with hardware specs and vendor lists.
Retrieval & Prompts
Explains how CharMemory's extraction prompt and Vector Storage settings determine memory retrieval quality in SillyTavern.
Advanced RAG Techniques
Surveys advanced RAG architectures, Agentic, Corrective, Self-RAG, Graph RAG, RAPTOR, Multimodal, Temporal, with design trade-offs and decision guidance.
RFC-BLite: High-Performance Embedded Document Database for .NET
Specifies an embedded document database for.NET with zero-allocation I/O, C-BSON format, and ACID transactions.