MK-QuizFlow

AI_ARCHITECTURE.md — MK QuizFlow v2

This document details the AI integration architecture, routing, rate limits, and BYOK overrides.

Routing Pipeline

Browser Request (fetch)
  │
  ├──► Attach x-byok-key (if client key is provided)
  │
  ▼
POST /api/ai/[capability]
  │
  ├──► Rate Limiter: Per-IP token-bucket check (12/min)
  │
  ├──► Input Validator: Zod parsing of body parameters
  │
  ├──► Credentials resolver: BYOK key ? createGateway({ apiKey }) : ambient gateway (server AI_GATEWAY_API_KEY / Vercel OIDC)
  │
  ├──► Quota checker: If not BYOK, consume daily free allowance (40/day limit)
  │
  ▼
Vercel AI SDK generateObject
  │
  ├──► Model: quality (Claude Sonnet 4.5) / fast (Claude Haiku 4.5)
  │
  ▼
Browser Response (with X-Quota-Remaining headers)

Credentials & degradation

Security & memory commitments