Six-Layer Architecture and Context Governance for AI Coding Agents
This article explains why AI coding agents often become harder to use over time and argues that the root cause is system architecture rather than prompt quality. It introduces a six-layer model, five governance surfaces, and practical guidance for skills, hooks, subagents, prompt caching, and project rule files.
Why “It Gets Messier the More You Use It” Is Not a Prompt Problem
Many developers initially treat AI coding tools as chat assistants. After a while, they often run into a familiar set of symptoms: the conversation context becomes increasingly noisy, more tools become available but the model still chooses the wrong one, and rule files keep growing while the model follows them less reliably.
After spending a lot of time adjusting prompts, many people reach the wrong conclusion: the prompt must be poorly written.
The real reason is different. The behavior of these systems is determined by architecture, not by a single prompt. To fix the problem, you need to understand the architecture first.
1. The Agent Loop: What It Actually Does
The core of an AI coding tool is not “answering.” It is a continuously running agent loop:
Collect context → Take action → Verify result → [Done or back to collect]↑ │└────────────────────────────────────┘
This loop is supported by infrastructure such as context, hooks and permissions, skills, tools, and memory.
In practice, the bottleneck is rarely that the model is not smart enough. It is usually one of two things:
- The agent is given the wrong context.
- The agent produces a result but cannot tell whether it is correct or how to roll it back.
Therefore, optimization should shift from “tuning the model” to “tuning the system.”
2. The Six-Layer Architecture: A Troubleshooting Map
Breaking this kind of system into six layers makes problems much clearer:
| Layer | Responsibility | Question It Answers |
|---|---|---|
| Project rules / memory | Long-term context | “What is it?” |
| Tools / external integrations | Action capability | “What can it do?” |
| Skills | On-demand methodology | “How should it be done?” |
| Hooks | Mandatory enforcement, not dependent on model discipline | “What must happen?” |
| Subagents | Isolated-context workers | “Who does it?” |
| Verifiers | Verifiable, rollbackable, auditable | “How do we know it is done?” |
Strengthening only one layer will inevitably create imbalance:
- If rule files become too long, they pollute the agent’s own context.
- If too many tools are added, tool selection becomes ambiguous.
- If subagents are spawned everywhere, state drift becomes likely.
- If verification is skipped, failures become impossible to locate.
3. Five Key Surfaces and How to Govern Them
You can also locate problems through five “surfaces”:
Context surface: Which information stays resident, and which is loaded on demand. The carriers are rule files, memory, and skills. The governance principle is to minimize resident context. Stable project conventions belong in rule files, while volatile details should be loaded on demand.
Action surface: Which capabilities are currently available. This is made up of built-in tools, external integrations, and plugins. More tools are not always better. Tools should be grouped by task domain to reduce ambiguity.
Control surface: Which actions must be constrained, blocked, or audited. This is implemented through permissions, sandboxes, and hooks. Any constraint that cannot rely on model discipline should be pushed down to the hook layer and enforced there.
Isolation surface: Which tasks need independent context and permissions. This can be implemented with subagents, working copies, or forked sessions to prevent long-running tasks from contaminating each other.
Verification surface: How to determine whether a task is truly complete. This depends on tests, static checks, screenshots, logs, and continuous integration. Without a verification loop, autonomy becomes uncontrolled behavior.
4. Designing Skills, Hooks, and Subagents Correctly
Skills should be on-demand methodology, not long documents stuffed into the context all at once. Reusable operational procedures should be packaged as skills and injected only when needed. This significantly reduces context noise.
Hooks are valuable because they do not depend on model judgment. For example, the following should be implemented as hooks:
- Enforcing formatting before commits.
- Blocking dangerous commands.
- Running tests automatically after changes to critical paths.
The rule is simple: if something must happen, do not rely on the model remembering it. Use a hook as a safeguard.
Subagents are for controlled autonomy and context isolation. They are suitable for broad retrieval tasks or independent subtasks, and they should return conclusions rather than the entire process. However, more subagents are not always better. Overusing them can cause state drift and make merging difficult.
5. The Architectural Impact of Prompt Caching
Prompt caching reduces latency and cost by reusing stable prefixes. However, it also imposes a reverse constraint on architecture: stable content must be placed earlier, and volatile content must be placed later.
This confirms the governance principle of the context surface. Rule files should remain concise and stable, while dynamic information should be placed later. Only then can cache hit rates be maximized.
In other words, caching mechanisms reward systems with well-organized context.
6. How to Write a Useful Project Rule File
A truly useful rule file should follow three principles:
Concise: Include only stable and frequently used conventions. Avoid self-pollution.
Executable: Anything that must be done should be handled by hooks, not by textual reminders.
Layered: Long-term conventions belong in rule files, stage-specific details belong in memory, and procedural methods belong in skills.
When each layer has a clear role, the system will not degrade into a mess.
Conclusion
The experience bottleneck of AI coding tools is essentially an architecture and context governance problem.
If you break the system into six layers and troubleshoot it through the five surfaces, you will find that most “uncooperative” behavior can be located and fixed.
The real next step is not writing longer prompts. It is designing a system with clean context, clear actions, enforced constraints, reasonable isolation, and a complete verification loop.
