How AI Coding Agents Work: Context Windows, Compression, Rules, Skills, and Subagents
As large language models move into real-world engineering workflows, more developers are using AI coding assistants in the terminal to read, modify, refactor, and maintain code. This article explains the core mechanisms behind these agent harnesses — what actually fills the context window, how context compression works, and how rules files, hooks, skills, and subagents help you build reliable, repeatable agent workflows. Understanding these internals is essential for moving from "occasionally useful" to "consistently reliable."
As large language models move into real-world engineering, more developers are using AI coding assistants in the terminal to read, modify, refactor, and maintain code. These tools are often described as an “agent harness” — an execution framework built around a large language model that manages context, orchestrates tools, and constrains behavior. Getting real value from them requires more than knowing how to write good prompts; you need to understand how they work internally. This article breaks down the mechanics of coding agents through several core concepts, using common industry implementations as a reference.
1. What Actually Sits in the Context Window
A common misconception is that an agent’s context is just “what I said and what it replied.” In reality, conversation history is only one part of the picture. A full session context typically contains seven categories of content:
- Conversation history — the multi-turn exchanges between the user and the model;
- File contents — source files loaded via the agent’s read tools;
- Command output — results from terminal commands, test scripts, and build tools;
- Project-level rules files — persistent project conventions such as coding standards and architectural constraints, injected into every session;
- Cross-session memory — learned preferences and notes the agent carries across sessions;
- Loaded skill instructions — specialized instruction sets injected in full when triggered;
- System instructions — the framework’s own operating rules, tool definitions, and safety constraints.
The key takeaway: context is assembled from multiple sources, not just a chat log. This also explains why where you write an instruction can make a dramatic difference in how well it’s followed.
2. When Context Fills Up: The Two-Stage Compression Strategy
A model’s context window is a finite resource. In long sessions — after reading many files and running many commands — the context inevitably approaches its limit. Mainstream agent frameworks typically handle this in two stages:
Stage one: purge low-value tool output. File contents and command results take up a lot of space but have limited future value, so they are removed first. The model only needs to remember “I read this file and the conclusion was X” — not the full text.
Stage two: summarize the conversation history. Specific wording, numbers, and code snippets are condensed into high-level summaries.
Compression comes at a cost: information loss. A rule emphasized in turn three or an architectural decision discussed in turn five may be reduced to a vague one-line summary. This is the real reason agents “forget” early instructions in long conversations.
3. Solution 1: Put Persistent Rules in a Rules File
The most effective countermeasure is a project-level rules file (the name varies across tools, but the purpose is the same): its contents are re-injected after every context compression, so they survive summarization intact.
Good candidates for the rules file include:
- Coding standards and style constraints (indentation, naming, comment requirements);
- Architectural decisions (e.g., “all database access must go through the unified data access layer”);
- Fixed workflows for building, testing, and deploying;
- Any convention that must persist across turns.
You can also add a “compression instructions” section to the rules file, explicitly telling the agent what must be preserved in full during compression. For example:
# Compression instructions- Preserve all records of API signature changes- Preserve unfinished TODOs and their context- Preserve architectural decisions with both conclusions and rationale
This acts as a whitelist for the compression process and significantly reduces the chance that critical information gets washed away.
4. Solution 2: Control When Compression Happens
Another practical technique is to trigger compression proactively and specify what to keep. For example, before starting a major refactoring, run a compression pass and instruct it to “focus on the design conclusions of this API change.” Compared with waiting for automatic compression when the context overflows, proactive compression gives you control over both timing and scope.
Practical recommendations:
- Split large sessions into smaller ones with clear goals;
- Run a focused compression or summary at the end of each task phase;
- Persist key conclusions into rules files or documentation rather than relying on conversation memory.
5. Hooks: Injecting Automation at Key Points
Beyond context management, mature agent frameworks provide hook mechanisms — scripts or rules that run automatically at specific lifecycle points, such as before a tool call, after a command executes, or when a session ends. Typical uses include:
- Safety checks before write operations, blocking dangerous commands;
- Automatic formatting or static analysis after file modifications;
- Mandatory test runs before code commits.
The value of hooks is turning conventions into enforcement. Instead of relying on the model’s judgment, hooks guarantee consistency — which makes them essential for team collaboration.
6. Skills: On-Demand Capability Packages
The skills mechanism lets you package a complete playbook for a specific type of task into an independent module. It occupies no context until the task matches, at which point the entire package is injected. You might define skill packages for “database migrations,” “performance testing,” or “internationalization,” each containing step-by-step instructions, caveats, and reference templates.
This on-demand loading design balances two concerns: context scarcity — irrelevant instructions don’t sit in the window permanently — and execution quality — when triggered, the model receives complete, structured guidance instead of scattered prompt fragments.
7. Subagents: Delegating Work to Child Agents
On complex tasks, a single-threaded agent risks context explosion. The subagent mechanism allows a main agent to delegate independent subtasks — for example, “scan the entire repository and find all deprecated APIs” — to a separate child agent. The child agent gets its own context window, and when it finishes, it returns only a summary of its conclusions to the main agent.
The benefit: large volumes of intermediate artifacts (file contents, command output) are consumed within the child agent’s context, while the main context receives only distilled results. This saves space and keeps the main thread of work clean.
8. Conclusion
An AI coding assistant is, at its core, a context engineering and task orchestration system built around a large language model. Understanding the seven components of context and the two-stage compression strategy — and making good use of rules files, proactive compression, hooks, skill injection, and subagent delegation — is what takes you from “occasionally useful” to “consistently reliable.” These principles and best practices apply across coding agent products from any vendor, and they’re worth mastering for every developer.
