AI Coding Agent Core Mechanisms: Context Management, Rules, Hooks, Skills, and Subagents
This article breaks down five core mechanisms of AI coding agents—context window management, project-level rule files, event hooks, skill modules, and subagent delegation—and explains why a model that performs well early in a session starts forgetting instructions during long tasks. It shows what a typical agent harness loads into context, how two-step compaction causes instruction loss, and how to counter it with layered rule files, deterministic scripts, reusable skills, and scoped parallel execution. Readers will find practical configuration examples and a minimal adoption path, plus evaluation metrics that go beyond generation speed.
Five Core Mechanisms of AI Coding Agents: A Deep Dive
1. Why “Using” an Agent Is Not the Same as Using It Well
Most developers have had the same experience with AI coding tools: the first few turns are precise—the model understands the request, locates the right files, and produces runnable code. As the task goes on, it starts “forgetting” the original agreements, repeats approaches that were already rejected, and even edits unrelated files. It is tempting to conclude that the model has become dumber. From an engineering perspective, though, this is context management at work—a predictable and controllable system behavior.
An AI coding tool is essentially an agent runtime framework (often called an agent harness): the model handles reasoning and generation, while the framework assembles context, constrains behavior, executes tools, and reclaims resources. Mainstream implementations—including common practices in Baidu AI Cloud’s related R&D—generally provide five core capabilities: context window management, project-level persistent rules, event hooks, skill modules, and subagent delegation. Understanding these five layers is the dividing line between using an agent and using it well.
2. What Actually Fills the Context Window
A common misconception is that context equals “what I said plus what it replied.” Conversation history is only one category. A typical framework loads seven kinds of information at once:
- Conversation history: the multi-turn exchange between user and model;
- File contents: source snippets the model has read or edited;
- Command output: results of test, build, and search commands;
- Project-level rules: injected content from persistent repository configuration;
- Automatic memory: preferences and lessons saved across sessions;
- Loaded skills: full instruction text injected once triggered;
- System instructions: the framework’s own runtime rules and tool definitions.
What really crowds the window is file content and command output. A single full-file read or a verbose test log can consume as much space as dozens of turns of conversation.
Two-Step Reclamation When Context Runs Out
Mature frameworks usually free space in two steps:
First, they clear the largest and least time-sensitive content—earlier tool call results and file reads.
Second, they summarize and compress the entire history, condensing exact wording, values, and code snippets into an overview.
Compression inevitably loses information. A coding standard emphasized in turn three or an architectural trade-off settled in turn five may survive only as one vague sentence—which is the real cause of “instruction forgetting” in long tasks.
Engineering Countermeasures
# Project Rules (re-injected after every compaction, unaffected by summaries)## Must Follow- Interface changes must be accompanied by contract test updates- Do not modify the /legacy directory## Compaction Retention InstructionsWhen compacting, always retain: data model field definitions, the list of rejected approaches, performance baseline values
Two practical recommendations. First, constraints that must stay in effect across turns should not live only in the conversation; put them in a project-level rule file (file names differ per tool, so check the respective documentation). Second, trigger compaction yourself and specify the retention focus—for example, “focus compaction on interface changes and the data model”—which is far more controllable than waiting for overflow.
3. Project-Level Rule Files: Turning Conventions into Engineering Assets
The value of a rule file is “write once, stay in effect.” A layered organization is recommended:
- Global layer: personal preferences such as commit message style and comment language;
- Project layer: tech stack versions, directory responsibilities, and paths that must not be touched;
- Subdirectory layer: special constraints for a specific module, injected only when that directory is in play.
The key to writing them is decidability. Change “write good error handling” into “all external calls must set a timeout and wrap a unified exception type,” and the model can execute it reliably. Rule files should be version-controlled and evolve alongside code review so documentation does not drift from the implementation.
4. Event Hooks: Leaving Determinism to Code
Hooks run user scripts before and after tool calls. Common trigger points include validating a command before execution, auto-formatting after a file write, and archiving logs when a session ends.
{"hooks": {"before_command": "scripts/guard.sh","after_write": "scripts/format.sh"}}
Three design principles apply. First, give deterministic tasks to scripts and ambiguous judgment to the model. Second, hooks must be fast and idempotent so they do not slow the main flow. Third, failures must surface explicitly—silent failures push problems into the testing phase. Typical scenarios are enforcing static checks before commits, unifying code style after writes, and requiring confirmation for dangerous commands.
5. Skill Modules and Subagent Delegation
A skill module is a reusable instruction package, usually containing trigger conditions, procedural steps, and reference resources; once its description matches, the whole package is injected into context. Its purpose is to turn team best practices into callable assets instead of re-explaining them every time. Keep one skill focused on one class of problem, and state the applicable and non-applicable boundaries clearly in the description.
Subagent delegation assigns work to execution units with independent context, which suits broad searches, batch file analysis, and parallel verification. The parent agent receives only a summary of conclusions, protecting the main context from being polluted by massive intermediate information. Note that subagents do not share context, so task descriptions must be self-contained; also cap concurrency and per-task scope to avoid resource contention and conflicting results.
Two related patterns are worth pairing with these. ReAct emphasizes a short “reason—act—observe” loop and fits exploratory debugging. Plan-and-Execute produces a plan first and then executes it step by step, which suits large-surface refactors. The former is flexible, the latter is controllable—choose based on task risk.
6. Getting Started
Begin with a minimal viable configuration: write the project rule file first, add two or three high-frequency hooks, then build up three to five skills, and finally introduce subagents for search-heavy and refactoring tasks. When evaluating, look beyond generation speed to rework rate, the number of out-of-scope edits, and context hit rate. Treat these five layers as designed engineering parameters rather than black-box features, and stability on long tasks will improve noticeably.
