AI Coding Agents: A Deep Dive into the 5 Core Mechanisms of the Agent Harness

    This article breaks down the five core mechanisms of an AI coding agent harness: context management, project rule files, event hooks, skill modules, and subagent delegation. It explains what actually fills the context window, why long sessions cause models to "forget" earlier instructions, and how two-stage context compaction drives that information loss. From there, it covers organizing rules across global, project, and directory layers, automating repetitive workflows with hooks, loading instructions on demand through skills, and choosing between ReAct-style and plan-and-execute orchestration. A staged adoption path is included for teams moving from occasional use to a controllable, production-grade setup.

    1. Why There Is a Gap Between “Using” and “Using Well”

    For many developers, the first encounter with an AI coding tool is impressive: describe a requirement, and the tool reads files, edits code, and runs commands. The problems surface quickly once real projects enter the picture. After twenty or thirty turns, conventions that were restated repeatedly get ignored. Start a new session, and the standards you taught yesterday have to be explained all over again. The same “analyze first, then change” workflow gets repeated manually every day.

    These are not model capability problems. They are agent harness problems. The harness is the engineering layer wrapped around the model: it assembles context, defines the available tools, intercepts events, and schedules subtasks. Understanding its five core concepts — context management, project rule files, event hooks, skill modules, and subagent delegation — is the dividing line between using these tools and using them well. This article takes a general technical view and unpacks each mechanism along with its engineering practices.

    2. What Actually Fills the Context Window

    A common misconception is that context equals what the user said plus what the model replied. Conversation history is only one category. A typical context window holds at least seven kinds of information:

    • Conversation history: the multi-turn exchange between user and model;
    • File contents: source snippets that tools have read or edited;
    • Command output: results of terminal commands, often the largest by volume;
    • Project rules: convention files persisted inside the repository;
    • Automatic memory: preferences and lessons saved across sessions;
    • Loaded skills: full instruction text injected after a skill is triggered;
    • System instructions: the harness’s own runtime rules and tool definitions.

    How Context Overflow Is Handled in Two Steps

    As tokens approach the limit, mainstream frameworks reclaim space in two stages:

    1. Evict low-recency, high-volume content first: file read results and command output are removed earliest;
    2. Summarize and compress conversation history: exact wording, numbers, and code snippets are condensed into an overview.

    Compression always loses information. A naming convention stressed in turn three or an architectural decision made in turn five may survive only as one vague sentence — which is the root cause of “model amnesia” in long sessions.

    Three Engineering Countermeasures

    Countermeasure 1: Move long-lived rules out of the conversation flow. Conventions that must hold across turns belong in a project rule file at the repository root (a common industry practice, though naming varies slightly between tools). That file is re-injected after every compaction and is unaffected by summarization.

    1. # Project Conventions
    2. - All API responses must use a unified envelope structure
    3. - Direct database access from the service layer is forbidden
    4. - Formatting and unit tests must pass before committing

    Countermeasure 2: Declare the compaction focus explicitly. When triggering compaction yourself, name the topic — for example, “keep all conclusions related to this database migration” — so the summary has a clear basis for what to keep.

    Countermeasure 3: Control the size of each injection. Read large files by range, and filter command output before it enters the context, so a single read does not crowd out the window.

    3. Project Rule Files: Making Tacit Knowledge Explicit

    Rule files do more than prevent forgetting; they are the contract layer between the team and the agent. A three-tier organization works well:

    • Global tier: personal preferences such as response language and comment style;
    • Project tier: tech stack, directory structure, build and test commands, code red lines;
    • Directory tier: local conventions for specific subdirectories, effective only when the agent enters that directory.

    The key to writing them is to be executable and verifiable. “Code should be elegant” means nothing; “every new exported function must include type annotations and pass static analysis” can actually be followed. Length matters too — rule files themselves consume context, and a bloated spec dilutes the constraints that count.

    4. Event Hooks: Turning Repetitive Workflows into Automation

    Hooks are a mechanism for running external scripts at specific points in the agent lifecycle. Common trigger points include before a tool call, after a tool call, session start, session end, and just before compaction.

    Typical uses:

    • Run formatting and static analysis automatically after a file is written;
    • Intercept high-risk commands and require human confirmation before execution;
    • Automatically summarize the change list when a session ends.
    1. {
    2. "post_tool_use": {
    3. "match": "write_file",
    4. "run": "npm run lint --silent"
    5. }
    6. }

    The design principles for hooks are short, fast, and idempotent. A single run should stay within seconds, or it will noticeably slow down interaction, and it must be repeatable without side effects. For high-risk operations, prefer “block by default, allow explicitly” over after-the-fact remediation.

    5. Skill Modules: From Full Injection to On-Demand Loading

    Skills resolve the tension between instruction volume and trigger timing. The mechanism is simple: a short description stays exposed at all times, and only when the task semantics match are the full instructions, scripts, and reference material loaded into context.

    A skill typically has three parts: metadata (name and trigger description), the main instructions, and optional supporting resources. The design hinges on writing trigger descriptions that state the boundaries of when the skill applies and when it does not. A vague description leads to skills that fail to fire when they should and fire when they should not.

    The division of labor with rule files is straightforward: rule files carry constraints that are “always true,” while skills carry operating manuals that are “true only in specific scenarios.” Mixing the two leaves the context both bloated and inefficient.

    6. Subagent Delegation and Plan Orchestration

    When a task can be split, delegating to a subagent with its own context is the core way to control complexity. A subagent has its own window and returns only conclusions, which keeps intermediate work from polluting the main context.

    Two mainstream orchestration patterns:

    • ReAct style: reason and act in alternation, suited to exploratory tasks with uncertain paths;
    • Plan-and-execute style: produce a complete plan first, then implement it step by step, suited to broad changes that need review.

    In practice, hand read-only work with summarizable results — research, retrieval, batch checks — to subagents, and keep architectural decisions that require global consistency in the main session. Too many subagents is counterproductive: the aggregation cost can exceed the gains from parallelism.

    7. A Practical Adoption Path

    1. Start with the rule file and write down the team’s red lines clearly — this is the fastest payoff;
    2. Add two or three high-frequency hooks to automate formatting, checks, and interception;
    3. Distill recurring workflows into skills;
    4. Introduce subagents and plan-and-execute orchestration for large tasks;
    5. Keep watching context usage and periodically trim rule and skill descriptions.

    Context management, rule files, hooks, skills, and subagents together form a complete controllability system. Once you master them, an AI coding tool stops being a demo and becomes production infrastructure — and can safely connect to cloud capabilities such as container platforms, log services, and monitoring and alerting, forming a development process that keeps evolving.