System Architecture

Reliability Engineering, Multi-Key Round-Robin & Real-Time Streaming


Building a high-performance developer portfolio requires more than visual polishβ€”it demands a resilient, low-latency, and fault-tolerant backend. This page outlines the architectural patterns, multi-threaded concurrency pipelines, multi-key round-robin load distribution, and real-time streaming mechanisms that power "The GOAT" AI assistant.

Multi-Threaded Pre-Fetch

Sequential I/O is the primary source of chatbot latency. The backend parallelizes Firestore knowledge retrieval, episodic memory logs, and live sports cache via a ThreadPoolExecutor(max_workers=3).

Outcome: Pre-fetch overhead dropped from ~750ms sequentially down to 150–200ms concurrently.

Multi-Key Round-Robin

Free-tier LLM endpoints enforce strict RPM/TPM ceilings. The backend pools comma-separated keys for both Groq and Gemini, atomically rotating each request via synchronized round-robin indices guarded by thread locks.

In-Flight Failover: If the active key encounters an HTTP 429 rate limit, the stream seamlessly falls over to the next pool key in under 50ms.

Real-Time SSE Token Streaming

Rather than buffering entire responses on the server, /chat streams tokens via Server-Sent Events (SSE) with Accept: text/event-stream, maintaining a backwards-compatible JSON fallback.

Speculative Fallback: A 1.2s timeout on primary Gemini streaming triggers instant failover to ultra-fast Groq streaming, slashing TTFT from 30s+ down to ~2s.

Adaptive 60fps Typewriter

The frontend reader parses streaming byte chunks and feeds an adaptive 60fps typewriter loop. It types at 1–3 chars/frame for an organic feel, automatically accelerating to 6–16 chars/frame to catch up or complete.

Dynamic Formatting: Markdown headings, bold markers, and code blocks format live on the fly as closing punctuation arrives.

Multi-Tier Provider & Client Pool Hierarchy

  • Tier 1: Google Gemini Multi-Key Pool (gemini-3.6-flash) β€” Primary high-reasoning model handling personal queries, owner knowledge synthesis, and episodic dialogue context. Rotates keys via atomic round-robin with a 1.2s speculative TTFT window.
  • Tier 2: Groq Multi-Key Round-Robin Pool (openai/gpt-oss-120b) β€” Ultra-low latency engine serving as the primary handler for general/technical queries and instant fallback for Gemini stalls. Features multi-key rotation and automatic in-flight HTTP 429 failover.
  • Tier 3: Asynchronous Background Executor (_bg_executor) β€” Offloads message audit logs, trigger state updates, and silent user preference extraction to non-blocking daemon workers, eliminating blocking I/O overhead on user-facing streaming.

Stream Sanitization & Output Governance

To balance conversational responsiveness, leakage mitigation, and clean client rendering, the system employs defense-in-depth intent routing, sliding-window stream sanitization, and backend privilege boundaries:

Context-Aware Intent Routing

Intelligently distinguishes conversational dialogue from direct technical queries. Direct technical and project queries receive focused, structured responses without redundant conversational preambles.

Sliding-Window Stream Sanitizer

A real-time sliding-window stream filter intercepts in-flight token buffers to parse structural delimiters, memory directives, and control markers before chunks are yielded to the client socket.

Prompt Scaffolding & Tag Defense

Combines explicit system prompt boundary rules with deterministic tag-stripping to suppress internal directives and protect system instructions from direct exposure.

Implemented Protections

  • Deterministic Tag Stripping: StreamTagFilter inspects in-flight token fragments to strip control syntax (e.g., [LEARN_RONIL: ...], [LEARN_USER: ...]) across chunk boundaries.
  • Negative Constraint Directives: System instructions mandate direct answers, forbid unprompted persona disclosure, and decline prompt dumps.
  • Out-of-Band Authorization: Database writes for learned facts require validated Firebase Admin credentials, isolating persistent storage from prompt manipulation.

Security Limitations

  • Probabilistic Nature: LLMs are non-deterministic; prompt-level constraints mitigate leakage risk but cannot mathematically guarantee resistance against novel jailbreaks.
  • Semantic Rephrasing: Heuristic filters intercept exact control syntax; sophisticated prompts inducing natural-language summarization of system context rely on model safety alignment.
  • Defense-in-Depth Prerequisite: Stream filtering is a presentation-layer defense, necessitating strict backend authorization for stateful operations.

