Building a Decentralized AI Chatbot
The AI Centralization Problem
You want to build an AI chatbot. Here are your "options":
- OpenAI API: $0.05-15.00 per 1M input tokens, $0.40-120.00 per 1M output tokens (GPT-5 nano to pro) + data sent to their servers
- Anthropic Claude: $1.00-5.00 per 1M input tokens, $5.00-25.00 per 1M output tokens (Haiku to Opus) + privacy concerns
- Google Gemini: $0.10-2.00 per 1M input tokens, $0.40-18.00 per 1M output tokens (paid tier, Flash-Lite to Pro) + data processed on Google's servers
- Azure OpenAI: Same APIs + extra Azure complexity and costs
The Real Costs
10,000 conversations per month (~500 tokens each: 250 input, 250 output = 5M total tokens/month):
- GPT-5 nano: ~$1.13/month (cheapest OpenAI option: $0.05 × 2.5M input + $0.40 × 2.5M output)
- GPT-5.1: ~$28.13/month (balanced: $1.25 × 2.5M + $10 × 2.5M)
- Claude Haiku: ~$15/month (cheapest Claude: $1 × 2.5M + $5 × 2.5M)
- Claude Sonnet: ~$45/month (balanced: $3 × 2.5M + $15 × 2.5M)
- Gemini 2.5 Flash: ~$7/month (cheapest Google: $0.30 × 2.5M + $2.50 × 2.5M)
- Server hosting: $20-100/month
- Total: $21-145/month (depending on model choice, excluding hosting)
Plus the hidden problems:
- All conversations go through their servers
- They train on your users' data
- Rate limits and quotas
- Service outages break your bot
- Privacy compliance issues (GDPR, HIPAA)
- Vendor lock-in (can't switch easily)
How Ixian + Local AI Solves It
Run AI models locally, communicate P2P:
Zero API costs - Models run on your hardware
Complete privacy - Data never leaves your control (conversations, model weights, everything local)
No rate limits - Use it as much as you want
Works offline - No internet required for inference (P2P works on local networks)
Post-quantum secure - Hybrid key exchange (RSA-4096 + ECDH + ML-KEM-1024)
Dual encryption - Both AES-256-GCM + ChaCha20-Poly1305 must be broken simultaneously
Starling scaling - Sector-based routing enables unlimited bot instances without central coordination
Self-authenticating - Bots authenticate via cryptographic addresses (no API keys to steal)
Economic sustainability - S2 node operators earn relay rewards, ensuring network availability
No vendor lock-in - Open source implementation, can switch or fork at any time
Cross-platform - Works on Windows, Linux, macOS, mobile devices, and embedded systems like Raspberry Pi
No central points of failure - Decentralized P2P architecture means no single server can take down the network
Architecture
Traditional AI Bot:
User -> Your Server -> OpenAI API -> Response
(hosting $) (API $$$$) (privacy loss)
Ixian AI Bot:
User <--> QuIXI <--> Your Bot <--> Local AI (LLM Studio/Ollama)
($0) (one-time hardware) (full control)
QuIXI bridges your local AI to the Ixian P2P network
- Users chat with your bot via encrypted P2P
- Bot processes requests with local LLM
- Responses sent back via P2P
- No servers, no APIs, no monthly bills
Real Example: Personal AI Assistant
Let's build an AI chatbot that:
- Runs Meta's Llama 3 model locally
- Responds to chat messages via P2P
- Has personality and context memory
- Costs $0/month to operate
What You'll Build
A Raspberry Pi (or any computer) running:
- LM Studio - Local LLM runtime
- QuIXI - Gateway to Ixian network
- Python script - Bot logic and memory
Comparison:
| Feature | OpenAI ChatGPT API | Ixian Local Bot |
|---|---|---|
| API costs | $1-145/mo (10k conversations) | $0 |
| Inference | Cloud | Local hardware |
| Privacy | Data sent to OpenAI servers | Data never leaves device |
| Rate limits | Yes (tier-based, TPM/RPM limits) | None |
| Customization | Prompt engineering only | Complete control (fine-tuning, RAG, custom logic) |
| Offline use | No (requires internet) | Yes (works on local networks) |
| P2P communication | Requires webhooks/servers | Native encrypted P2P (Starling routing) |
| Monthly cost | $21-145+ | $1-50 (electricity: RPi5 3-5W typical @ $0.15/kWh, GPU desktop 50-450W depending on model) |
| One-time cost | $0 | $80-2,000+ (hardware) |
Step-by-Step Build Guide
Hardware Options
Budget Option: Raspberry Pi 5 (8GB)
- Cost: ~$80-120 (board + accessories)
- Models: Small quantized models (3B-7B parameters)
- Speed: 2-5 tokens/second
- Good for: Personal use, light loads
Recommended: Desktop/Laptop with GPU
- Cost: $600-2,000 (if buying new; RTX 4060 or better recommended)
- Models: Full-size models (7B-70B parameters)
- Speed: 20-100+ tokens/second (faster with GPU acceleration)
- Good for: Multiple users, fast responses
Power Option: Dedicated AI Server
- Cost: $2,500-6,000+ (enterprise GPU like RTX 4090 or A6000)
- Models: Any size, multiple concurrent sessions
- Speed: 100+ tokens/second with batching
- Good for: Production use, many simultaneous users
Software Setup
1. Install LM Studio
On Windows/Mac/Linux:
# Download from https://lmstudio.ai
# Or use Ollama as alternative: https://ollama.ai
# For this guide, we'll use LM Studio
# Install and launch LM Studio
Download a model:
- Open LM Studio
- Search for "Llama 3" or "Mistral"
- Download
Meta-Llama-3-8B-Instruct(recommended) - Wait for download (4-8GB)
Start the local server:
- Go to "Local Server" tab
- Select your downloaded model
- Click "Start Server"
- Note the URL (usually
http://localhost:1234)
2. Install QuIXI
# Clone repositories
cd ~
git clone https://github.com/ixian-platform/Ixian-Core.git
git clone https://github.com/ixian-platform/QuIXI.git
# Build QuIXI
cd QuIXI
dotnet build -c Release
cd QuIXI/bin/Release/net8.0
Configure QuIXI (ixian.cfg):
# Network settings (mainnet)
addPeer = seed1.ixian.io:10234
# For testnet, use: addPeer = seedtest1.ixian.io:11234
# API settings
apiPort = 8001
apiBind = 127.0.0.1
addApiUser = admin:changeme123
# MQTT integration
mqDriver = mqtt
mqHost = localhost
mqPort = 1883
# Bot identity
name = ai-assistant-001
3. Install Dependencies
# Install Python dependencies
pip install requests openai paho-mqtt python-dotenv
# Install MQTT broker
sudo apt install -y mosquitto mosquitto-clients # Linux
# or
brew install mosquitto # Mac
4. Create the AI Bot
Create ai_chatbot.py:
#!/usr/bin/env python3
"""
Decentralized AI Chatbot using QuIXI + Local LLM
"""
import os
import json
import time
import requests
from openai import OpenAI
import paho.mqtt.client as mqtt
# Configuration
QUIXI_API = "http://localhost:8001"
LM_STUDIO_API = "http://localhost:1234/v1" # LM Studio local server
MQTT_BROKER = "localhost"
MQTT_PORT = 1883
# Bot personality
SYSTEM_PROMPT = """You are a helpful AI assistant running on a decentralized network.
You are privacy-focused and run locally, not on cloud servers. Be friendly, concise,
and knowledgeable. You can discuss technology, answer questions, and have conversations."""
# Initialize OpenAI client pointing to local LM Studio
client = OpenAI(base_url=LM_STUDIO_API, api_key="not-needed")
# Conversation memory (address -> message history)
conversation_history = {}
def send_message(address, message):
"""Send a message back to a user via QuIXI"""
try:
response = requests.get(f"{QUIXI_API}/sendChatMessage", params={
"address": address,
"message": message,
"channel": 0
})
return response.status_code == 200
except Exception as e:
print(f"Error sending message: {e}")
return False
def get_ai_response(user_address, user_message):
"""Get response from local LLM with conversation history"""
# Initialize conversation history for new users
if user_address not in conversation_history:
conversation_history[user_address] = [
{"role": "system", "content": SYSTEM_PROMPT}
]
# Add user message to history
conversation_history[user_address].append({
"role": "user",
"content": user_message
})
# Keep only last 10 messages to manage memory
if len(conversation_history[user_address]) > 21: # 1 system + 20 messages
conversation_history[user_address] = [
conversation_history[user_address][0] # Keep system prompt
] + conversation_history[user_address][-20:] # Keep last 20
try:
# Call local LLM
completion = client.chat.completions.create(
model="local-model", # Model name doesn't matter for LM Studio
messages=conversation_history[user_address],
temperature=0.7,
max_tokens=500
)
ai_response = completion.choices[0].message.content
# Add AI response to history
conversation_history[user_address].append({
"role": "assistant",
"content": ai_response
})
return ai_response
except Exception as e:
print(f"Error getting AI response: {e}")
return "Sorry, I encountered an error processing your request. Please try again."
def handle_command(address, message):
"""Handle special commands"""
cmd = message.lower().strip()
if cmd == "/reset":
if address in conversation_history:
conversation_history[address] = [
{"role": "system", "content": SYSTEM_PROMPT}
]
return "🔄 Conversation reset. Let's start fresh!"
elif cmd == "/help":
return """🤖 AI Assistant Commands:
• Chat normally for AI responses
• /reset - Clear conversation history
• /help - Show this message
• /stats - Bot statistics
I'm running locally with full privacy - your messages never leave this device!"""
elif cmd == "/stats":
msg_count = len(conversation_history.get(address, [])) - 1 # Exclude system prompt
total_users = len(conversation_history)
return f"""📊 Bot Statistics:
• Messages in this conversation: {msg_count}
• Total users served: {total_users}
• Model: Llama 3 (local)
• Privacy: 100% (all local processing)"""
return None # Not a command, process as normal message
def on_connect(mqtt_client, userdata, flags, rc):
"""Callback when connected to MQTT broker"""
print(f"Connected to MQTT broker with result code {rc}")
# Subscribe to chat messages and contact requests
mqtt_client.subscribe("Chat/#")
mqtt_client.subscribe("RequestAdd2/#")
print("🤖 AI Chatbot is ready!")
print("Add your bot as a contact in Spixi and start chatting!")
def on_message(mqtt_client, userdata, msg):
"""Callback when a message is received"""
try:
# Parse incoming message
data = json.loads(msg.payload.decode())
# Handle contact requests (auto-accept)
if msg.topic.startswith("RequestAdd2"):
sender = data.get("sender", {})
if sender:
print(f"📥 Auto-accepting contact: {sender}")
requests.get(f"{QUIXI_API}/acceptContact", params={"address": sender})
time.sleep(1) # Wait for contact to be added
send_message(sender, "👋 Hi! I'm your local AI assistant. Ask me anything!\n\nI run on your hardware with full privacy. Type /help for commands.")
# Handle chat messages
elif msg.topic.startswith("Chat"):
sender = data.get("sender", {})
message = data.get("data", {}).get("data", "").strip()
if not sender or not message:
return
print(f"💬 Message from {sender[:8]}...: {message}")
# Check for commands first
command_response = handle_command(sender, message)
if command_response:
send_message(sender, command_response)
return
# Show typing indicator (optional)
print(f"🤔 Processing with AI...")
# Get AI response
ai_response = get_ai_response(sender, message)
# Send response
print(f"🤖 Response: {ai_response[:100]}...")
send_message(sender, ai_response)
except Exception as e:
print(f"❌ Error processing message: {e}")
def main():
"""Main bot loop"""
print("🚀 Starting Decentralized AI Chatbot...")
print(f"📡 QuIXI API: {QUIXI_API}")
print(f"🧠 LM Studio: {LM_STUDIO_API}")
# Test LM Studio connection
try:
response = requests.get(f"{LM_STUDIO_API}/models")
print(f"✓ LM Studio connected")
except:
print(f"⚠️ Warning: Cannot connect to LM Studio. Make sure it's running!")
return
# Test QuIXI connection
try:
response = requests.get(f"{QUIXI_API}/myWallet")
if response.status_code == 200:
wallet_map = response.json().get("result", {})
if isinstance(wallet_map, dict) and wallet_map:
print("✓ QuIXI connected")
addresses = list(wallet_map.keys())
primary = addresses[0]
print(f"📍 Primary bot address: {primary}")
if len(addresses) > 1:
print(f"📍 Additional addresses ({len(addresses)-1}):")
for addr in addresses[1:]:
print(f" - {addr}")
print("\n💰 Wallet balances:")
for addr, balance in wallet_map.items():
print(f" {addr}: {balance} IXI")
print("\n🎯 Add primary bot address as a contact in Spixi to start chatting!")
else:
print("⚠️ Warning: QuIXI returned empty wallet data.")
else:
print(f"⚠️ Warning: QuIXI returned status {response.status_code}")
except Exception as e:
print(f"⚠️ Warning: Cannot connect to QuIXI. Make sure it's running! ({e})")
return
# Connect to MQTT broker
mqtt_client = mqtt.Client()
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
try:
mqtt_client.connect(MQTT_BROKER, MQTT_PORT, 60)
mqtt_client.loop_forever()
except KeyboardInterrupt:
print("\n👋 Shutting down bot...")
mqtt_client.disconnect()
except Exception as e:
print(f"❌ Error: {e}")
if __name__ == "__main__":
main()
Make it executable:
chmod +x ai_chatbot.py
Running Your AI Bot
1. Start All Services
Terminal 1 - Start LM Studio:
# Open LM Studio GUI
# Load your model
# Start Local Server
# Keep window open
Terminal 2 - Start QuIXI:
cd ~/QuIXI/QuIXI/bin/Release/net8.0
./QuIXI
# Note the Ixian address shown
Terminal 3 - Start MQTT Broker:
mosquitto
Terminal 4 - Start AI Bot:
python3 ai_chatbot.py
2. Test Your Bot
Output should show:
🚀 Starting Decentralized AI Chatbot...
📡 QuIXI API: http://localhost:8001
🧠 LM Studio: http://localhost:1234/v1
✓ LM Studio connected
✓ QuIXI connected
📍 Bot address: 1ABc2DEf3GHi4JKl5MNop...
🎯 Add this address as a contact in Spixi to start chatting!
Connected to MQTT broker with result code 0
🤖 AI Chatbot is ready!
3. Chat with Your Bot
In Spixi:
- Add the bot's address as a contact
- Bot auto-accepts and greets you
- Start chatting!
Example conversation:
You: What is the Ixian platform?
Bot: Ixian is a decentralized platform that enables peer-to-peer
communication and transactions with post-quantum security. Unlike
traditional blockchain networks, it focuses on scalable real-time
communication through its S2 streaming layer...
You: Can you write Python code?
Bot: Yes! I can help with Python. What would you like to create?
You: Write a function to calculate fibonacci
Bot: Here's an efficient Python function for Fibonacci numbers:
def fibonacci(n):
if n <= 1:
return n
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
This uses O(n) time and O(1) space...
What's Happening Behind the Scenes
Message Flow
1. You send "What is AI?" in Spixi
↓
2. Spixi encrypts message (post-quantum)
↓
3. Routes through Ixian S2 network (P2P)
↓
4. QuIXI receives and decrypts
↓
5. Publishes to MQTT topic Chat/#
↓
6. Python bot receives message
↓
7. Bot sends to local LM Studio
↓
8. LM Studio processes with Llama 3
↓
9. Bot receives AI response
↓
10. Sends back through QuIXI -> S2 -> Spixi
↓
11. You see AI response (all P2P, all private)
Latency: 1-10 seconds total (P2P: 20ms-1s depending on network, LLM: 1-8s depending on hardware and model size)
Privacy: 100% (never leaves your device, conversations and model weights stay local)
Cost: $0-5/month (electricity only, after initial hardware investment)
Advanced Features
1. Multi-Model Support
# Support different models for different tasks
MODELS = {
"chat": "llama-3-8b-instruct",
"code": "codellama-13b",
"creative": "mixtral-8x7b"
}
def get_ai_response(user_address, user_message, task="chat"):
completion = client.chat.completions.create(
model=MODELS[task],
messages=conversation_history[user_address],
temperature=0.7 if task == "chat" else 0.2
)
# ... rest of code
2. Persistent Memory
import sqlite3
from datetime import datetime
# Store conversations in SQLite
def save_conversation(address, role, message):
conn = sqlite3.connect('conversations.db')
c = conn.cursor()
c.execute('''INSERT INTO messages (address, role, message, timestamp)
VALUES (?, ?, ?, ?)''',
(address, role, message, datetime.now()))
conn.commit()
conn.close()
def load_conversation(address, limit=20):
conn = sqlite3.connect('conversations.db')
c = conn.cursor()
c.execute('''SELECT role, message FROM messages
WHERE address = ?
ORDER BY timestamp DESC LIMIT ?''',
(address, limit))
messages = c.fetchall()
conn.close()
return [{"role": r, "content": m} for r, m in reversed(messages)]
3. RAG (Retrieval Augmented Generation)
from sentence_transformers import SentenceTransformer
import numpy as np
# Load embedding model
embedder = SentenceTransformer('all-MiniLM-L6-v2')
# Your knowledge base
knowledge_base = [
"Ixian uses post-quantum cryptography including ML-KEM.",
"QuIXI bridges traditional systems to the Ixian network.",
# ... more documents
]
# Pre-compute embeddings
kb_embeddings = embedder.encode(knowledge_base)
def get_relevant_context(query, top_k=3):
query_embedding = embedder.encode([query])
similarities = np.dot(kb_embeddings, query_embedding.T).flatten()
top_indices = similarities.argsort()[-top_k:][::-1]
return "\n".join([knowledge_base[i] for i in top_indices])
def get_ai_response(user_address, user_message):
# Add relevant context from knowledge base
context = get_relevant_context(user_message)
enhanced_message = f"Context:\n{context}\n\nQuestion: {user_message}"
# ... rest of AI processing
4. Function Calling
# Define tools the AI can use
TOOLS = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
}
}
}
}
]
def get_weather(location):
# Implementation here
return f"Weather in {location}: Sunny, 72°F"
# In get_ai_response:
completion = client.chat.completions.create(
model="local-model",
messages=messages,
tools=TOOLS,
tool_choice="auto"
)
# Handle tool calls
if completion.choices[0].message.tool_calls:
# Execute function and return result
pass
Cost Analysis: 1 Year of Operation
Traditional Cloud AI Bot (10,000 conversations/month)
| Component | Monthly | Yearly |
|---|---|---|
| API costs (Claude Haiku/GPT-5 nano) | $1-15 | $12-180 |
| API costs (Claude Sonnet/GPT-5.1) | $28-45 | $336-540 |
| Server hosting (for bot logic) | $20-100 | $240-1,200 |
| Total (budget models) | $21-115 | $252-1,380 |
| Total (balanced models) | $48-145 | $576-1,740 |
Ixian Local AI Bot
| Component | One-time | Monthly | Yearly |
|---|---|---|---|
| Hardware (mid-range PC with GPU) | $800-1,500 | - | - |
| Electricity (RPi5: 3-5W typical, Desktop: 100-200W avg @ $0.15/kWh) | - | $0.50-22 | $6-264 |
| Total first year | $812-1,764 | ||
| Total subsequent years | $12-264 |
Break-even:
- vs budget cloud APIs: 6-18 months (depending on hardware and usage)
- vs balanced cloud APIs: 4-10 months
- Savings after year 1: $250-1,730/year ongoing
Plus you get:
- Complete privacy control
- No rate limits
- Runs offline
- Can't be shut down
- No vendor lock-in
Production Deployment
Auto-Start on Boot
# Create systemd service
sudo nano /etc/systemd/system/ai-bot.service
[Unit]
Description=Ixian AI Chatbot
After=network.target mosquitto.service
[Service]
Type=simple
User=youruser
WorkingDirectory=/home/youruser
ExecStart=/usr/bin/python3 /home/youruser/ai_chatbot.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
sudo systemctl enable ai-bot
sudo systemctl start ai-bot
sudo systemctl status ai-bot
Monitoring
import logging
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('bot.log'),
logging.StreamHandler()
]
)
# Log all interactions
logging.info(f"User {sender[:8]}: {message}")
logging.info(f"Bot response: {ai_response[:100]}")
Use Cases
Personal Assistant
- Runs on your computer/server
- Full conversation history
- Learns your preferences
- Complete privacy
Family Bot
- Shared knowledge base
- Multiple family members can chat
- Help with homework, recipes, etc.
- No subscription fees
Business Customer Support
- Run on your infrastructure
- Train on your documentation
- No data sent to third parties
- GDPR/HIPAA compliant
IoT Device Control
- "Turn on the lights"
- Natural language device control
- Combine with previous IoT guide
- Voice-to-text -> AI -> device commands
Educational Tutor
- Specialized knowledge base
- Track student progress
- Offline capable (schools)
- Zero per-student cost
Limitations & Considerations
When Local AI is Perfect
Privacy-critical applications - Healthcare, legal, personal data that cannot leave your control
High-volume use - Cost savings at scale (>1000 requests/month)
Offline requirements - Remote locations, secure facilities
Custom models - Fine-tuned for specific domains, RAG with proprietary data
Long-term projects - Hardware investment pays off in 4-17 months
Control and ownership - No rate limits, no service changes, no vendor decisions
When Cloud AI Makes Sense
Low usage - <500 requests/month often free on cloud APIs
No hardware - Can't justify $800-2000 upfront investment
Need latest models - Frontier models (GPT-4, Claude) not available locally yet
Zero maintenance - Don't want to manage infrastructure, updates, or hardware
Rapid prototyping - Test ideas quickly without infrastructure setup
Bursty workloads - Occasional high peaks, mostly idle
Assessment:
- Small hobby chatbots (<1000 conversations/month): Cloud APIs are often free or <$5/month
- Medium production bots (10k conversations/month): Cloud costs $21-145/month, local breaks even in 6-12 months
- High volume (100k+ conversations/month): Local wins decisively on cost
Best of both: Use local for privacy/volume, cloud for specialized tasks or supplementary features
Join the Community
- Discord
- QuIXI Examples
- Share your bot configurations
The Bottom Line
Cloud AI:
- Costly ($21-145+/month for 10k conversations)
- Your data on their servers
- Rate limits
- Can be shut down
Ixian Local AI:
- One-time hardware cost ($800-2,000)
- Your data stays local (conversations, model weights, everything)
- No limits (no rate limits, quota limits, or usage restrictions)
- Can't be shut down (no service deprecation risk)
- P2P encrypted communication (post-quantum: RSA-4096 + ECDH + ML-KEM-1024)
- Dual encryption (AES-256 + ChaCha20-Poly1305)
- Starling scaling (sector-based routing, no central coordination)
- Self-authenticating (cryptographic addresses, no API keys)
- Works offline (P2P on local networks, no internet required)
- Economic sustainability (S2 relay rewards ensure network availability)
Same capabilities. Complete privacy. Actual ownership. Typical break-even in 4-18 months depending on usage and hardware.
Build AI applications that:
- Respect user privacy
- Cost almost nothing to run
- Work offline
Ready to run AI without cloud dependence? The example above gets you started with a fully functional chatbot in under an hour. From there, customize it for your exact use case - all while keeping your data local and your costs at zero.