Why AI-Generated Apps Break in Production (And the Guardrails That Fix Them)
Generating working UI takes minutes. Making auth, database access, and HTTP boundaries resilient takes deliberate guardrails encoded in code.
AI coding assistants optimize for plausible happy paths. Ask an agent for a login screen or a CRUD endpoint and you will get working routes in seconds. Deploy that code to real traffic, and the missing pieces show up immediately.
It is rarely syntax that fails in production. It is the silent assumptions around state, concurrency, and trust boundaries.
1. The missing “boring” security checklist
When you prompt for features quickly, agents tend to skip defensive defaults unless your codebase enforces them structurally:
- Weak or inconsistent password hashing. Defaulting to fast hashes or mismatched cost factors leaves user credentials exposed if a database snapshot leaks.
argon2idwith calibrated memory and iteration parameters should be non-negotiable. - Form endpoints without CSRF validation. State-changing
POST,PUT, andDELETEhandlers need explicit CSRF token verification bound to the session—not left as aTODOcomment. - Unbounded authentication endpoints. Login, password reset, and registration routes without rate limiting invite credential stuffing from day one.
- Permissive cookies and missing headers. Session cookies need
HttpOnly,SameSite, andSecurein production, paired with explicitX-Frame-Options,X-Content-Type-Options, and CSP headers.
2. Guardrails work better when they are code, not wishes
Telling an AI agent “please write secure code” inside a prompt is fragile. Conventions drift after ten turns of context compaction.
What actually holds up is encoding security and architecture as mechanical constraints:
- Compile-time checks over runtime surprises. Typed templates (
askama,templ) and checked SQL (sqlcor explicit typed query modules) fail the build the moment an agent renames a field or breaks a route. - A single convention file (
AGENTS.md). Document exactly where routes, migrations, CSRF middleware, and integration tests live. Agents follow explicit patterns reliably when the repository structure has one obvious way to add a feature. - Tests that verify security boundaries. Your test suite should assert that unauthenticated requests get
401/302, invalid CSRF tokens get403, and rate limits trigger429.
3. Predictability scales with team size—and agent autonomy
Whether a human engineer or an AI assistant modifies your app six months later, predictable structure prevents regressions. Keep auth, migrations, and security middleware wired before writing your first product feature—not after your first incident.