Skip to content
vs.
Articles

June 12, 2026 · 9 min read

A portfolio chatbot on Gemini at $0: architecture, guardrails and observability

This site has an AI assistant in the corner of the screen. It answers questions about my career, captures leads from recruiters and costs me exactly $0.00 a month. This article documents the architecture decisions — including the things I decided not to build, which in the end are the most important ones.

The decision that defines the project: context-stuffing, not RAG

The default reflex for “a chatbot that answers questions about X” in 2026 is to set up a RAG pipeline: embeddings, a vector database, chunking, re-ranking. For a career record that fits in a few kilobytes, that is over-engineering — more infrastructure, more latency, more points of failure, to retrieve a document that would fit entirely in the prompt.

The knowledge base is a hand-curated markdown file (knowledge.ts) embedded in the system prompt of every call. Full editorial control over the bot's voice, zero retrieval infrastructure. RAG becomes a documented upgrade for when there is enough volume to justify it — not before. The engineering philosophy on my home page applies here: deterministic strategy first, AI as a last resort, cost accounted for from day 1.

$0 as an architecture requirement, not an accident

The model is Gemini 2.5 Flash on the free tier, called server-side by a Cloudflare Worker (Hono) — the key never reaches the browser. But a free tier without guardrails is a bill waiting to happen (or, at the very least, a dead quota). Protection has three layers, all in Cloudflare KV:

Everything is fail-closed: if KV fails, the Worker returns an error before calling Gemini. And the final guarantee is not in the code: the key's Google project has no billing attached. Blowing the quota produces a 429, never a charge. Absolute worst case: the chat is down until the next day — availability is negotiable, cost is not.

Resilience that costs nothing: model fallback

The 2.5 Flash free tier has a tight per-minute quota and returns the occasional 503 when the model is overloaded. The cheap fix: flash-lite has a separate quota bucket. When flash fails — 429, 503, any error — the same request tries lite immediately, with no wait. Visitors only see an error if both fail, and telemetry records which model served each answer.

Streaming end to end

The Worker consumes Gemini's SSE, extracts the text deltas with a TransformStream (handling frames split across network chunks) and re-emits SSE to the browser. The React widget accumulates the tokens in a message placeholder — the first word appears on screen in under a second, instead of a 3-5 s wait for the full answer.

Lead capture: the LLM never touches the data

The feature with the most business value uses no AI at all. When the visitor engages (two completed answers, or the bot mentioning the contact page), a deterministic card appears in the conversation: a name and a free-form contact field — email, WhatsApp, whatever the person prefers. Forced formats kill conversion.

Asking the model to extract a name and phone number from the conversation would be less reliable, open a prompt-injection flank and mix PII into the LLM flow. The card is a plain form: honeypot, its own rate limit, email via Resend straight to my inbox. Analytics only gets a counter — personal data lives exclusively in the email, which keeps the privacy design clean under the LGPD (Brazil's data protection law).

Observability: if it's not in Grafana, it didn't happen

The Worker emits server-side events to Cloudflare Analytics Engine — chat_served (latency, answer length, model), chat_blocked (with the block reason) and chat_error — with no session and no message content. The widget emits session events (gated by consent). In Grafana this becomes a funnel — page_view → chat_open → chat_message → chat_lead — plus a quota panel with a visual threshold at 80% of the daily cap. When the limit gets tight, I see it before the visitors do.

Quality: 235 tests and coverage locked at 100%

The project has 235 tests behind a 100% coverage gate — statements, branches, functions and lines — that fails CI on any regression. The most interesting ones are the abuse tests: oversized payloads rejected before any side effect, IP spoofing (CF-Connecting-IP beats the forgeable X-Forwarded-For), a distributed attack hitting the global cap without burning quota, and XSS escaped in lead emails.

What I would do differently

The assistant is right there in the corner of the screen — ask it anything you want about me.