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:
Intelligently distinguishes conversational dialogue from direct technical queries. Direct technical and project queries receive focused, structured responses without redundant conversational preambles.
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.
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:
StreamTagFilterinspects 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.