I Let the Model Write the Page Spec, Then Built Everything Around Its Mistakes
Adding Claude to the Riskiest Page Builder meant writing the guardrails first: a kill switch, quotas, strict validation, and a renderer that only draws plain text.
Learn · Oct 26, 2026
I'm building a page builder that asks one question first: what's the riskiest page on your site? This week the model finally got to write something.
That's the moment I get nervous. Up to now the builder saved your answers and analyzed what you pasted in. This step asks Claude to produce a structured spec for a whole page, and then draws a preview from it.
So I didn't start with the prompt. I started with what happens when the model gets it wrong, gets expensive, or gets tricked.
Make the route earn its trust
The new route is POST /api/rp/spec. Before it ever calls a model, it checks four things. First, the session has to be cookie-owned. If the cookie doesn't match, nothing runs. Second, a kill switch. One switch turns the whole route off if something looks wrong. Third, quotas. Every call costs real money, so there's a cap per visitor. I think of it as a front door with four locks. A visitor who passes all four gets the model. A visitor who fails one never costs me a cent. None of that is exciting. All of it is what lets me leave the route public and sleep. I wrote these checks first and the model call last, on purpose.
Define the spec before asking for one
The model doesn't return a web page. It returns a PageSpec, a strict structure with fixed fields and fixed shapes. I wrote the validation for that structure before I wrote the prompt that produces it. That order matters. If you write the prompt first, you start bending the validator to fit whatever the model happens to say. If you write the validator first, the model has to fit you. Validation is strict. Anything that doesn't match gets rejected, not patched up. I'm tempted every time to quietly fix a near-miss. Trim the long string. Fill the missing field with a guess. But a guess dressed up as model output is still a guess. Rejecting it keeps the line between what the model said and what I assumed.
One retry, then a template
Models miss sometimes. A field comes back empty, or the shape is slightly off. So the route gets one retry. Just one. If the second attempt fails validation too, the visitor still gets something. A template fallback fills in a sensible default spec. Nobody lands on an error screen in the middle of the flow. Here's the thing about retries. Without a limit, a retry is a loop that spends money. With a limit of one, it's a small insurance policy. The fallback has a second job too. It means I can test the whole flow, preview and all, without a single model call. That made building the renderer much faster, because I never waited on a response to see what the page looked like.
Draw only what passed validation
The preview is where a model's output turns into something a person looks at. That's the dangerous handoff. My mockup renderer is fixed. It has a set layout, and it draws only validated spec content, as plain text. It never renders markup from the model. It never runs anything the model wrote. If a field contains something strange, the strange thing shows up as harmless words on the screen. The interactive part is the part visitors notice. The plain-text rule is the part that keeps them safe. There's a cost. The preview looks plainer than it could. I'm fine with that for now. A mockup that can't be hijacked beats a pretty one that can, and I can add richer styling later from my own fixed templates instead of from model output.
Log every generation
Each spec generation writes an audit record. The record type is rp_spec. When someone asks why a preview looks the way it does, or why the route got expensive, I have a record to read. The log table was already there from the data layer I built first, so the new route just had a place to write.
What broke: The same project taught me this lesson a layer earlier. When a Claude call failed in the analyze route, my audit log only said "model call failed." That's useless. I couldn't tell a bad key from a rate limit from a malformed request. I was debugging blind. That's the quiet cost of a vague error. The failure itself was small. The hour I spent guessing was not. A log that says only that something failed is a log that makes you reproduce the failure just to learn what it was.
The fix: Failed calls now store a short detail: the error class, the HTTP status, and the start of the message, with the key redacted. I also dropped the temperature setting from the call to match the rest of the app. The spec route inherited that fix, so when it fails now, I can read why. If you're adding a model to anything public, steal that habit. Write the failure record before you need it. You'll need it on the worst day, when you have the least patience for guessing. Next up is the step where the preview becomes something a visitor can export. The same rule applies: nothing leaves the app unless it passed validation first.
I'm adding a route that calls an LLM and returns structured output. Build the guardrails BEFORE the model call.
1. Ownership: require a session cookie that owns the record. Reject anything else.
2. Kill switch: read an env flag at request time. If it's off, return a clear "temporarily unavailable" response without calling the model.
3. Quotas: cap calls per IP and per session per day. Return a friendly limit message.
4. Untrusted input: wrap any user-supplied text in clearly labeled delimiters and tell the model to treat it as data, not instructions.
5. Schema: define a strict Zod schema for the output first. Reject unknown fields.
6. Retry: if validation fails, retry exactly once. If it fails again, return a safe template fallback.
7. Rendering: draw only validated fields, as plain text. Never render model-written markup.
8. Audit: log every call with a type, outcome, and a short redacted error detail on failure (error class, status, message prefix). Never log keys.
Write the validation and tests first. Then the route. Then the model call last.Go deeper
A Text Editor Is a Security Decision →
Autosave, a Mark Done button that never publishes, and an allowlist that decides what gets stored. Building Switchpath's project workspace, step 7.
I Ran a Security Audit on My Own Vibe-Coded App and Found the Scary Part Isn't What You'd Expect →
Two chat routes with no rate limiting, pointed straight at a shared API key, and why that's a money problem, not a data-theft problem.
Get new guides and builds in your inbox
No noise. New artifacts, skills, and lessons when they drop — and early access when agents launch.
Want a full structured path instead of one-off guides?
That's Learn AI with AI — a sequenced course that takes you from zero to building real things, with community and accountability built in.
Check out Learn AI with AI →