AI Coding Agents: Core Mechanisms and a Practical Guide

    This article explains how modern AI coding agents manage context, compress conversation history, persist project rules, enforce deterministic automation through hooks, load skills on demand, and isolate exploratory work through subagents. It also provides practical guidance for reducing information loss and building a reliable agent harness.

    In recent years, AI coding agents have evolved from conversational completion tools into agent harnesses capable of executing complete tasks. A mature Agent Harness must not only call an LLM but also manage context, persist rules, orchestrate tools, and delegate subtasks. I previously built a command-line AI coding assistant from scratch, and that process gave me a systematic understanding of context management, summary compression, tool protocols, skill injection, and subagent delegation. This article distills that experience into five core concepts to help developers build a complete mental framework.

    1. What Is Actually Inside the Context Window

    Many people assume that an agent’s context is simply “what the user said and what the model replied.” That is the biggest misunderstanding about context. In a complete conversation request, the context window typically carries seven types of content:

    1. System instructions: The agent framework’s own operating rules, role definition, and tool definitions. This content is injected by the framework, is invisible to the user, and still consumes tokens.
    2. Conversation history: The multi-turn interaction records between the user and the model.
    3. File contents: Source code snippets that the agent has read or edited.
    4. Command output: Results returned after terminal commands are executed, often large in size.
    5. Rule files: Project-level persistent configuration (described later), injected into every request.
    6. Automatic memory: Learned records such as preferences and conventions saved across sessions.
    7. Loaded skill instructions: The full text of a triggered skill is injected into context.

    The point of understanding these seven categories is that context is a scarce resource, and every category competes for limited window space. When file reads and command output accumulate too much, the truly important conversation information is pushed out or compressed.

    2. When Context Is Full: Two-Stage Compression

    Mainstream agent frameworks generally adopt a two-step strategy: clear first, then compress.

    Step 1: Clear early tool-call output. File read results and command execution logs take up a lot of space and have low timeliness. By round ten, a file read in round three is likely already outdated. The framework prioritizes removing such content from context and keeps only a trace that “the tool was called.”

    Step 2: Summarize and compress the entire conversation history. Specific wording, parameters, and code snippets are condensed into summary descriptions.

    The direct consequence of compression is information loss. A naming convention you emphasized in round three or an architecture decision finalized in round five may be reduced to a single sentence such as “the two sides discussed code style.” This is the real reason an agent “forgets” instructions in long conversations: it is not necessarily a lack of model capability, but the loss of detail during compression.

    The industry has two proven approaches to this problem:

    • Write persistent rules into rule files, not into the conversation. Rule files are fully re-injected into context after every compression, so they are naturally immune to summary loss. Furthermore, you can add a dedicated “compression preservation instructions” section to the rule file, explicitly telling the framework which decisions, constraints, and interface conventions must be preserved verbatim during compression.
    • Actively trigger compression and specify the focus to preserve. Most tools provide a manual compression command that lets the user declare, for example, “focus this compression on API changes.” Active compression is far more controllable than passive compression because you can decide what stays and what is discarded.

    3. Rule Files: Project-Level Persistent Memory

    Rule files are the highest-return mechanism in agent engineering practice. In essence, a rule file is a convention document stored in the project root. Different frameworks have different naming conventions, but the role is the same: the framework injects it into system-level context on every request.

    A high-quality rule file usually contains:

    1. # Project conventions
    2. - Language: Python 3.11, use type annotations
    3. - Testing: all public functions must have unit tests
    4. - Directory structure: src/ for source code, tests/ for tests
    5. # Compression preservation instructions
    6. - Database schema change decisions must be preserved verbatim
    7. - Field naming conventions for external interfaces must not be summarized

    A practical recommendation is to keep rule files short and precise. The more rules you write, the more context every request consumes; too many rules also dilute the model’s attention and can reduce adherence to each rule. Write only the hard constraints where “violating once means rework.”

    4. Hooks: Deterministic Automation Points

    If rule files are “suggesting that the model comply,” Hooks are “enforcing compliance through the framework.” Hooks are mechanisms that trigger user-defined scripts at specific event points in the agent lifecycle, such as before a tool call, after a file write, or when a session ends.

    Typical use cases include:

    • Post-write checks: After the agent modifies a code file, automatically run static checks or formatting tools and feed the results back to the model, forming an immediate correction loop.
    • Dangerous operation interception: Validate before command execution and block high-risk operations such as deletion or overwriting.
    • Audit logs: Record the parameters and results of every tool call for traceability.

    The value of Hooks is that they turn “things the model may not remember to do” into “things the system will always do.” This is a key step toward engineering reliability.

    5. Skills and Subagents: Modular Capabilities and Task Division

    Skills are on-demand instruction packages. Each skill contains a trigger description and a full instruction text. Normally, the framework places only the skill name and description in context; when user intent matches a skill, the full instructions are injected. This design enables modular capabilities: you can write one skill for scenarios such as “database migration” or “API documentation generation” without keeping all instructions permanently in context.

    Subagent delegation solves another problem: pollution of the main conversation context. When a task requires a large amount of exploratory work—for example, searching globally for a pattern in a codebase or analyzing dozens of files in batches—executing everything in the main context can quickly overflow the window with massive intermediate output. The correct approach is to delegate such tasks to a subagent: the subagent completes exploration in an independent context and returns only refined conclusions to the main conversation.

    Used together, these mechanisms form a clear division of responsibilities: rule files manage persistent constraints, Hooks manage deterministic execution, Skills manage on-demand capabilities, and Subagents manage context isolation.

    Conclusion

    Once you understand these five mechanisms, you can quickly identify the design trade-offs of any AI coding tool: how context is organized, how rules are persisted, how capabilities are extended, and how tasks are divided. This knowledge framework can also guide you in building similar tools on top of an alternative LLM backend—the model layer can be replaced, but the engineering methodology of the agent harness is general. I recommend starting from a minimal implementation: first get “rule-file injection + tool-call loop” working, then gradually add Hooks, Skills, and subagents, and experience in practice the real pain points each mechanism solves.