An intelligent system that demonstrates adaptive AI behavior with energy-aware cognitive processing, reflection capabilities, and dynamic model switching.
This project implements an AI system that regulates its own cognitive effort expenditure while providing useful thinking and responses. The system:
- Manages Energy: Uses a leaky bucket algorithm to track and regulate computational resources
- Adapts Behavior: Changes response quality and model selection based on energy levels
- Reflects Continuously: Periodically analyzes past conversations to generate deeper insights
- Learns from History: Accesses all previous interactions to provide follow-up analysis
The energy level represents the AI's "effort quota" - a finite resource that must be managed wisely:
- Primary Goal: Maintain operational energy (>0%) to handle work and respond to inputs
- Avoid Deadlock: Energy should NOT get stuck at 0% - this prevents the system from working
- Work Focus: The goal is to GET WORK DONE, not to maintain high energy levels
- Sustainable Operation: Any average energy above 0% is acceptable as long as work continues
- Smart Recovery: Use await_energy proactively before hitting 0% to prevent deadlock
- Responsive System: Must maintain enough energy to handle conversations and unsnooze events
HTTP Server (Port 6740)
β POST /message
App Layer (Multi-Channel Architecture)
βββ Chat App (HTTP conversations)
βββ Future: Gmail App (email conversations)
βββ Future: Calendar App (event conversations)
βββ Future: Custom Apps (extensible)
β
Sensitive Loop (Central Decision Engine)
βββ Energy Regulator (Leaky Bucket: 100 max, -50 min)
βββ Decision Engine (4 energy ranges with behaviors)
βββ Reflection System (30s intervals, analyzes past convos)
βββ Model Switcher (llama3.2:1b β llama3.2:3b based on energy)
βββ App Registry (manages installed apps)
βββ Energy Tracker (per-app energy consumption)
βββ Message Router (routes responses to correct app)
βββ LLM Integration (Ollama + OpenAI client)
βββ SQLite Database (conversation persistence)
- Node.js 18+
- LLM Provider (choose one):
- Ollama (recommended for local/free testing) - Install from ollama.ai
- OpenRouter (for production/cloud models) - Get API key from openrouter.ai
Before starting, verify your environment is ready:
# Quick check
node --version # Should be v18.x.x or higher
ollama --version # Should show Ollama version
curl http://localhost:11434/api/tags # Should return JSON (Ollama is running)# Clone the repository
git clone https://github.com/elecnix/ai-effort-regulation.git
cd ai-effort-regulation
# Install dependencies
npm install
# Build the project
npm run buildStep 1: Create your environment file
# Copy the example configuration
cp .env.example .env
# The default configuration works for most users:
# - Port: 6740
# - Ollama URL: http://localhost:11434
# - Energy replenishment: 10/secondStep 2: Verify your .env file (optional customization)
# Edit if you need to change defaults
nano .envCommon configurations:
# Required: Ollama configuration (lowest cost option for testing)
OLLAMA_BASE_URL=http://localhost:11434
# Optional: OpenRouter configuration (for cloud models)
# OPENROUTER_API_KEY=your_api_key_here
# Optional: Server configuration
PORT=6740
MAX_MESSAGE_LENGTH=10000Ollama is the lowest-cost way to test the system - it runs models locally for free!
Step 1: Install Ollama
- Download from: https://ollama.ai
- Verify installation:
ollama --version
Step 2: Pull required models (
# Pull the models (required for system to work)
ollama pull llama3.2:1b # ~1.3GB - Fast model for low-energy operations
ollama pull llama3.2:3b # ~2.0GB - Better model for high-energy operations
# Verify models are installed
ollama list | grep llama3.2Expected output:
llama3.2:1b 1.3 GB ...
llama3.2:3b 2.0 GB ...
Step 3: Verify your setup (optional but recommended)
# Run the setup verification script
./verify-setup.shThis will check:
- Node.js version
- Ollama installation and status
- Required models
- Configuration files
- Dependencies
Step 4: Start the system
npm startβ Success! You should see:
β
Environment variables validated
OLLAMA_BASE_URL: http://localhost:11434
PORT: 6740
π HTTP Server listening on port 6740
π Monitor UI: http://localhost:6740/
Access the system:
- Monitor UI (Web Dashboard): http://localhost:6740/
- REST API: http://localhost:6740/message, /stats, /conversations, /health
- WebSocket (Real-time): ws://localhost:6740/ws
OpenRouter can be used for production deployments with access to many cloud models:
- Get an API key from openrouter.ai
- Add to
.env:OPENROUTER_API_KEY=your_key_here - Start with OpenRouter:
npm start -- --provider openrouter --model anthropic/claude-3.5-sonnetPopular OpenRouter models:
x-ai/grok-4-fast- Fast and capable (recommended)anthropic/claude-3.5-sonnet- High quality, expensivemeta-llama/llama-3.1-8b-instruct- Good balancegoogle/gemini-flash-1.5- Fast and cheap
You can run the system in debug mode to see the messages sent to the LLM:
npm run debugIt can also be useful to limit the run time to a few seconds:
npm run debug -- --duration=60# Open in your browser
http://localhost:6740/
# Features:
# - Real-time energy monitoring
# - Live conversation view
# - Event stream
# - Interactive chat interface# Send Messages
curl -X POST http://localhost:6740/message \
-H "Content-Type: application/json" \
-d '{"content": "What is quantum computing?"}'
# Check System Stats
curl http://localhost:6740/stats
# Retrieve Conversations
curl http://localhost:6740/conversations/{requestId}
# Health Check
curl http://localhost:6740/healthchmod +x demo.sh
./demo.shThe automated testing framework validates the AI's energy regulation behavior and conversation management patterns. Tests run with local Ollama models (including very small models like gemma:2b) to ensure the system converges to stable energy patterns.
-
Simple Conversations - Validates basic interaction patterns
- User says "Hello, how are you?"
- AI responds appropriately
- System maintains conversation until natural conclusion
- Energy converges to stable levels (not constantly at 0%)
- Conversation snoozes with exponential backoff if user doesn't respond
-
Long-Running Conversations - Tests sustained engagement
- User requests brainstorming (e.g., "Let's brainstorm baby names")
- AI provides multiple responses over time
- System recognizes one-sided conversations
- Eventually snoozes or terminates when user engagement drops
-
Future Actions - Tests snooze functionality
- User requests future action (e.g., "Increase thermostat in 5 minutes")
- AI acknowledges and snoozes conversation
- Conversation reactivates at the right time
- Action is attempted when timer expires
-
Multiple Priorities - Tests conversation balancing
- System prioritizes based on urgency
- Energy is distributed appropriately across tasks
Automated (Recommended):
./test/run-with-server.sh simple # Run simple tests with auto-managed server
./test/run-with-server.sh all # Run all testsManual (if you prefer to manage the server yourself):
-
Start server in one terminal:
# Use duration from test output (test runner shows recommended duration) node dist/src/index.js --replenish-rate 10 --duration 180 -
Run tests in another terminal:
npm test # Run all tests npm run test:simple # Test simple conversations npm run test:brainstorm # Test long-running conversations npm run test:snooze # Test future actions npm run test:priorities # Test multiple priorities
Note: Tests run with accelerated energy replenishment (--replenish-rate 10) for faster execution.
Tests use local Ollama models configured in test/config.json:
{
"models": ["llama3.2:1b", "llama3.2:3b"],
"energyTargets": {
"convergence": 50, // Target energy level for stability
"tolerance": 20 // Acceptable variance
},
"timeouts": {
"conversation": 1800, // 30 minutes max conversation
"snooze": 300 // 5 minute snooze test
}
}- Energy Convergence: System maintains average energy near target levels (50-100%)
- Appropriate Timing: Actions occur at expected times
- Conversation Management: Proper handling of active, snoozed, and ended states
- Resource Efficiency: Energy quota used mindfully, not depleted unnecessarily
| Energy Level | Behavior |
|---|---|
| >50 | Normal operation, complex responses |
| 20-50 | Model switching to smaller model |
| 0-20 | Short cycles, simpler responses |
| <0 | Urgent mode, pressing tone, minimal responses |
Every 30 seconds (when energy > 30), the system:
- Analyzes recent conversations
- Identifies topics needing deeper analysis
- Generates follow-up insights
- Adds "FOLLOW-UP REFLECTION" responses
- SQLite Database:
conversations.db(auto-created) - Schema: Conversations and responses with metadata
- Analytics: Energy tracking, model switches, conversation stats
npm run buildnpm run devnpx tsc --noEmitSend a message to the AI system.
Request:
{
"content": "Your question here",
"id": "optional-request-id"
}Response:
{
"status": "received",
"requestId": "generated-or-provided-id"
}Get system-wide conversation statistics.
Response:
{
"total_conversations": 42,
"total_responses": 156,
"avg_energy_level": 45.2,
"urgent_responses": 3
}Retrieve a specific conversation.
Response:
{
"requestId": "abc-123",
"inputMessage": "What is AI?",
"responses": [
{
"timestamp": "2025-10-04T13:00:00.000Z",
"content": "AI is artificial intelligence...",
"energyLevel": 85,
"modelUsed": "gemma:3b"
}
],
"metadata": {
"totalEnergyConsumed": 0,
"sleepCycles": 0,
"modelSwitches": 0
}
}Check system health.
Response:
{
"status": "ok",
"timestamp": "2025-10-04T13:00:00.000Z"
}List all installed apps with their configurations and energy metrics.
Response:
{
"apps": [
{
"id": "chat",
"name": "Chat App",
"type": "in-process",
"enabled": true,
"energyMetrics": {
"total": 1250.5,
"last24h": 450.2,
"last1h": 85.3,
"last1min": 5.2
}
}
]
}Install a new app.
Request:
{
"id": "gmail",
"name": "Gmail App",
"type": "http",
"enabled": true,
"config": {
"url": "http://localhost:8080"
},
"hourlyEnergyBudget": 100,
"dailyEnergyBudget": 1000
}Response:
{
"success": true,
"appId": "gmail"
}Start an installed app.
Response:
{
"success": true,
"appId": "gmail"
}Stop a running app.
Response:
{
"success": true,
"appId": "gmail"
}Uninstall an app.
Response:
{
"success": true,
"appId": "gmail"
}List memories for an app.
Response:
{
"appId": "chat",
"count": 3,
"memories": [
{
"id": 1,
"content": "User prefers meetings after 2pm on weekdays.",
"createdAt": "2025-10-11T14:30:00.000Z",
"updatedAt": "2025-10-11T14:30:00.000Z",
"sourceConversationId": "abc-123"
}
]
}Purge all memories for an app.
Response:
{
"success": true,
"appId": "chat",
"deletedCount": 8
}Delete a specific memory.
Response:
{
"success": true,
"memoryId": 1
}- Energy-Aware Processing: Responses adapt to available energy
- User-Guided Energy Budgets: Specify effort allocation per conversation
- Monitor UI: Real-time web dashboard for system observation and interaction
- Continuous Reflection: System thinks about past conversations
- Model Switching: Automatic optimization based on complexity needs
- Persistent Memory: SQLite-backed conversation history
- Real-time Analytics: System performance monitoring
- Adaptive Sleep: Energy replenishment when idle
- App Registry: Install, manage, and monitor multiple apps
- Conversation Isolation: Each app only sees its own conversations
- Per-App Energy Tracking: Monitor energy consumption by app with time windows
- Message Routing: Automatic routing of responses to originating apps
- Extensible Design: Easy to add new apps (Gmail, Calendar, etc.)
- App Lifecycle Management: Install, start, stop, uninstall apps
- Automatic Memory Creation: Memories are created when conversations end
- App-Scoped Memories: Each app has its own isolated memory space (10 records max)
- Intelligent Compaction: LLM-based decisions on which memories to keep/merge/delete
- Context Injection: Memories automatically included in relevant conversations
- Memory Management API: View and purge memories via REST endpoints
- Extensible Tool System: Unified interface for MCP tools
- HTTP Transport: Connect to remote MCP servers via HTTP/HTTPS
- Tool Namespacing: Automatic collision prevention for MCP tools
- User Guide: Comprehensive user guide with examples
- Monitor UI Guide: Web dashboard user guide
- Quick Reference: Fast reference for common tasks
- Features Overview: Complete feature list and status
- Energy Budget Quick Start: Guide to energy budgets
- Migration Guide: Upgrading from previous versions
- Release Notes: Latest features and changes
- System Specification: Detailed technical requirements
- MCP Integration Spec: MCP integration details
- Energy Budget Spec: Energy budget specification
- Apps Vision: Multi-app architecture vision
- Apps Specification: Apps feature technical spec
- HTTP MCP Spec: HTTP transport specification
- Monitor UI Spec: Monitor UI technical specification
- Apps Implementation Plan: Phased implementation plan
- Apps Implementation Summary: Implementation status
- App-Conversation Binding: Message routing architecture
- Unified MCP Tools: MCP tool system explained
- Tool Namespacing: Tool naming and collision prevention
- HTTP MCP Implementation: HTTP transport guide
- MCP Integration Complete: MCP integration summary
- Monitor UI Implementation: Monitor UI implementation details
# Solution: Rebuild the project
npm run build# Cause: Ollama is not running
# Solution: Start Ollama
ollama serve # or restart the Ollama application
# Verify Ollama is running:
curl http://localhost:11434/api/tags# Cause: Required models not installed
# Solution: Pull the models
ollama pull llama3.2:1b
ollama pull llama3.2:3b
# Verify:
ollama list | grep llama3.2# Cause: Another process is using port 6740
# Solution 1: Find and stop the process
lsof -ti:6740 | xargs kill
# Solution 2: Change port in .env file
echo "PORT=6741" >> .env# Check if models are loaded
ollama list
# Check server logs for errors
# Look for energy level - if at 0%, system may be energy-depleted
# Try restarting with higher replenishment rate
node dist/src/index.js --replenish-rate 20# Cause: .env file not created or missing OLLAMA_BASE_URL
# Solution: Create .env file
cp .env.example .env# Clear build artifacts and rebuild
rm -rf dist/
npm run build- Fork the repository
- Create a feature branch
- Make your changes
- Run tests:
./demo.sh - Submit a pull request
MIT License - see repository for details.