{"slug": "how-we-built-an-ai-saas-on-the-edge-for-nearly-0-in-infrastructure-costs", "title": "How We Built an AI SaaS on the Edge for Nearly $0 in Infrastructure Costs", "summary": "A developer built Propoza, an AI-powered SaaS for generating business proposals, using Cloudflare Workers and D1 database to achieve near-zero infrastructure costs. The edge serverless approach eliminated the need for traditional servers, reducing monthly costs from $10-$90 to nearly $0 during validation. The stack leverages Hono framework for HTTP routing and Zod for validation, running across 330+ global data centers for low latency.", "body_md": "A few months ago, we started building [Propoza](https://propoza.com.br), a tool that generates business proposals with AI for Brazilian freelancers and small business owners.\n\nThe problem was practical: freelancers spend hours crafting proposals in Canva or Word, delivering generic documents that don't protect the project scope. The tool needed to be simple — the user describes the service, the AI structures a complete proposal with scope, timeline, and payment terms.\n\nThe technical challenge: how do you run a freemium SaaS for Brazilian freelancers without blowing your budget before you have revenue?\n\nA lean SaaS or micro SaaS typically costs between $5 and $20 per month before your first paying customer — just for server, managed database, and CDN. For a validation-phase product, that's burned money.\n\nWe decided to try a different route: build everything on the edge, with serverless infrastructure and zero servers to manage. Here's what worked, the trade-offs, and what we learned.\n\nBefore choosing our stack, we calculated the minimum cost of a Brazilian SaaS running on conventional infrastructure:\n\n| Component | Typical Provider | Estimated Monthly Cost |\n|---|---|---|\n| Server | DigitalOcean / AWS EC2 | $8–$30 |\n| Database | Managed PostgreSQL (RDS, Supabase) | $10–$40 |\n| CDN | Cloudflare (paid plan) or similar | $5–$20 |\n| Domain | Namecheap / Porkbun | ~$1/month |\nTotal |\n$10–$90/month |\n\nThat's not unreasonable for an established SaaS. But for a product still validating its market fit with zero paying customers, it means burning $100–500 before you even know if the model works.\n\nThere's another Brazil-specific problem: **latency**. Servers concentrated in São Paulo and Rio de Janeiro leave users in the North and Northeast with poor experiences, especially on mobile connections. A CDN helps, but adds cost.\n\nWe needed something that:\n\nThat's when we looked at the edge serverless model.\n\nThe application's backbone is Cloudflare Workers — a serverless runtime that executes code across 330+ data centers worldwide, including points of presence in Brazil (São Paulo, Rio de Janeiro, Fortaleza).\n\n**The trade-off:** Workers is not Node.js. There's no filesystem access, native WebSocket, or Node standard library. It's an isolated V8 runtime. Most things you need exist as native APIs (fetch, Web Crypto, streams), but libraries depending on `fs`\n\nor `net`\n\nwon't work.\n\nWe needed an HTTP framework that ran inside Workers with zero overhead.\n\nMost popular Node.js frameworks (Express, Fastify, Koa) were designed for full Node.js environments and have compatibility issues. They depend on the `http`\n\nmodule, use synchronous APIs, or carry middleware too heavy for the serverless model.\n\nHono sidesteps all that. It's under 14KB, runs natively on Workers, Deno, Bun, and Node.js, and is TypeScript-first with strong type inference. It supports middleware, route params, and Zod validation.\n\nHere's a proposal generation route:\n\n``` js\nimport { Hono } from 'hono'\nimport { z } from 'zod'\nimport { zValidator } from '@hono/zod-validator'\n\nconst app = new Hono()\n\nconst proposalSchema = z.object({\n  clientName: z.string().min(1),\n  projectDescription: z.string().min(10),\n  deliverables: z.array(z.string()).min(1),\n  deadline: z.string(),\n  paymentMethod: z.string()\n})\n\napp.post('/api/proposals/generate', zValidator('json', proposalSchema), async (c) => {\n  const data = c.req.valid('json')\n  const result = await generateProposal(data)\n  return c.json({ proposal: result })\n})\n\nexport default app\n```\n\nValidation with Zod at the edge of the request, combined with Hono's inferred types, eliminates several runtime bugs without adding processing overhead.\n\nThe database choice was the hardest decision. For an application that needs to persist proposals, users, and settings, the relational model is still the most natural fit.\n\nWe examined two options in the edge ecosystem:\n\nWe went with D1 for its native integration with Workers. You define the schema locally, run migrations with `wrangler d1 migrations apply`\n\n, and queries run in the same data center as the Worker.\n\n```\nCREATE TABLE proposals (\n  id TEXT PRIMARY KEY,\n  user_id TEXT NOT NULL,\n  client_name TEXT NOT NULL,\n  content TEXT NOT NULL,\n  status TEXT DEFAULT 'draft',\n  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE users (\n  id TEXT PRIMARY KEY,\n  email TEXT UNIQUE NOT NULL,\n  name TEXT NOT NULL,\n  proposals_count INTEGER DEFAULT 0,\n  created_at DATETIME DEFAULT CURRENT_TIMESTAMP\n);\n```\n\n**Issues we ran into:**\n\n`ALTER TABLE`\n\nwith complex constraints, `RETURNING`\n\n, and some window functions aren't available. Always test with `wrangler dev`\n\nbefore deploying.The most expensive part of an AI SaaS isn't infrastructure — it's the LLM API. Each call costs fractions of a penny, but hundreds of calls per day add up fast.\n\nThe AI Gateway acts as a proxy between the Worker and the LLM API:\n\nThe request flow:\n\n``` php\nRequest -> Worker -> AI Gateway -> Cache hit? Return cached response\n                                -> Cache miss? LLM API -> Cache the response -> Return\n```\n\nCaching cut about 40% of actual LLM calls in the first few weeks. Many users test with similar inputs.\n\nFrontend with React + Vite, hosted on Cloudflare Pages. The free tier covers 500 builds/month and unlimited bandwidth for static sites.\n\nCommunication with the API is via typed `fetch`\n\n, leveraging the fact that both Worker and frontend share the same TypeScript types:\n\n```\n// shared types\ninterface GenerateRequest {\n  clientName: string\n  projectDescription: string\n  deliverables: string[]\n  deadline: string\n  paymentMethod: string\n}\n\n// client-side call\nconst response = await fetch('/api/proposals/generate', {\n  method: 'POST',\n  headers: { 'Content-Type': 'application/json' },\n  body: JSON.stringify(request)\n})\n```\n\nOne detail that made a difference: **PDFs are generated client-side**, using libraries like `@react-pdf/renderer`\n\nor `html2canvas + jspdf`\n\n. The Worker never allocates memory or CPU for PDF rendering — the user's browser handles it.\n\nAfter a few weeks in production with dozens of active users:\n\n| Component | Service | Cost/Month |\n|---|---|---|\n| API / Backend | Cloudflare Workers (Free Tier) | $0 |\n| Database | Cloudflare D1 (Free Tier) | $0 |\n| Frontend / Hosting | Cloudflare Pages (Free Tier) | $0 |\n| AI (LLM) | External API (cached via AI Gateway) | ~$1–$4 |\n| Domain | Namecheap / Porkbun | ~$1/month |\nTotal |\n< $5/month |\n\nThe only variable cost is the LLM. It scales with actual usage, not with registered users. If someone signs up but never generates a proposal, the cost is zero.\n\nThis isn't forever. When we scale to thousands of users, the Workers free tier will need an upgrade ($5+/month after 100k requests/day), and D1 may need a paid plan. But for PMF validation, it buys you months of experimentation.\n\nFive things we'd take to the next project:\n\n**1. Remote debugging on Workers is harder. Wrangler tail helps.**\n\nThere's no SSH into a Worker. `wrangler tail`\n\nstreams live production logs. Pair it with structured logging (JSON with `requestId`\n\n, `userId`\n\n, `latency`\n\n).\n\n**2. Secrets, vars, and bindings have nuances.**\n\nAPI keys go in `secrets`\n\n. Public config in `vars`\n\n. Resources like D1 or KV go in `bindings`\n\n(direct runtime references). Secrets aren't accessible via `wrangler dev`\n\nwithout a `.dev.vars`\n\nfile.\n\n**3. D1 doesn't accept everything SQLite accepts.**\n\nA migration that worked locally broke on D1 because it used `ALTER TABLE ... ADD COLUMN`\n\nwith constraints D1 rejects. Test every migration with `wrangler d1 migrations apply --local`\n\n.\n\n**4. AI Gateway saved us instrumentation work.**\n\nWe didn't need to implement token logging, caching, or rate limiting manually. The gateway delivers everything through environment variables. Saved days of work.\n\n**5. Hono + Zod is a solid combo for secure edge APIs.**\n\nValidation at the request edge with auto-inferred types eliminates runtime errors without adding noticeable latency.\n\nBuilding on the edge with Cloudflare Workers let us validate the product without the fixed costs of a traditional SaaS. The stack is lean, deployment is `wrangler deploy`\n\n, and the infra bill doesn't scare you.\n\nIs this for every SaaS? No. If you need heavy computation, complex queues, or a database with all the features of PostgreSQL, the edge stack has limitations. But for validation in markets where every dollar counts, it works.\n\nThe tool we built is called Propoza — an AI-powered proposal generator for freelancers. It's free to use.\n\nIf you've used this stack or have a different approach to low-cost SaaS infrastructure, I'd love to hear about it in the comments. The goal here is to share experience, not to sell technology.", "url": "https://wpnews.pro/news/how-we-built-an-ai-saas-on-the-edge-for-nearly-0-in-infrastructure-costs", "canonical_source": "https://dev.to/theo_oliveira_40b15cfaf73/how-we-built-an-ai-saas-on-the-edge-for-nearly-0-in-infrastructure-costs-24jm", "published_at": "2026-06-18 04:26:22+00:00", "updated_at": "2026-06-18 04:51:21.432226+00:00", "lang": "en", "topics": ["ai-products", "developer-tools", "artificial-intelligence", "ai-infrastructure"], "entities": ["Propoza", "Cloudflare Workers", "D1", "Hono", "Zod", "Brazil"], "alternates": {"html": "https://wpnews.pro/news/how-we-built-an-ai-saas-on-the-edge-for-nearly-0-in-infrastructure-costs", "markdown": "https://wpnews.pro/news/how-we-built-an-ai-saas-on-the-edge-for-nearly-0-in-infrastructure-costs.md", "text": "https://wpnews.pro/news/how-we-built-an-ai-saas-on-the-edge-for-nearly-0-in-infrastructure-costs.txt", "jsonld": "https://wpnews.pro/news/how-we-built-an-ai-saas-on-the-edge-for-nearly-0-in-infrastructure-costs.jsonld"}}