Building an AI Chat for My Blog: Gemini, Cloud Functions, $5-15/month - The Emergence Machine
Skip to content<br>Building an AI Chat for My Blog: Gemini, Cloud Functions, $5-15/month<br>Written by<br>Sail S<br>in<br>engineering
March 4, 2026
Trace. Question. Emerge.
Articles are general. Readers are specific. I built an AI chat to close that gap — it RAGs my entire blog about emergence theory and lets readers bring their own context to the frameworks I write about. A founder applies competition theory to SaaS pricing. A strategist rethinks geopolitics. Someone connects two posts I never expected to relate. Single Cloud Function, Gemini Flash, Firestore, no frameworks, $5–10/month.<br>Here’s the architecture.<br>Architecture<br>The frontend is vanilla JavaScript (2K lines, no dependencies) running on WordPress shared hosting (Hostinger, $4/month). It handles streaming SSE responses and session management. A PHP page template serves the chat interface, and a mu-plugin handles security monitoring.<br>The backend is a single GCP Cloud Function (Python 3.12) that does everything: reCAPTCHA Enterprise verification, IP-based rate limiting via Firestore, RAG retrieval (vector search + RSS feed), question classification, Gemini streaming, session management, and async Telegram alerts. A second Cloud Function handles security monitoring and budget backstops.<br>Firestore is the only database — it stores session state, vector embeddings for RAG, rate limiting counters, and security analytics.<br>RAG: Two Layers<br>Layer 1 — RSS feed (dynamic). Every hour, the function fetches the blog’s RSS feed, extracts up to 15 articles, and injects them as context. Zero indexing required. New posts are available within an hour of publishing.<br>Layer 2 — Firestore vector search (precise). Foundational articles are chunked (~1,500 chars each), embedded with gemini-embedding-001 (768 dimensions), and stored in Firestore. Each query gets embedded, and the top 4 chunks by cosine similarity are retrieved.<br>Vector search finds the most relevant chunks; RSS ensures fresh content is always available. If vector search fails (cold start, network timeout), the system falls back to full RSS context. For most blogs, the RSS layer alone gets you 80% of the way.<br>Smart Model Routing<br>Not every question needs an expensive thinking model. “What is emergence?” doesn’t require extended thinking. “If language shaped how humans abstract the world, and LLMs learn from that language, are they inheriting our abstraction blind spots?” does.<br>Prometheus uses a regex classifier that runs in Readers can invoke Prometheus from any page, and it arrives pre-loaded with starter questions based on the article they just read.<br>Coming from “The Sensing Surface” (about how understanding cycles through absorption, anomaly, and abstraction):<br>“What framework am I inside that feels permanent but might be mid-collapse?”<br>“Where is someone deliberately stalling understanding in my industry?”<br>Coming from “What Kind of Fight Is This?” (about six classes of competition):<br>“How do I identify which class a competitor is in?”<br>“What vulnerabilities does each class have?”<br>Guardrails and Budget Controls<br>Running an AI chat on a public site means you need abuse controls. IP-based rate limiting prevents individual users from running up API costs. Daily and monthly budget caps track actual Gemini spend in Firestore and shut off the chat automatically if thresholds are crossed, with Telegram alerts at every milestone. Security monitoring and automated blocking layers sit on top — shared hosting limits infrastructure-level controls, but WordPress hooks handle more than you’d expect.<br>Subscription as Earned Conversion<br>Users get 8 messages per session. That’s enough for a meaningful conversation. When they hit the limit, they see an option: enter your email for 5 more messages.<br>This is deliberately not a paywall. It’s “the chat was useful to you — want more?” The subscription is earned by the conversation, not demanded upfront. If the AI gave shallow answers for 8 messages, nobody would subscribe.<br>At the end of the extended session, users get a summary of their conversation and an invitation to continue the discussion in the project’s subreddit — a ready-made post they can copy over where they can talk with real humans if they are still curious or refresh page to start over. The email goes to WordPress’s Newsletter plugin via REST API, and the session limit extends immediately.<br>Cost Breakdown<br>This is a niche site — most days it gets bots and a handful of visitors, occasionally more. The entire infrastructure had to work at near-zero cost during quiet periods and scale without intervention during spikes.<br>At ~500 conversations/month (current traffic):<br>Service Cost/month Notes Website hosting (Hostinger)$4Shared hostingGCP Cloud Functions~$0Free tier covers itFirestore~$0Free tier covers itGemini API (Flash + Flash Lite)$1–10Varies with traffic and depth of questionsreCAPTCHA Enterprise~$0Free tierTelegram Bot API$0FreeTotal$5–15 At...