6.2 KiB
OpenRouter AI Integration
Overview
Lumina 3D Modern Stellar Explorer now uses OpenRouter for AI-powered transaction analysis and insights. OpenRouter provides access to multiple AI models (Claude, GPT-4, Gemini, Llama, etc.) through a single API.
How It Works
Architecture
-
OpenRouter Client (
ui/business-logic/ai/openrouter-client.js)- Handles API communication with OpenRouter
- Manages API key and model selection
- Implements caching (1-minute cache)
- Provides fallback to rule-based analysis
-
Transaction Pattern Analyzer (
ui/business-logic/ai/transaction-pattern-analyzer.js)- Tries AI analysis first (if API key available)
- Falls back to rule-based analysis if AI fails or unavailable
- Seamlessly switches between AI and rule-based modes
-
AI Insights Panel (
ui/views/components/ai-insights-panel.js)- Displays AI-generated insights
- Shows indicator when AI is active vs rule-based
- Updates in real-time as transactions stream in
Setup
Option 1: Environment Variable (Recommended for Production)
Add to ui/.env:
REACT_APP_OPENROUTER_API_KEY=sk-or-v1-your-api-key-here
REACT_APP_OPENROUTER_MODEL=anthropic/claude-3.5-sonnet
Option 2: User Settings (Recommended for Development)
- Navigate to
/settingsin the app - Scroll to "AI Configuration" section
- Enter your OpenRouter API key
- Select your preferred AI model
- Click "Save Configuration"
The API key is stored locally in your browser (localStorage) and never sent to our servers.
Option 3: Global Window Variable
For development/testing, you can set it globally:
window.OPENROUTER_API_KEY = 'sk-or-v1-your-api-key-here'
Getting an API Key
- Visit OpenRouter.ai
- Sign up for an account
- Go to Keys page
- Create a new API key
- Copy the key (starts with
sk-or-v1-)
Supported Models
OpenRouter supports 100+ AI models. Popular options:
- anthropic/claude-3.5-sonnet (Default) - Best balance of quality and cost
- openai/gpt-4 - Most capable, higher cost
- openai/gpt-3.5-turbo - Fast and affordable
- google/gemini-pro - Good alternative
- meta-llama/llama-3-70b-instruct - Open source option
View all models at OpenRouter Models
Features
AI-Powered Analysis
When OpenRouter is configured, the AI will:
-
Analyze Transaction Patterns
- Detect whale movements and large transaction clusters
- Identify arbitrage opportunities
- Find unusual patterns and anomalies
- Assess network health
-
Generate Smart Insights
- Contextual explanations of patterns
- Actionable recommendations
- Risk assessments
- Market trend analysis
-
Provide Natural Language Explanations
- Human-readable insights
- Detailed pattern descriptions
- Network health assessments
Fallback Behavior
If OpenRouter is not configured or fails:
- Automatically falls back to rule-based analysis
- No functionality is lost
- Seamless user experience
- Indicator shows "Rule-Based" instead of "AI Active"
Privacy & Security
- API Key Storage: Stored locally in browser localStorage (if set via UI)
- Data Sent: Only transaction summaries (not full transaction data)
- No Server Storage: API keys are never sent to our servers
- User Control: Users can remove API key at any time
- Caching: Results cached for 1 minute to reduce API calls
Cost Considerations
OpenRouter charges per API call based on the model used:
- Claude 3.5 Sonnet: ~$0.003 per analysis
- GPT-4: ~$0.03 per analysis
- GPT-3.5 Turbo: ~$0.001 per analysis
Caching: Results are cached for 1 minute, so repeated views of the same data don't incur additional costs.
Recommendation: Start with Claude 3.5 Sonnet for best quality/cost balance.
API Request Format
The system sends transaction summaries to OpenRouter:
{
"model": "anthropic/claude-3.5-sonnet",
"messages": [
{
"role": "system",
"content": "You are an expert blockchain analyst..."
},
{
"role": "user",
"content": "Analyze these transactions: [summary]"
}
]
}
Response Format
OpenRouter returns JSON with:
{
"whaleMovements": [...],
"arbitrageOpportunities": [...],
"unusualPatterns": [...],
"networkHealth": {
"healthScore": 85,
"status": "healthy",
"recommendations": [...]
},
"insights": [...]
}
Troubleshooting
AI Not Working
- Check API Key: Verify it's set correctly in settings
- Check Console: Look for error messages in browser console
- Check Network: Ensure OpenRouter API is accessible
- Check Model: Verify the model name is correct
- Check Credits: Ensure your OpenRouter account has credits
Fallback to Rule-Based
If you see "Rule-Based" badge:
- API key not configured
- API key invalid
- Network error
- API rate limit exceeded
- Model unavailable
The system will automatically use rule-based analysis in these cases.
Configuration in Code
Initialize OpenRouter Client
import openRouterClient from './business-logic/ai/openrouter-client'
// Initialize with API key
openRouterClient.init('sk-or-v1-your-key', 'anthropic/claude-3.5-sonnet')
// Use in analyzer
const analysis = await openRouterClient.analyzeTransactions(transactions)
Disable AI
To force rule-based analysis only:
// In ai-insights-panel.js
const analyzedPatterns = await transactionAnalyzer.analyzePatterns(
transactions,
3600000,
false // Set to false to disable AI
)
Future Enhancements
- Model selection per analysis type
- Cost tracking and limits
- Batch analysis for historical data
- Custom prompts for specific use cases
- Multi-model ensemble analysis
- Fine-tuned models for Stellar-specific patterns
Support
For issues with OpenRouter:
For issues with Lumina integration:
- Check browser console for errors
- Verify API key is correct
- Ensure model name is valid
- Check network connectivity