Single Chat Request Lifecycle
Phase 1: HTTP Reception
- ASP.NET Core request logging
- JWT authentication middleware validates token
- CORS middleware (allows all origins in development)
- Request reaches
ArenaController.Chat()
Phase 2: JWT Validation
Process:sub: User UUID (becomesuserIdin code)email: User emailaud: Must be “authenticated”iss: Must match Supabase URLexp: Expiration timestamp
Phase 3: User Synchronization
- UPSERT operation (idempotent)
- Executes on every authenticated request
- Non-blocking (awaited but fast due to index)
- Failure logged as warning, doesn’t fail request
Phase 4: Model Selection
Random Selection:Phase 5: Provider Execution
Provider Selection:- Groq with selected model (45s timeout)
- Groq with
llama-3.3-70b-versatile(45s timeout) - Bytez provider (45s timeout)
- Throw exception → 500 error response
Phase 6: Message Persistence
If threadId provided:Phase 7: Response Construction
Dual Chat Request Lifecycle
Differences from Single Chat
Phase 4b: Model Pair Selection- Both models execute simultaneously
- Independent timeout counters (45s each)
- Independent fallback chains
- One model failure doesn’t block the other
Streaming Request Lifecycle
Differences from Non-Streaming
Phase 1b: SSE HeadersError Handling Flow
Provider Failure
Authorization Failure
Validation Failure
Performance Characteristics
Request Timing Breakdown
Single Chat (~2000ms):- JWT validation: ~5ms
- User sync: ~10ms (indexed UPSERT)
- Model selection: ~5ms (indexed query)
- Provider execution: ~1500-2000ms (AI inference)
- Message persistence: ~10ms (INSERT)
- Response serialization: ~5ms
- JWT validation: ~5ms
- User sync: ~10ms
- Model selection: ~10ms (2 queries)
- Parallel provider execution: max(1500ms, 1600ms) ≈ 1600ms
- Comparison persistence: ~15ms (2 INSERTs)
- Response serialization: ~10ms
Database Transaction Boundaries
User Sync: Auto-commit (single UPSERT) Message Persistence: Auto-commit (single INSERT) Dual Chat Persistence: No explicit transaction (2 sequential INSERTs, not atomic) Constraint: No multi-statement transactions. Each database operation commits immediately. Implication: Comparison can be written without corresponding message (if thread message insert fails). This is acceptable since comparisons are first-class entities.Next Steps
Database Schema
Table structures and relationships
System Invariants
Enforceable constraints and rules
Authentication Flow
JWT validation internals