βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CLIENT LAYER β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Next.js 14+ (App Router) β React 18+ β TypeScript β TailwindCSS β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Components: β
β βββ Chat Interface β
β βββ Provider Dashboard β
β βββ Real-time Analytics β
β βββ Admin Panel β
β βββ Theme System β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β EDGE LAYER β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Vercel Edge Functions β Middleware β Rate Limiting β Caching β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Routes: β
β βββ /api/chat/* (Streaming) β
β βββ /api/admin/* (Protected) β
β βββ /api/analytics/* β
β βββ /api/webhooks/* β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β API GATEWAY LAYER β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β OpenRouter Integration β Load Balancer β Fallback System β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Features: β
β βββ Intelligent API routing β
β βββ Cost optimization β
β βββ Automatic failover β
β βββ Model compatibility layer β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PROVIDER ABSTRACTION LAYER β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β OpenAI β Anthropic β Google Gemini β OpenRouter β Custom LLMs β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Unified Interface: β
β βββ Standardized message format β
β βββ Token counting β
β βββ Cost calculation β
β βββ Error handling β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β DATA LAYER β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β PostgreSQL/Neon β Redis β Vector DB β Blob Storage β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Models: β
β βββ User & Authentication β
β βββ Conversations & Messages β
β βββ API Keys (Encrypted) β
β βββ Usage Analytics β
β βββ Vector Embeddings β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
anygpt/
βββ π¦ package.json
βββ π³ docker-compose.yml
βββ π src/
β βββ π app/
β β βββ π (auth)/
β β β βββ login/
β β β βββ register/
β β β βββ forgot-password/
β β β βββ layout.tsx
β β βββ π (dashboard)/
β β β βββ layout.tsx
β β β βββ page.tsx
β β β βββ π chat/
β β β β βββ [conversationId]/
β β β β β βββ page.tsx
β β β β β βββ loading.tsx
β β β β βββ new/
β β β βββ π providers/
β β β β βββ configure/
β β β β βββ manage/
β β β βββ π analytics/
β β β β βββ usage/
β β β β βββ costs/
β β β β βββ performance/
β β β βββ π settings/
β β β βββ api-keys/
β β β βββ preferences/
β β β βββ billing/
β β βββ π api/
β β β βββ auth/
β β β β βββ [...nextauth]/
β β β β βββ callback/
β β β βββ chat/
β β β β βββ route.ts (Main gateway)
β β β β βββ π providers/
β β β β β βββ openai/
β β β β β βββ anthropic/
β β β β β βββ gemini/
β β β β β βββ openrouter/
β β β β βββ π stream/
β β β β βββ π batch/
β β β βββ admin/
β β β β βββ providers/
β β β β βββ users/
β β β β βββ system/
β β β βββ analytics/
β β β β βββ realtime/
β β β β βββ historical/
β β β βββ webhooks/
β β β β βββ stripe/
β β β β βββ monitoring/
β β β βββ edge/
β β β βββ rate-limit/
β β β βββ cache/
β β βββ π admin/ (Protected)
β β β βββ dashboard/
β β β βββ provider-management/
β β β βββ user-management/
β β βββ layout.tsx
β β βββ page.tsx
β β βββ globals.css
β β βββ middleware.ts
β βββ π components/
β β βββ π ui/ (shadcn/ui)
β β β βββ button/
β β β βββ card/
β β β βββ dialog/
β β β βββ ...
β β βββ π chat/
β β β βββ ChatInterface.tsx
β β β βββ MessageList.tsx
β β β βββ MessageBubble.tsx
β β β βββ ProviderBadge.tsx
β β β βββ ModelSelector.tsx
β β β βββ TokenCounter.tsx
β β β βββ StreamDisplay.tsx
β β βββ π providers/
β β β βββ ProviderCard.tsx
β β β βββ APIToggle.tsx
β β β βββ ModelGrid.tsx
β β β βββ OpenRouterPanel.tsx
β β β βββ ProviderThemes.tsx
β β βββ π dashboard/
β β β βββ StatsOverview.tsx
β β β βββ UsageChart.tsx
β β β βββ CostCalculator.tsx
β β β βββ RealTimeMetrics.tsx
β β βββ π forms/
β β β βββ APIKeyForm.tsx
β β β βββ ProviderConfigForm.tsx
β β β βββ SettingsForm.tsx
β β βββ π layout/
β β βββ Sidebar.tsx
β β βββ Header.tsx
β β βββ ThemeToggle.tsx
β βββ π lib/
β β βββ π providers/
β β β βββ index.ts (Factory)
β β β βββ base/
β β β β βββ BaseProvider.ts
β β β β βββ types.ts
β β β βββ openai/
β β β β βββ client.ts
β β β β βββ models.ts
β β β β βββ adapter.ts
β β β βββ anthropic/
β β β β βββ client.ts
β β β β βββ adapter.ts
β β β βββ gemini/
β β β β βββ client.ts
β β β β βββ adapter.ts
β β β βββ openrouter/
β β β β βββ client.ts
β β β β βββ models.ts
β β β β βββ router.ts
β β β βββ custom/
β β β βββ provider-builder.ts
β β βββ π api/
β β β βββ gateway/
β β β β βββ router.ts
β β β β βββ load-balancer.ts
β β β β βββ fallback.ts
β β β βββ streaming/
β β β βββ sse.ts
β β β βββ web-socket.ts
β β βββ π utils/
β β β βββ encryption.ts (AES-256)
β β β βββ token-counter.ts
β β β βββ cost-calculator.ts
β β β βββ rate-limiter.ts
β β β βββ logger.ts
β β β βββ validation.ts
β β βββ π db/
β β β βββ schema/
β β β β βββ index.ts
β β β β βββ user.ts
β β β β βββ conversation.ts
β β β β βββ provider.ts
β β β β βββ analytics.ts
β β β βββ queries/
β β β βββ migrations/
β β βββ π auth/
β β β βββ config.ts
β β β βββ middleware.ts
β β β βββ permissions.ts
β β βββ π types/
β β βββ global.d.ts
β β βββ chat.ts
β β βββ providers.ts
β β βββ api.ts
β βββ π hooks/
β β βββ useChat.ts
β β βββ useProviders.ts
β β βββ useAnalytics.ts
β β βββ useWebSocket.ts
β β βββ useDebounce.ts
β β βββ useLocalStorage.ts
β βββ π store/
β β βββ chat.store.ts
β β βββ provider.store.ts
β β βββ user.store.ts
β β βββ ui.store.ts
β β βββ analytics.store.ts
β βββ π styles/
β β βββ themes/
β β β βββ openai.css
β β β βββ anthropic.css
β β β βββ gemini.css
β β β βββ openrouter.css
β β βββ animations.css
β βββ π public/
β βββ themes/
β βββ icons/
β βββ fonts/
βββ π prisma/
β βββ schema.prisma
β βββ migrations/
β βββ seed.ts
βββ π tests/
β βββ unit/
β βββ integration/
β βββ e2e/
βββ π scripts/
β βββ deploy/
β βββ backup/
β βββ monitoring/
βββ π config/
β βββ env.ts
β βββ providers.json
β βββ rate-limits.json
βββ .env.example
βββ .env.local
βββ tailwind.config.ts
βββ next.config.js
βββ tsconfig.json
βββ README.md
OpenRouter Integration Flow:
1. User Request β AnyGPT API Gateway
2. Gateway analyzes request:
βββ Required capabilities
βββ Cost constraints
βββ Performance needs
βββ User preferences
3. Decision Engine:
βββ Use direct provider (if enabled & available)
βββ Use OpenRouter (if better match/fallback)
βββ Load balance between options
4. OpenRouter Router:
βββ Model selection (100+ models)
βββ Automatic retries
βββ Cost optimization
βββ Unified response format
// Core Models
model User {
id String @id @default(cuid())
email String @unique
name String?
avatar String?
role UserRole @default(USER)
tier UserTier @default(FREE)
// Relationships
apiKeys ApiKey[]
conversations Conversation[]
settings UserSettings?
usage UsageStats[]
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Provider {
id String @id @default(cuid())
name ProviderType // openai, anthropic, gemini, openrouter
displayName String
enabled Boolean @default(true)
config Json? // Provider-specific config
// Models available for this provider
models Model[]
// Usage tracking
totalRequests Int @default(0)
totalTokens BigInt @default(0)
totalCost Float @default(0)
// Rate limiting
rateLimit Int @default(1000)
rateWindow Int @default(3600) // seconds
}
model ApiKey {
id String @id @default(cuid())
userId String
providerId String
name String
key String @encrypted
enabled Boolean @default(true)
lastUsed DateTime?
usageCount Int @default(0)
// Relationships
user User @relation(fields: [userId], references: [id])
provider Provider @relation(fields: [providerId], references: [id])
createdAt DateTime @default(now())
}
model Conversation {
id String @id @default(cuid())
userId String
title String @default("New Conversation")
providerId String
model String
tokensUsed Int @default(0)
estimatedCost Float @default(0)
// Messages (1:N)
messages Message[]
// Metadata
tags String[] @default([])
archived Boolean @default(false)
starred Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Message {
id String @id @default(cuid())
conversationId String
role MessageRole // system, user, assistant
content String
tokens Int
model String?
provider ProviderType?
cost Float?
// Metadata
processingTime Int? // milliseconds
error String?
createdAt DateTime @default(now())
}
// OpenRouter Specific
model OpenRouterRequest {
id String @id @default(cuid())
conversationId String
model String
route String // which model was actually used
cost Float
latency Int
success Boolean
fallbackUsed Boolean @default(false)
createdAt DateTime @default(now())
}
// Analytics
model UsageAnalytics {
id String @id @default(cuid())
userId String
date DateTime
provider ProviderType
model String
requests Int
tokens Int
cost Float
@@index([userId, date])
}Request Flow:
1. Request β API Gateway (/api/chat)
2. Authentication & Authorization
3. Rate Limiting Check
4. Request Analysis:
βββ Extract provider preference
βββ Check provider availability
βββ Validate model compatibility
βββ Apply user quotas
5. Provider Selection Logic:
βββ If specific provider requested β use it
βββ If OpenRouter preferred β route through OpenRouter
βββ If fallback needed β use OpenRouter as backup
βββ If load balancing β distribute requests
6. Request Processing:
βββ Format conversion
βββ Token counting
βββ Cost estimation
βββ Stream handling
7. Response:
βββ Standardized format
βββ Analytics logging
βββ Real-time updates
βββ Error handling
// Provider Configuration Interface
interface ProviderConfig {
id: 'openai' | 'anthropic' | 'gemini' | 'openrouter' | 'custom';
name: string;
enabled: boolean;
apiKey?: string; // Encrypted
baseURL?: string;
models: {
id: string;
name: string;
enabled: boolean;
capabilities: string[]; // ['chat', 'vision', 'function-calling']
contextWindow: number;
maxTokens: number;
cost: {
input: number; // per 1K tokens
output: number; // per 1K tokens
};
}[];
rateLimit: {
requests: number;
tokens: number;
window: number; // seconds
};
features: {
streaming: boolean;
vision: boolean;
functionCalling: boolean;
jsonMode: boolean;
};
ui: {
theme: string;
icon: string;
color: string;
};
}
// OpenRouter Specific Config
interface OpenRouterConfig extends ProviderConfig {
routing: {
strategy: 'cost' | 'performance' | 'quality';
fallbackProviders: string[];
costLimit: number;
timeout: number;
};
models: Array<{
id: string; // OpenRouter model ID
provider: string; // Original provider
name: string;
// ... other model config
}>;
}Theming Layers:
1. Base Theme (Tailwind)
2. Provider Themes:
βββ OpenAI: Green gradient, clean
βββ Anthropic: Purple gradient, elegant
βββ Gemini: Blue gradient, modern
βββ OpenRouter: Orange gradient, dynamic
3. User Preferences:
βββ Dark/Light mode
βββ Compact/Comfortable
βββ Custom colors
4. Dynamic Elements:
βββ Message bubbles
βββ Loading states
βββ Buttons & inputs
βββ Status indicators
Monitoring Stack:
1. Real-time Metrics:
βββ Request rate
βββ Token usage
βββ Response times
βββ Error rates
βββ Cost tracking
2. Provider Health:
βββ Uptime monitoring
βββ Latency tracking
βββ Success rates
βββ Cost efficiency
3. User Analytics:
βββ Usage patterns
βββ Model preferences
βββ Feature usage
βββ Retention metrics
4. Business Metrics:
βββ Cost per user
βββ Provider costs
βββ ROI calculation
βββ Growth tracking
Security Layers:
1. Encryption:
βββ API Keys (AES-256)
βββ Database fields
βββ Communication
2. Authentication:
βββ NextAuth.js
βββ OAuth providers
βββ API key authentication
βββ Session management
3. Authorization:
βββ Role-based access
βββ Provider permissions
βββ Rate limiting
βββ Request validation
4. Monitoring:
βββ Audit logs
βββ Suspicious activity
βββ API abuse detection
βββ Compliance tracking
Multi-Environment Setup:
1. Development:
βββ Local Docker Compose
βββ Hot reload
βββ Mock providers
2. Staging:
βββ Vercel Preview
βββ Test database
βββ Limited API access
3. Production:
βββ Vercel Production
βββ PostgreSQL (Neon)
βββ Redis (Upstash)
βββ Object storage
βββ CDN for static assets
4. Backup:
βββ Automated backups
βββ Point-in-time recovery
βββ Disaster recovery plan
Horizontal Scaling:
1. Stateless API servers
2. Database connection pooling
3. Redis cache for sessions
4. CDN for static assets
5. Queue system for async tasks
Vertical Scaling:
1. Database optimization
2. Query optimization
3. Caching strategy
4. Asset optimization
Cost Optimization:
1. Provider cost comparison
2. Token usage optimization
3. Caching responses
4. Batch processing
This architecture provides a production-ready, scalable, and maintainable foundation for AnyGPT with complete OpenRouter integration and enterprise-grade features.