Best Practices Guide
Best Practices for Moltbot
This guide covers recommended practices for deploying and using Moltbot in production environments.
Security Best Practices
1. API Key Management
- Never commit API keys to your repository
- Use environment variables for all sensitive credentials
- Rotate API keys regularly
- Use separate API keys for different environments (dev, staging, production)
- Store keys securely using a secrets manager
# .env.local (never commit this!)
ANTHROPIC_API_KEY=your-key-here
OPENAI_API_KEY=your-key-here
TELEGRAM_BOT_TOKEN=your-token-here2. Network Security
- Run Moltbot behind a firewall or VPN if possible
- Use HTTPS for all webhook connections
- Validate webhook signatures to ensure requests are legitimate
- Restrict database access to necessary services only
3. Data Privacy
- Review which messaging platforms have end-to-end encryption
- Be aware that messages may be logged for debugging purposes
- Implement appropriate data retention policies
- Consider data residency requirements
Configuration Best Practices
1. Multi-Agent Setup
For different use cases, configure multiple AI agents:
// Agent for coding help
const codingAgent = {
name: 'coder',
model: 'claude-opus',
systemPrompt: 'You are an expert programmer...'
};
// Agent for creative writing
const creativeAgent = {
name: 'writer',
model: 'gpt-4',
systemPrompt: 'You are a creative writer...'
};2. Rate Limiting
- Implement rate limits to prevent abuse
- Use different limits for different users/groups
- Monitor usage patterns
- Set up alerts for unusual activity
3. Error Handling
- Configure appropriate error messages for users
- Log errors for debugging and monitoring
- Set up alerts for critical errors
- Implement graceful degradation when services are unavailable
Deployment Best Practices
1. Use Docker
Deploy Moltbot using Docker for consistency:
FROM node:22-alpine
WORKDIR /app
COPY . .
RUN npm install -g moltbot
CMD ["moltbot", "gateway"]2. Process Management
Use systemd or PM2 to keep Moltbot running:
# Using systemd
sudo systemctl enable moltbot
sudo systemctl start moltbot
# Using PM2
pm2 start moltbot --name "moltbot"
pm2 save3. Monitoring
- Set up health checks
- Monitor CPU and memory usage
- Track API response times
- Monitor error rates
4. Backups
- Regular backups of your configuration
- Backup database regularly
- Test restore procedures
- Keep multiple backup locations
Performance Optimization
1. Caching
- Cache frequent requests
- Use connection pooling for databases
- Cache AI model responses when appropriate
- Monitor cache hit rates
2. Resource Allocation
- Allocate appropriate CPU and memory
- Use horizontal scaling for high load
- Implement load balancing
- Monitor resource utilization
3. Message Batching
- Batch messages when processing multiple requests
- Use webhooks instead of polling
- Implement connection pooling
- Use streaming responses
Troubleshooting Guide
Common Issues
Issue: Bot not responding to messages
- Check webhook configuration
- Verify API keys are valid
- Check logs for error messages
- Ensure bot has appropriate permissions
Issue: High latency responses
- Check AI provider status
- Monitor network latency
- Review system resources
- Check for rate limiting
Issue: Memory leaks
- Monitor memory usage over time
- Check for unclosed connections
- Review log file sizes
- Restart Moltbot periodically
Community Resources
- GitHub Issues: Report bugs and request features
- Discord Community: Get help from other users
- Official Docs: Detailed API documentation
- GitHub Discussions: Share ideas and best practices
Disclaimer: This is a community guide based on best practices. Always consult the official documentation for the most up-to-date information.