Master Agentic Coding
I recently released my AI Agent Engineer video course on agentic engineering: 10 modules, 55 video lessons, 27 hands-on tasks, and 3.5 hours of video.
You learn harness engineering, testing best practices for AI-written code, and how to build your own AI assistant that works 24/7.
Motivation
Claude Code is the best agentic coding tool today.
Its secret? The harness.
Agent = model + harness
Claude Code’s harness uses 12 patterns that make it the best.
To get the most out of AI agents, you need to understand these patterns. And the best way to learn them is to build them.
So let’s build our own mini version of Claude Code, with the same 12 patterns.
We’ll start with a 20-line Python agent and implement one pattern at a time.
By the end, you’ll understand all 12 patterns and get the full repository with their implementations.
Let’s get started!
The base agent
A coding agent is a loop. The model only reads text and writes text. The harness does everything else:
The harness builds the input: the system prompt, your rules files, memory, the tool list, and the conversation so far.
The model answers with text, or with a tool call.
The harness checks the tool call against the permission rules, runs it, and adds the result to the conversation.
The loop goes back to step 1. It stops when the model answers without a tool call.
The model never opens a file or runs a command. It returns a tool call, and the harness runs it or refuses it.
This is the base of our mini agent: one model call in a loop, and a call_tool function that looks up each tool by name in the TOOLS dictionary and runs it:
The API is stateless. Every call sends the full messages list again.
The 12 Patterns of Claude Code’s Harness
Claude Code adds 12 patterns to this loop. Each pattern is code that runs at one point around the model calls: before a call, after a tool call, or between sessions. The timeline below shows when each one runs.
Part 1: What the agent knows
Problem: The model has no memory between calls. Each call sees only the input that the harness builds in step 1.
Patterns 1 to 5 decide what goes into that input.
1. Persistent Instruction File
A persistent instruction file is your rules file. It holds the build commands, the test command, and the rules for your codebase.
How Claude Code does it: It loads CLAUDE.md from the repo root into every session.
Under the hood: The harness reads the file from disk and puts its text into the system prompt of every model call. An @path line in the file makes the harness load that file too, up to 4 hops deep.
2. Scoped Context Assembly
Scoped context assembly gives each folder its own rules file. The agent loads the payments rules only when it works on payments code.
Pattern 1 puts one file into every call. Pattern 2 loads a folder’s file only after the agent touches a file in that folder.
How Claude Code does it: It loads your user file and the repo file at start. A subfolder’s CLAUDE.md loads the first time the agent reads or edits a file in that subfolder.
Under the hood: At start, the harness walks up from the working folder to the root and loads each CLAUDE.md it finds. After that, it watches the file tool calls. When a call touches packages/api/routes.ts for the first time, the harness checks that folder and its parents for rules files that are not loaded yet and adds them to the input.
💡 This is progressive disclosure: the harness adds context only when the task needs it. Patterns 3 and 9 use the same mechanism for memory notes and tool schemas.
3. Tiered Memory
Tiered memory loads a short index on every call and the full notes only when the agent asks for them. The index has a fixed size, so the memory can grow to hundreds of notes and each call still pays the tokens of the index only.
How Claude Code does it: Its auto memory keeps MEMORY.md as an index with one line per fact. The first 200 lines load at the start of every session, and Claude reads the topic files only when it needs them.
Under the hood: The memory has two tiers. Tier 1 is the index, and it is in the input of every call. Tier 2 is the topic files, and they are not in the input. The model reads the one-line descriptions in the index and opens a topic file with the normal read tool. A 200-line cap keeps the index at a fixed size.
💡 This is how an AI second brain works: an index.md with one line per note, plus one file per topic. Every time you add a note, you add its line to the index. The agent reads the index on every task and opens a note only when the task needs it.
4. Dream Consolidation
Dream consolidation is a cleanup job for memory. It merges duplicates and deletes facts that a newer fact contradicts.
How Claude Code does it: When MEMORY.md nears its limit of 200 lines or 25 KB, Claude Code tells Claude to merge or drop stale entries and move details into topic files.
Under the hood: In our harness, when a session ends and a memory file changed since the last cleanup, the harness starts the cleanup as a background process and returns at once. The cleanup is a separate agent run with its own context. It gets write access to the memory folder only, reads all notes, and rewrites them. Your coding session pays no tokens for it and does not wait for it.
5. Progressive Context Compaction
Progressive context compaction shrinks old history so a long session fits in the context window.
How Claude Code does it: It compacts the conversation when the context window gets full, then loads CLAUDE.md and memory again from disk. You can also use the /compact command and say what to keep.
Under the hood: The harness counts the tokens of the input before each call. When the count gets near the window limit, it sends the old messages to the model with a summary prompt. The summary replaces the old messages, and the last messages stay as they are. Then the harness loads the rules files from disk again, because a summary can drop a rule.
Our harness compacts at 90% of the window and turns old tool outputs into one-line stubs first. They are the largest part of the history, and the agent rarely needs them again.
💡 The summary prompt decides what survives compaction. Ask for the decisions, the file paths, the gotchas found so far and the next step. A generic “summarize the conversation” prompt can leave out important details.
Part 2: How the agent works
Problem: The model works in one long loop by default. It edits the first file that matches the error, and every log it reads stays in its context.
Patterns 6 to 8 change the shape of the loop.
6. Explore-Plan-Act Loop
The explore-plan-act loop takes the edit tools away until a plan is approved.
How Claude Code does it: Plan mode blocks edits. The agent reads files and runs read-only commands, writes a plan, and edits only after you approve it.
Under the hood: Plan mode is a permission mode. The harness refuses every edit tool call and every command that changes files. When the plan is ready, the model calls a tool that shows the plan to you. Your approval switches the permission mode, and the same loop continues with the edit tools allowed.
7. Context-Isolated Subagents
A context-isolated subagent is a helper agent with its own context window and fewer tools. It returns a short report, and its full transcript never reaches the main agent.
How Claude Code does it: Subagents are Markdown files with a tool list. A reviewer gets Read, Grep and Glob, and no Edit.
Under the hood: To start a subagent, the main agent calls a tool with a task description. The harness starts a second loop with a new, empty conversation: the subagent’s system prompt, its tool list, and the task. When the second loop ends, the harness returns only its last message to the main agent, as the result of that tool call.
The @tool decorator adds run_subagent to the TOOLS dictionary, so the model sees it in the tool list next to read_file. When the model answers with a run_subagent tool call, call_tool looks up TOOLS["run_subagent"] and calls it. The string that run_loop returns goes back to the main agent as the tool result.
A 40 KB test log stays in the subagent’s context, and the main agent reads only a 10-line report.
8. Fork-Join Parallelism
Fork-join parallelism splits work into parts, runs one agent per part at the same time, and merges the results.
How Claude Code does it: It runs several subagents in one turn, and a subagent with isolation: worktree gets its own git worktree. A git worktree is a second checkout of the same repo in another folder, on its own branch.
Under the hood: The fork is one model answer with several subagent tool calls. The harness runs them at the same time. The join is the next model call: it gets all the reports together as tool results. With worktree isolation, the harness creates the worktree before the subagent starts and sets it as that subagent’s working folder, so two agents never write to the same files.
💡 Worktrees keep the subagents apart only while they work. At the join, git merges their branches, and two branches that changed the same lines conflict. Give each subagent a different folder, for example one for api/ and one for web/, so their changes do not touch the same lines.
Part 3: What the agent can do
Problem: Every tool schema in the input costs tokens on every call, and more tools raise the chance that the model picks the wrong one. Every shell command the harness runs can change your machine.
Patterns 9 to 11 control which tool calls exist and which ones run.
9. Progressive Tool Expansion
Progressive tool expansion puts only the tool names in the prompt. The full tool description loads when the agent needs that tool.
How Claude Code does it: MCP, the Model Context Protocol, is the standard way to connect an agent to external tools. Claude Code’s MCP tool search keeps MCP tool schemas out of the context until the agent searches for them. Skills work the same way: the description is always loaded, and the body loads when the task matches.
Under the hood: A tool schema is the JSON that describes the tool’s parameters, and the model can call a tool only when its schema is in the input. At start, the harness gives the model the tool names and one search tool. When the model calls the search tool, the harness adds the matching schemas to the input, and the model can call those tools from the next step on.
💡 80 full tool schemas cost tens of thousands of tokens before the first message, and 80 one-line descriptions cost under 2,000.
10. Command Risk Classification
Command risk classification sorts every shell command into allow, ask, or deny before it runs.
How Claude Code does it: It splits a command at &&, ||, ; and |, and checks each part against your permission rules. Deny rules are checked first, then ask rules, then allow rules.
Under the hood: The check runs in step 3 of the loop, after the model asks for the command and before the harness runs it. A deny returns an error text to the model as the tool result. An ask stops the loop and waits for your answer. A command that matches no rule is also an ask.
Check every part: npm test && git push && cat .env starts with an allowed command, and its last part prints your secrets.
11. Single-Purpose Tool Design
Single-purpose tool design gives the agent small typed tools instead of a raw shell for everyday work.
How Claude Code does it: It has Read, Edit, Grep and Glob tools. Bash stays for everything else.
Under the hood: Each tool has a typed schema, so a call with a missing or wrong parameter fails before the tool runs, with an error text the model can fix. The Edit tool takes an exact old string and a new string, and it fails when the old string is not found exactly once in the file. A failed check returns an error text as the tool result.
The model reads the error text as its next input, so the error “'old' matches 3 times. Add more context.” tells it how to retry.
A typed tool also makes permissions simple: a rule can allow edit_file only inside src/. For a sed command, the harness has to parse the shell text to find which files it changes.
Part 4: What always runs
Problem: You can write “run the formatter after every edit” in the prompt, but there is no guarantee that the model calls the formatter after every edit. The model is non-deterministic, so a rule that must run every time needs a deterministic component: code that the harness runs at a fixed point.
Pattern 12 moves the rule out of the prompt and into the harness.
12. Deterministic Lifecycle Hooks
Deterministic lifecycle hooks are code that the harness runs at fixed points: before a tool call, after a tool call, and when the agent wants to finish.
How Claude Code does it: Hooks run on events such as PreToolUse, PostToolUse, and Stop. A hook that exits with code 2 blocks the action, and its error text goes back to the model.
Under the hood: A hook is a shell command in your settings file. At each event, the harness runs the command and sends it the event as JSON: the tool name and its input. Exit code 0 lets the loop continue. Exit code 2 blocks the action, and the harness gives the hook’s error text to the model. A Stop hook that exits with 2 cancels the finish, so the loop runs again.
Recap
A coding agent is a model in a loop, and the harness is responsible for every step of the loop except the model’s answer.
Patterns 1 to 5 decide what the model sees: rules files, memory, and compacted history.
Patterns 6 to 8 decide the shape of the loop: plan before edits, subagents with their own context, and parallel work in worktrees.
Patterns 9 to 11 decide what the model can do: tools that load on search, a permission check on every command, and small typed tools.
Pattern 12 decides what always runs: hooks that the harness calls at fixed events.
If you are interested in the implementation of the mini-harness we just built, check it out: mini-harness on GitHub.
To build these patterns hands-on, join my AI Agent Engineer course: learn to build, steer, verify, and run AI coding agents.
Keep crafting better software.
Daniel




























