Architecting for AI Agents: How to Prevent Frontend Codebases from Collapsing
Architecting for AI Agents: How to Prevent Frontend Codebases from Collapsing
AI coding agents can generate 1,500 lines of frontend code in under thirty seconds. Features that once took sprint cycles now land in review queues before your morning coffee cools.
However, frontend engineering teams are hitting a wall. The bottleneck is no longer code production; it is architectural coherence and code review capacity.
When developers prompt AI tools to build complex features in a single pass, the output is almost always a monolithic pull request. These massive PRs blend state management, API integration, styling, and business logic into a brittle block of code. Human reviewers, overwhelmed by diff size, give cursory approvals. Within three months, the architecture degrades, duplicate utilities sprout everywhere, and bundle sizes explode.
To survive in an agent-heavy environment, frontend architects must evolve their system designs. Here is how we scale frontend architecture when machine velocity meets human code review limits.
The Core Problem: Context Blindness at High Velocity
AI agents excel at localized problem-solving. If you ask an agent to build a data table with filtering and sorting, it will give you a working data table.
What the agent will not do is inspect your team's unwritten architectural conventions. It will not notice that you prefer signals over global state for local UI tables, nor will it check if a custom hook for pagination already exists in a sibling directory.
Instead, it invents new patterns. Multiply this behavior across ten engineers running multiple agent sessions daily, and your frontend architecture fractures into fragmented coding styles.
To fix this, we need two structural changes:
- Decomposed delivery pipelines (stacked workflows).
- Machine-enforceable component boundaries.
Strategy 1: Enforce Stacked PRs for Agent Workflows
Instead of letting an agent emit a single massive PR containing types, hooks, UI, and tests, force the workflow into stacked, single-responsibility layers.
Stacked pull requests are not just a git hygiene preference; they are an architectural containment strategy. When an agent decomposes work into a clean, ordered stack, human reviewers can inspect logic sequentially rather than drowning in a 20-file diff.
The Standard 4-Layer Frontend Stack
-
Layer 1: Schema & Type Definitions
Contains: OpenAPI/GraphQL codegen updates, Zod validation schemas, domain interfaces.
Review focus: Is the data contract correct and normalized? -
Layer 2: Headless State & Domain Hooks
Contains: Custom hooks, state machines, API integration. Zero JSX allowed.
Review focus: Is business logic decoupled from presentation? Are side effects properly handled? -
Layer 3: Presentational Components
Contains: UI components consuming the Layer 2 hooks and Design System tokens.
Review focus: Accessibility, design token usage, visual responsiveness. -
Layer 4: Integration & E2E Tests
Contains: Route assembly, analytics wiring, Playwright/Cypress coverage.
Review focus: User flow completeness and error boundaries.
When agents build sequentially in stacked sessions, reviewing a 50-line type layer or a 120-line headless hook takes two minutes. If Layer 2 has an architectural flaw, you catch it before the UI layer is even generated.
Strategy 2: Implement Hard Micro-Boundaries
AI agents follow path-of-least-resistance imports. If an agent needs a utility function buried inside a feature module three directories away, it will write a relative import across domain boundaries without hesitation. Over time, your dependency graph becomes an entangled web.
To stop this, replace folder conventions with hard module boundaries enforced by AST linting or tooling like Dependency Cruiser.
Defining Clean Boundaries
Consider this standard module structure:
src/
├── features/
│ ├── checkout/
│ │ ├── public-api.ts
│ │ └── internal/
│ └── user-profile/
│ └── public-api.ts
└── shared/
└── ui/
Enforce strict import rules:
- Features may only import from another feature's
public-api.tsfile. - Features can never import from another feature's
internal/directory. - Shared UI components cannot import from feature modules.
Here is an example ESLint boundary configuration using eslint-plugin-import:
{
"rules": {
"import/no-restricted-paths": [
"error",
{
"zones": [
{
"target": "./src/features/checkout",
"from": "./src/features/user-profile/internal",
"message": "Cross-feature internal access prohibited. Use public-api.ts instead."
},
{
"target": "./src/shared",
"from": "./src/features",
"message": "Shared utilities cannot depend on domain features."
}
]
}
]
}
}
When an AI agent attempts to shortcut an architectural layer, the build fails locally before the PR is created.
Strategy 3: Machine-Readable Architectural Rules
If a rule lives only in a Confluence doc or team wiki, an AI agent will ignore it. To guide automated tools, convert your design standards into machine-readable formats.
Custom ESLint Rules for Component Design
If your architecture prohibits direct fetch or axios calls inside UI components, write a linter rule that blocks network primitives inside files located under components/.
// custom-rules/no-direct-fetch-in-ui.js
module.exports = {
meta: {
type: 'problem',
docs: { description: 'Disallow network calls inside presentation UI components.' },
},
create(context) {
return {
CallExpression(node) {
const isFetch = node.callee.name === 'fetch';
const isAxios = node.callee.object && node.callee.object.name === 'axios';
if (isFetch || isAxios) {
context.report({
node,
message: 'Direct API calls inside UI components are prohibited. Extract logic to a headless domain hook.',
});
}
},
};
},
};
When your linter enforces component separation, AI agents self-correct during their execution loop before a human ever looks at the code.
Strategy 4: Automated Architectural Budgets
Reviews should focus on user experience, system ergonomics, and business rules. Reviewers should not spend time pointing out bundle sizes, missing test cases, or missing design tokens.
Set up automated pull request checks that run in CI:
- Bundle Size Budgets: Set strict limits on route-level JS entry points. If an agent pulls in a massive 80KB utility library for simple date manipulation, the build breaks automatically.
- DOM Depth & Component Complexity: Use static analysis to flag components with cyclomatic complexity exceeding acceptable thresholds.
- Design System Audits: Run static analysis checks to catch hardcoded hex colors or arbitrary pixel margins (
margin: 13px). Force agents to use established design tokens.
Key Takeaways
- AI velocity accelerates technical debt: Rapid generation of unstructured code leads to PR review exhaustion and architectural fragmentation.
- Stacked PRs are essential: Break agent outputs into distinct architectural layers (Types, Logic, UI, Integration) to make reviews manageable and maintainable.
- Boundaries must be enforced programmatically: Use AST linters, path restrictions, and public APIs to prevent cross-domain module coupling.
- Convert docs to code rules: Architectural guidelines must exist as machine-readable linter rules or static analysis constraints so agents self-correct automatically.
What You Should Do Today
- Audit your last 5 large PRs: Identify whether pull requests are combining data contracts, business hooks, and UI layouts into single commits.
- Set up module boundary linting: Install
eslint-plugin-importordependency-cruiserto block cross-domain internal imports. - Establish stack conventions: Teach your team how to prompt agents to break features down into layered steps rather than requesting entire features in one prompt.
More TechSheets
Beyond Components: Architecting Resilient Frontend Systems for Scale
Stop building components and start designing systems. An architect's guide to scaling frontend applications through domain-driven design, state layering, and strategic decoupling.
Scaling Beyond the Component: A Senior Architect's Guide to Robust Frontend Systems
Master the complexities of frontend architecture. Explore micro-frontends, state management patterns, and strategic technical leadership for building scalable web applications.
Architecting Frontend Systems for AI Coding Agents: Stacks, Boundaries, and Reviewability
Learn how to structure frontend architectures to safely absorb AI code velocity using vertical slicing, stacked PRs, and strict contract boundaries.