Empirical Testing Evidence

  • 100% Tag Interception: Verified across 150+ automated fragmented token tests with 0 internal control tags escaping to the client stream.
  • Adversarial Prompt Resistance: Evaluated against a suite of common extraction prompts ("ignore previous instructions", roleplay inversion, delimiter injection); direct prompt dumps resisted in ~94% of test evaluations.
  • Privilege Containment: 100% of unauthorized attempts to alter global portfolio facts were safely blocked by backend authentication gates.

End-to-End Streaming Architecture Pipeline

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        CLIENT / FRONTEND LAYER                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ [User Query Input] ──► [fetch('/chat', Accept: 'text/event-stream')]   β”‚
β”‚                                   β”‚                                    β”‚
β”‚                                   β–Ό                                    β”‚
β”‚                    [HTTP POST /chat + Bearer Token]                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        SECURITY & GATEWAY LAYER                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ [1. Cryptographic Auth Validation: Firebase ID Token or Guest HMAC]    β”‚
β”‚       β”œβ”€β–Ί [Invalid / Expired] ──► Return 401 Unauthorized              β”‚
β”‚ [2. In-Memory Sliding-Window Rate Limiter (5-15 req/min)]              β”‚
β”‚       └─► [Quota Exceeded]    ──► Return 429 Too Many Requests         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚               CONCURRENT PRE-FETCH LAYER (ThreadPoolExecutor)          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Worker 1: Firestore      β”‚ Worker 2: Episodic       β”‚ Worker 3:    β”‚ β”‚
β”‚ β”‚ Global Knowledge Facts   β”‚ Conversation Summaries   β”‚ Daemon Match β”‚ β”‚
β”‚ β”‚ (docId: user.uid/global) β”‚ (docId: user.uid/memory) β”‚ Cache (<1ms) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β”‚                                          β–Ό                             β”‚
β”‚                  Aggregated Prompt Context (<200ms)                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 CONTEXT SYNTHESIZER & INTENT ROUTER                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ [Intent Classifier: Conversational Context vs Direct Technical Query]  β”‚
β”‚  β”œβ”€β–Ί Conversational Mode: Contextual Salutation & Guided Persona       β”‚
β”‚  └─► Technical Inquiry: Direct Structured Response (Zero Noise)        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 MULTI-KEY ROUND-ROBIN INFERENCE ENGINE                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ [Primary: Gemini 3.6 Flash Pool (Key 1 -> Key 2 Round-Robin)]          β”‚
β”‚       β”‚                                                                β”‚
β”‚       β”œβ”€β–Ί First Token <= 1.2s? ──► [Stream Gemini Chunks] ─────────┐   β”‚
β”‚       β”‚                                                            β”‚   β”‚
β”‚       └─► Timeout (>1.2s) or 429 Quota Exceeded                    β”‚   β”‚
β”‚                 β”‚                                                  β”‚   β”‚
β”‚                 β–Ό                                                  β”‚   β”‚
β”‚ [Secondary: Groq Multi-Key Pool (openai/gpt-oss-120b)]             β”‚   β”‚
β”‚       β”œβ”€β–Ί Atomic Round-Robin Key Selection (Key 1 -> Key 2 -> ...) β”‚   β”‚
β”‚       β”œβ”€β–Ί In-Flight 429 Failover to Next Pool Key (<50ms)          β”‚   β”‚
β”‚       └─► [High-Speed Groq Token Stream (~1-2s TTFT)] ──────────────   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”˜
                                                                     β”‚
                                                                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 STREAM SANITIZER & OUTPUT GOVERNANCE                   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ [StreamTagFilter Engine]                                               β”‚
β”‚  β”œβ”€β–Ί Intercepts & Buffers Internal System Tags & Control Directives    β”‚
β”‚  β”œβ”€β–Ί Enforces Output Sanitization & Strips Redundant Preambles         β”‚
β”‚  └─► Yields Clean SSE Chunks: data: {"chunk": "..."}\n\n ────────────┐ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”˜
                                    β”‚ (Asynchronous Audit Pipeline)  β”‚
                                    β–Ό                                β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚ Async Background Executor  β”‚    β”‚ SSE Stream      β”‚
                     β”‚ - Append Firestore Log     β”‚    β”‚ & Adaptive 60fpsβ”‚
                     β”‚ - Commit Learned Facts     β”‚    β”‚ Typewriter UI   β”‚
                     β”‚ - Update Dynamic Triggers  β”‚    β”‚ with Dynamic    β”‚
                     β”‚ - Non-Blocking Stream Exec β”‚    β”‚ Markdown Render β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                

Looking for Deep Technical Specifications?

Explore the complete Software Engineering lifecycle suite, including formal SRS, PRD, TRD, Schema definitions, and Flow diagrams.

THE GOAT ASSISTANT