Skip to main content

Single Chat Request Lifecycle

Phase 1: HTTP Reception

Middleware Pipeline:
  1. ASP.NET Core request logging
  2. JWT authentication middleware validates token
  3. CORS middleware (allows all origins in development)
  4. Request reaches ArenaController.Chat()

Phase 2: JWT Validation

Process:
JWT Claims Required:
  • sub: User UUID (becomes userId in code)
  • email: User email
  • aud: Must be “authenticated”
  • iss: Must match Supabase URL
  • exp: Expiration timestamp
Failure: Returns 401 Unauthorized before controller method executes

Phase 3: User Synchronization

Database Operation:
Characteristics:
  • 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:
Query:
Explicit Selection:
Validation: If model not found or inactive, returns 400 Bad Request

Phase 5: Provider Execution

Provider Selection:
Groq Provider Execution (Primary):
Fallback Chain:
  1. Groq with selected model (45s timeout)
  2. Groq with llama-3.3-70b-versatile (45s timeout)
  3. Bytez provider (45s timeout)
  4. Throw exception → 500 error response

Phase 6: Message Persistence

If threadId provided:
Database Operation:
If threadId omitted: No database write occurs

Phase 7: Response Construction

HTTP Response:

Dual Chat Request Lifecycle

Differences from Single Chat

Phase 4b: Model Pair Selection
Phase 5b: Parallel Execution
Execution Properties:
  • Both models execute simultaneously
  • Independent timeout counters (45s each)
  • Independent fallback chains
  • One model failure doesn’t block the other
Phase 6b: Comparison Persistence
Then link to thread message:

Streaming Request Lifecycle

Differences from Non-Streaming

Phase 1b: SSE Headers
Phase 5c: Streaming Callback
Client Disconnect Handling:
Phase 6c: No Persistence Streaming endpoints do not write to database. Client must use non-streaming endpoint separately if persistence needed.

Error 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
Dual Chat (~2000ms):
  • 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
Key Insight: Dual chat is only marginally slower than single chat due to parallel execution.

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