A rigid architectural pattern that OpenAI’s Codex team uses to keep an agent-generated codebase coherent at scale. Each business domain is divided into a fixed set of layers with mechanically enforced dependency directions. Cross-cutting concerns enter through a single explicit interface.
Source: Harness Engineering at OpenAI.
Key
Coding agents are most effective in environments with strict boundaries and predictable structure.
The architecture you would normally postpone until you have hundreds of engineers becomes an early prerequisite when agents are doing the writing. Constraints are what allow speed without decay.
The pattern
Within each business domain (e.g. App Settings, Billing, Onboarding), code can only depend “forward” through this fixed sequence:
Types -> Config -> Repo -> Service -> Runtime -> UI
Cross-cutting concerns (auth, connectors, telemetry, feature flags) enter through a single explicit interface called Providers. Anything not on these edges is disallowed and rejected by custom linters and structural tests.
┌─── business domain ────────────────────┐
│ │
Types ─► Config ─► Repo │
│ │ │
│ ▼ │
Providers ─────►│ Service ─► Runtime ─► UI │
(auth, telemetry,│ │
feature flags) │ │
└────────────────────────────────────────┘
│
App Wiring + UI
│
Utils
(outside the boundary)
What each layer holds
| Layer | Responsibility |
|---|---|
| Types | Shape definitions; the lingua franca of the domain. No behaviour. |
| Config | Settings, feature flag defaults, env-var schemas. Read-only at runtime. |
| Repo | Persistence access. The only layer that talks to the database. |
| Service | Business logic. Orchestrates Repo calls, applies rules, returns plain values. |
| Runtime | Long-running processes, schedulers, workers, queue consumers. |
| UI | Presentation. Reads from Service via Runtime, never directly from Repo. |
| Providers | The single explicit injection point for cross-cutting concerns (auth, telemetry, feature flags, connectors). |
Allowed edges
- Forward only: Types -> Config -> Repo -> Service -> Runtime -> UI.
- Providers feeds into the domain at well-defined points (typically Service and Runtime).
- Utils sits outside the domain and feeds into Providers.
- No back-edges. No cross-domain edges except through explicit ports.
Why this works for agent-generated code
Three properties make this pattern unusually well-suited to agents:
1. Predictability collapses the search space
The agent does not have to reason about where something should go. The layer dictates the location. Generating new code becomes “given task X in domain Y, what is the minimum diff across these six layers?” rather than “where in this 1M-line codebase should this live?”
This compounds over time: every new feature reuses the same skeleton, so the agent’s own training-time priors and the repo’s actual layout converge.
2. Mechanical enforcement removes a class of mistakes
Custom linters and structural tests reject any disallowed edge. The agent cannot accidentally create a UI -> Repo shortcut even if it would “work”. The lint catches it before the PR opens, the agent reads the lint message (which carries remediation instructions, see Agent-Computer Interface (ACI)), and either rewrites or escalates.
This converts an entire class of “the agent did something subtly wrong” into “the agent saw a clear error and fixed it.”
3. Verifiability supports auto-merge
Because the rules are mechanical, a passing CI implies the structural invariants hold. Auto-merge becomes safe for routine changes. Human review concentrates on the things only humans can judge (product correctness, taste, edge cases) instead of structural compliance.
This is what makes the Harness Engineering at OpenAI throughput numbers achievable. Without the architecture, every PR would need a human structural review.
Pairing with golden principles
This pattern is one specific manifestation of Golden Principles and Garbage Collection. The layer rules are golden principles. The structural lints are GC. Recurring agents detect drift (a service quietly importing a Repo from another domain) and open refactor PRs.
The two patterns are not separable in OpenAI’s setup. Either alone is weaker.
Where this differs from “clean architecture” / hex / onion
These patterns predate AI agents. They share the same intuition (boundaries, dependency direction). What is new:
- Mechanical enforcement is the headline, not a footnote. Most clean-architecture write-ups treat enforcement as a discipline question. For agent-generated code, enforcement must be automatic; discipline does not scale.
- The layer count is small and fixed. Six named layers, not “as many as you need”. Smaller surface area = less for the agent to misuse.
- Cross-cutting through a single Providers interface. Many architectures handle cross-cutting concerns ad-hoc. Here it is exactly one named edge.
- Custom lint messages target the agent. Standard lint output is for humans. Here, the message is written so the agent can fix the issue from the message alone.
When to use this
This pattern is heavy. It is the right call when:
- An AI agent will write or modify a meaningful share of the code.
- Multiple agents may run on the same repo (Codex + Aardvark, Claude Code + Cursor, etc).
- The codebase is expected to grow past the size where a single human can hold it.
- Compliance, audit, or reliability constraints make structural drift expensive (payments, banking, healthcare).
It is overkill when:
- The codebase is a small script or prototype.
- Throughput is low and humans review every PR carefully.
- The product domain is too unstable for any architectural commitment yet.
Worked example
Adding a new feature “users can pin frequently-used dashboards” in a system using this pattern:
- Types: add
PinnedDashboardId,PinnedDashboardtypes. - Config: add a feature flag
pinned_dashboards.enabled. - Repo: add
pinDashboardForUser,getPinnedDashboardsForUser,unpinDashboard. - Service: add business rules (max pins per user, ordering rules) calling Repo.
- Runtime: nothing new unless background processing is needed.
- UI: add a Pin button, pinned-dashboards sidebar, calls Service via the existing edge.
- Providers: nothing new (auth and telemetry already wired).
The agent generates each layer’s diff in one PR or in a sequence. A linter validates each layer’s imports. Tests verify the new edges follow the allowed direction. The change is reviewable in minutes.
Compare with an unstructured codebase where the agent must guess where each piece lives, and the reviewer must check every import for direction violations.
Common failure modes
- Layer ambiguity. “Is this Service or Runtime?” If the boundary is unclear, the agent picks inconsistently. Document the layer definitions explicitly in
ARCHITECTURE.mdand let lints enforce. - Providers becomes a junk drawer. Anything cross-cutting goes through Providers, so Providers grows. Sub-divide once it is unwieldy (“AuthProvider”, “TelemetryProvider”, “FeatureFlagProvider”).
- Domain coupling sneaks through Providers. If domain A reaches domain B through “shared” providers, you have lost domain isolation. Lint for cross-domain types in Provider interfaces.
- The pattern is applied without enforcement. Without lints, this is just convention, and conventions decay. Either ship the lints or do not bother.
Application to payments / regulated systems
- Audit-friendly. The layer definitions and dependency rules become part of the documented architecture. Auditors love mechanical rules.
- Aligns with PCI-DSS scope reasoning. A clean Service / Repo split makes “what touches cardholder data” mechanically verifiable.
- Domain isolation is already required. Payment flows must not bleed into reporting must not bleed into onboarding. Layered domains formalise what is already policy.
- Compatible with strict merge gates. This pattern is more useful when humans review every PR, because structural review becomes mechanical and humans can focus on correctness and risk.
Related Concepts
- Harness Engineering at OpenAI — the case study where this pattern was named
- Golden Principles and Garbage Collection — the maintenance pattern this enforces
- Agent-Computer Interface (ACI) — lint messages with remediation as a special case
- Agent Legibility — clear architecture is one form of legibility
- Agent-Friendly Codebase — Marble Madness; the architecture is the level
- Spec-Driven Development — specs define what each domain should do; architecture defines where it goes
- Writing Specs for AI Coding Agents — modular prompts map cleanly onto modular layers
- AGENTS.md Six Core Areas — the architectural rules belong in AGENTS.md (or its referenced ARCHITECTURE.md)
Open Questions
- Is the specific 6-layer choice (Types -> Config -> Repo -> Service -> Runtime -> UI) optimal, or just one shape that works? What is the right number?
- How does this scale across many domains? OpenAI’s writeup suggests it works for their internal product but does not say how many domains they have.
- Does the pattern survive when the domain itself is refactored? Layer-internal refactors are easy; cross-layer ones are still hard.
- Can this be retrofitted into an existing unstructured codebase, or only applied greenfield?