Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

Running a free AI app without going broke: dollars per call, a daily budget and per-IP limits

Mister Blocks is free, needs no account, and every build is a few minutes of Claude Opus thinking about bricks. That's a couple of dollars a build. Put that on the open internet and one bored person with a script can spe

Running a free AI app without going broke: dollars per call, a daily budget and per-IP limits

Mister Blocks is free, needs no account, and every build is a few minutes of Claude Opus thinking about bricks. That's a couple of dollars a build. Put that on the open internet and one bored person with a script can spend your month's budget before lunch.

Here is the plumbing that keeps it free and affordable. None of it is clever, but all of it was needed.

Amazon delivery van as a white and blue brick model on a wooden table
Amazon Delivery Van: 102 pieces, 11 steps. More LEGO trucks.

1. Log every call, in dollars

Every Claude call goes through one function, and every call writes one row to MongoDB: which run, which step, the model, input and output tokens, cache reads and writes, the cost in USD, and how many seconds it took.

const PRICE = { input: 4, output: 20, cacheRead: 0.2, cacheWrite: 5 };   // USD per million tokens
const usd = (u.input_tokens * PRICE.input + u.output_tokens * PRICE.output
  + (u.cache_read_input_tokens || 0) * PRICE.cacheRead
  + (u.cache_creation_input_tokens || 0) * PRICE.cacheWrite) / 1e6;

Two details that matter:

  • The row is written before checking whether the call was refused or cut off. A failed call still costs money, so it still counts.
  • A database error is logged and ignored. Accounting must never be the reason a build fails.

A small admin endpoint shows tokens and dollars for one run, for today and for all time. That's how I know a build costs "a couple of dollars" rather than guessing.

2. A site-wide daily budget, checked first

With every call in dollars, a budget is one query: sum usd since midnight UTC. Before any expensive action, that's checked against DAILY_BUDGET_USD (25 by default). If it's spent, the site says so plainly ("Mister Blocks has used up today's building budget. Please come back tomorrow.") and the 3D guides keep working, since they cost nothing.

3. Per-visitor limits, without accounts

Then a per-visitor counter: one Mongo document per kind|ip|date, incremented with an upsert. The limits:

Action Per IP per day
New build from a photo 5
New build from an idea 4
Chat messages 40

Each limit has its own human message ("You can start 5 new builds a day. Try again tomorrow, or keep chatting in a build you already started."), returned with HTTP 429.

Coco 2 delivery robot as a pink and white brick robot on wheels
Coco 2 delivery robot: 61 pieces, 10 steps. More robot builds.

4. Knowing the visitor's real IP

Per-IP limits only work if you know the IP. The site runs on Vercel, but misterblocks.com is served through a Cloudflare Worker that forwards to Vercel (the domain's DNS lives in a Cloudflare zone, and a Worker custom domain was the cleanest way in).

That means Vercel sees Cloudflare's IP, not the visitor's. So the Worker adds two headers: the visitor's IP from cf-connecting-ip, and a shared secret. The site trusts the IP header only when the secret matches; otherwise it falls back to x-forwarded-for. Without the secret check, anyone could send a fake IP header and get unlimited builds.

The same Worker redirects www to the bare domain and rewrites any redirect that names the Vercel address. Old *.vercel.app names get a permanent redirect to misterblocks.com, except for requests that came from the Worker itself, which would otherwise loop.

5. Spend tokens where they matter

Not every call needs the same effort:

Call Effort Max tokens
Reading the photo of your pieces high 64,000
Designing a model medium 24,000 (photo) / 64,000 (idea)
Fixing reported problems medium 32,000
Translating a build medium 32,000
Tagging categories for SEO low 2,000

Recognising pieces in a messy photo is where mistakes are most expensive, so it gets the most thinking. Picking two category tags gets almost none.

Other habits that keep the bill down:

  • Structured output (a JSON schema on every call), so there's no retry because the answer didn't parse.
  • Prompt caching on the long, fixed system prompt of the idea designer.
  • Patches, not rewrites. When the stud checker finds problems, the model answers with a patch (remove these pieces, add these) instead of the whole model again.
  • A deadline. The photo designer gets up to three rounds, but a new round only starts if it's still under 170 seconds.
  • Server-side fallback: if the model declines, the API falls back to another model instead of failing the whole build.

Pancake chef robot as a white brick robot at a stove
Pancake Chef Robot: 142 pieces, 20 steps.

6. Long jobs without long requests

An idea build can take ten minutes. The request that starts it answers right away; the work continues with waitUntil and writes its progress onto the build document (throttled to one write per 1.2 seconds). The page polls a tiny, uncached endpoint every 2 seconds. If a job stops reporting for 150 seconds, it's shown as failed with a retry button, rather than a progress bar that never moves.

What it costs to look

The 3D guides, the printable booklets and the PDFs cost nothing per visit: the models are stored, the pictures are drawn in your browser, and the files sit in Cloudflare R2 with a one-year cache. Only making something new costs money. That's why the limits only apply there.

Browse the free guides as much as you like: robots, trucks, easy builds, or LEGO build ideas. And when you want your own, snap your bricks at misterblocks.com, which works like a LEGO scanner app with no install.

Hover courier drone as a brick quadcopter on a landing pad

Unofficial fan builds. LEGO® is a trademark of the LEGO Group, which does not sponsor or endorse this site.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.