← Back to Guides

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. argon2id with calibrated memory and iteration parameters should be non-negotiable.
  • Form endpoints without CSRF validation. State-changing POST, PUT, and DELETE handlers need explicit CSRF token verification bound to the session—not left as a TODO comment.
  • 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, and Secure in production, paired with explicit X-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:

  1. Compile-time checks over runtime surprises. Typed templates (askama, templ) and checked SQL (sqlc or explicit typed query modules) fail the build the moment an agent renames a field or breaks a route.
  2. 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.
  3. Tests that verify security boundaries. Your test suite should assert that unauthenticated requests get 401/302, invalid CSRF tokens get 403, and rate limits trigger 429.

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.