LESSON 13TRACK 04 · Agentic AI, Model Context Protocol (MCP) & GovernanceFoundations → Architecture16 min read

AI Agent Harness Engineering: What It Is, How It Works & How to Master Context and Code Quality

A complete visual guide—from beginner analogies to advanced multi-agent patterns from Anthropic and OpenAI—on building, managing, and optimizing your AI harness (`Agent = Model + Harness`).

KEY ARCHITECTURAL TAKEAWAYS
  • An AI Agent is not just a language model: `Agent = Model (Brain) + Harness (Eyes, Hands, Memory, Sensors & Brakes)`. Upgrading the harness yields a larger quality leap than swapping models.
  • Treat the context window as working RAM (a desk), not a hard drive: once context fills past 55–60%, "Context Rot" and "Lost in the Middle" degrade code quality and multiply token costs.
  • Keep root instruction files (`AGENTS.md`, `CLAUDE.md`) under 60 lines as a routing map (Progressive Disclosure), offload heavy exploration to isolated Subagents, and enforce a Default-FAIL verification loop.
  • You can bootstrap a complete, stack-tailored harness in 30 seconds using our open-source `RolePlayingTech/agent-harness-kit` repository and its interactive `harness-architect` skill.
SYSTEM DIAGNOSIS

The "Brain in a Jar" Trap & Context Window Bankruptcy

Most people start working with AI by pasting code or text into a chat window, or by dumping a 1,500-line instruction file into an IDE agent and running a single marathon chat for three hours. After 40 minutes, the AI starts forgetting project rules, inventing non-existent functions, breaking working features, and burning through token budgets. The model did not suddenly become dumb—its harness was either missing or choked with context pollution.

Anti-Pattern (Disconnected)

Raw chat copy-pasting OR a bloated 1,500-line CLAUDE.md/AGENTS.md + one endless chat session (>70% context full) + zero automated test verification.

Connected Pattern (MUMRO)

Lean Map (<60-line AGENTS.md) + On-Demand Skills (SKILL.md) + Scout Subagents (Isolated Context) + Deterministic Test Loop + Fresh-Context Evaluator.

LEVEL 1 · FOUNDATIONS (PLAIN ENGLISH)

1. What Is an AI Harness? The "Brain in a Locked Room" vs. "Equipped Engineer" Analogy

In English, a "harness" means climbing gear, a horse harness, or the cockpit, safety cage, steering, and brakes that turn a raw V12 engine into a drivable Formula 1 car. In AI engineering—formalized by researchers at Anthropic, OpenAI, and Thoughtworks (Martin Fowler)—it is captured by one fundamental equation: `Agent = Model + Harness`.

Imagine hiring a world-class programmer (the LLM Model—such as Claude, GPT, or Gemini), locking them inside a windowless, soundproof room without a computer, and sliding paper notes under the door. That is how a normal browser chat works. You slide a note with a code snippet; the model guesses an answer from memory and slides a note back. It cannot open your other files, cannot run the compiler to see if its idea actually works, and forgets you exist the moment you walk away.

An AI Harness is the complete workshop, exoskeleton, and rulebook you build around that brain. It gives the model five concrete layers: (1) Instructions on the wall (`AGENTS.md` / `CLAUDE.md`), (2) Eyes and Hands (tools to read files, run terminal commands, and call MCP servers), (3) A Working Desk (managed context window + disk notes), (4) Automatic Quality Sensors (compilers, linters, and test runners that catch mistakes immediately), and (5) Safety Brakes (`Prepare → Confirm` permission gates). When the model writes code inside a harness, the harness automatically runs the tests and feeds any compiler error right back to the model so it fixes its own bug before you ever look at the screen.

GOLDEN RULE OF HARNESS ENGINEERING

The Model is the rented engine; the Harness is the steering, brakes, and workshop you own. A well-engineered harness around a standard model consistently beats a frontier model running in a chaotic environment.

Anatomy of `Agent = Model + Harness` (Closed-Loop ReAct Execution)
┌─────────────────────────────────────────────────────────────────────────┐
│                       AI AGENT HARNESS (YOUR ENVIRONMENT)               │
│                                                                         │
│  [1. CONSTITUTION]   AGENTS.md / CLAUDE.md (<60 lines map & red lines)  │
│  [2. ON-DEMAND DOCS] .agents/skills/*/SKILL.md (Loaded Just-In-Time)    │
│                                   │                                     │
│                                   ▼                                     │
│        ┌───────────────►  🧠 LLM MODEL (BRAIN)  ───────────────┐        │
│        │                 (Reason & Choose Tool)                │        │
│        │                                                       ▼        │
│  [4. SENSORS & OBSERVE]                               [3. HANDS & TOOLS]│
│  • Typecheck (tsc)                                    • Read/Edit Files │
│  • Test Runner (Playwright/Vitest)                    • Terminal / Git  │
│  • Context Linter (verify-harness.mjs)                • Scoped MCP APIs │
│        ▲                                                       │        │
│        └─────────────────  Auto-Feedback Loop  ◄───────────────┘        │
│                                   │                                     │
│                                   ▼                                     │
│  [5. SAFETY BRAKE]   Prepare → Confirm Gate / Human Review Sign-Off     │
└─────────────────────────────────────────────────────────────────────────┘
LEVEL 1 → 2 · CONTEXT WINDOW PHYSICS

2. Why AI "Gets Dumb" After an Hour: The Context Window Is a Desk (RAM), Not a Filing Cabinet

Every AI model has a "Context Window" (for example, 200,000 tokens). Beginners often think: "Great, it fits a whole book—I will dump my entire codebase into one chat and work in that single thread all week!" In practice, this is the #1 cause of hallucinations, broken code, and skyrocketing API bills.

Think of the context window as the surface of your physical working desk (RAM), and your hard drive or repository as the filing cabinet. If you dump 80 folders, old terminal error logs, and three hours of dead-end arguments onto your desk, two physical problems occur: (1) Context Rot & "Lost in the Middle"—the model’s attention mechanism gets diluted by noise, causing it to silently ignore rules from your `AGENTS.md`; and (2) Compounding Token Tax—LLMs are stateless, meaning on every single new message in a thread, the harness must re-send and re-process the entire desk from scratch. Past 55–60% context capacity, both reasoning precision and speed drop sharply.

THE 60% CONTEXT CEILING

Never wait for the context bar to hit 95%. In professional harness engineering, 55–60% utilization is the red line where you either delegate to a subagent, compact state, or start a fresh session.

LEVEL 2 · CONTEXT SAVING & SUBAGENTS

3. Four Levers to Save 80% of Context & Maximize Precision (Progressive Disclosure & Subagents)

How do elite engineers keep their main agent context at a crisp 15–25% utilization even when building million-line systems? They use four structural harness mechanisms:

Lever 1: Map, Not Encyclopedia (`AGENTS.md` / `CLAUDE.md` under 60 lines). Because your root instruction file is injected into every turn, stuffing 1,500 lines of style guides into it is a permanent tax. Keep `AGENTS.md` under 60 lines: state the stack, the exact build/test commands, hard red lines, and pointers to deeper files.

Lever 2: Progressive Disclosure via On-Demand Skills (`SKILL.md`). Store specialized runbooks—like database migrations, E2E testing, or brand voice rules—inside `.agents/skills/<skill-name>/SKILL.md` (or `.claude/skills/`). At session start, the harness loads only the 2-line `name` and `description` from the YAML header (~30 tokens). The full 150-line skill is pulled onto the desk only when the current task actually needs it.

Lever 3: Subagents as "Scouts with Disposable Scratchpads" (Context Isolation). If your agent needs to search 30 files to understand how authentication works, never let the main agent read all 30 files directly—that instantly dumps 60,000 tokens of raw code onto your main desk. Instead, the harness spawns a read-only `research` Subagent. The subagent opens its own isolated, temporary desk, reads the 30 files, and returns ONLY a 200-word summary with exact file paths and line numbers (`src/auth.ts#L40-L85`). The subagent’s scratchpad is discarded, saving ~95% of your main context window!

Lever 4: Filesystem-as-Memory & Clean Session Handoffs (`1 Task = 1 Session`). Instead of keeping a 12-step project plan inside chat history, write it to `TODO.md` or `feature_list.json` on disk. When one feature is done and tested, commit to Git and run `/compact` or open a clean session.

Production-Grade Lean AGENTS.md (<50 lines) Using Progressive Disclosure & Subagents
# AGENTS.md — Workspace Harness & Operational Map

## 1. Project Identity & Stack
- **System:** Bilingual educational platform & interactive studio
- **Stack:** TypeScript, Astro 5, Playwright E2E, Node.js 22
- **Key Paths:** `src/content/` (bilingual data), `src/components/` (UI), `tests/` (E2E)

## 2. Deterministic Verification Loop (Mandatory Before Task Completion)
1. **Build & Schema Parity:** `npm run build`
2. **HTML & SEO Contract Audit:** `npm run verify`
3. **Playwright E2E Suite:** `npx playwright test`
4. **Harness Context Budget Check:** `node scripts/verify-harness.mjs`

## 3. Context Hygiene & Subagent Rules
- **60% Context Ceiling:** Keep main thread lean; read targeted line ranges instead of whole files.
- **Subagent Delegation:** Spawn a `research` subagent whenever exploring >4 files; require a <250-word synthesis with exact file/line references.
- **Session Discipline:** One feature per session; persist multi-step state in `feature_list.json`.

## 4. Progressive Disclosure Map (Load Just-In-Time)
- Context saving & subagent patterns → `.agents/skills/context-hygiene/SKILL.md`
- Default-FAIL verification & evaluator loop → `.agents/skills/verification-loop/SKILL.md`
- Interactive harness bootstrapper → `.agents/skills/harness-architect/SKILL.md`

## 5. Hard Guardrails (Red Lines — Require Human Confirmation)
- NEVER push to `main` or mutate external APIs without explicit human approval.
- NEVER delete failing assertions or add `@ts-ignore` to force a green build.
LEVEL 2 → 3 · TESTING VS. CONTEXT TRADE-OFFS

4. The Testing vs. Context Token Trade-Off Matrix & 3-Strike Circuit Breaker

Why does the exact same LLM write sloppy, broken code for one developer and clean, production-ready architecture for another? In OpenAI’s landmark report ("Harness Engineering: Leveraging Codex in an Agent-First World"), the secret was pairing a probabilistic model with deterministic repository sensors. However, there is a critical engineering trade-off: running full E2E test suites inside the main chat after every tiny edit floods your context window with thousands of lines of terminal output, burning tokens and accelerating Context Rot—while running too few tests lets silent bugs slip into production.

To solve this dilemma, `agent-harness-kit` v2.0 introduces 4 selectable Testing-vs-Context Profiles (`testing-tradeoffs` skill): (1) `fast-prototype` (~300–600 tokens/turn: fast syntax/typecheck only, ideal for weekend PoCs), (2) `smart-tiered` (~800–1,500 tokens/turn: the Golden Ratio default—runs `tsc` + 1 targeted unit test per edit, reserving full E2E for final task completion), (3) `isolated-verifier` (~500 tokens in main chat: delegates heavy Playwright/Docker suites to a disposable subagent that returns a `<200-word` pass/fail digest), and (4) `mission-critical` (~3,000–8,000 tokens/turn: full unit + E2E + security audit for payments, auth, and DB migrations). Every profile is paired with a 3-Strike Repair Circuit Breaker: if the same test fails 3 times in a row, the agent must stop blind guessing, log its hypotheses in `HANDOFF.md`, and ask the human developer.

THE 3-STRIKE REPAIR CIRCUIT BREAKER

Never let an agent loop 15 times on a failing test—that burns 50,000 tokens and corrupts your working tree. After 3 consecutive failed repair attempts, the harness trips the circuit breaker, writes findings to HANDOFF.md, and escalates to you.

The 4 Testing Depth vs. Context Token Profiles (`skills/testing-tradeoffs/SKILL.md`)
┌─────────────────────┬────────────────────────────────────┬──────────────────────┬───────────────────────┐
│ Profile (--profile) │ Inner Loop (Every Edit)            │ Est. Token Tax/Turn  │ Best For              │
├─────────────────────┼────────────────────────────────────┼──────────────────────┼───────────────────────┤
│ fast-prototype      │ Fast typecheck / syntax only       │ ~300–600 tokens      │ Hackathons, early PoC │
│ smart-tiered ★      │ Typecheck + 1 targeted unit test   │ ~800–1,500 tokens    │ 90% of daily projects │
│ isolated-verifier   │ Heavy E2E delegated to Subagent    │ ~500 tok (main chat) │ Noisy Playwright/E2E  │
│ mission-critical    │ Full lint + unit + E2E + security  │ ~3,000–8,000 tokens  │ Auth, billing, DBs    │
└─────────────────────┴────────────────────────────────────┴──────────────────────┴───────────────────────┘
LEVEL 3 · DUAL-MODE WIZARD (GREENFIELD & BROWNFIELD)

5. Dual-Mode Hand-Holding Wizard (Greenfield vs. Brownfield) & `agent-harness-kit` v2.0

A truly useful harness builder cannot treat a blank folder and a 100,000-line legacy repository the same way. That is why our open-source `RolePlayingTech/agent-harness-kit` v2.0 guides you "by the hand" across two distinct operational paths (`harness-architect`):

Path A — Greenfield Project & Stack Advisor (`greenfield-advisor`, for empty repos): When you start from just an app idea, no code or linter exists yet. The agent first helps you define your MVP core loop and explicit Non-Goals, compares 2–3 technology stacks with honest pros/cons, hosting costs, and AI-legibility ratings, helps you pick a Testing-vs-Context profile, and generates a complete project blueprint (`docs/PROJECT_BRIEF.md`, `docs/ARCHITECTURE.md`, ADR, `docs/ROADMAP.md`, `HANDOFF.md`, and a Default-FAIL `feature_list.json` initialized with honest `"pending"` statuses).

Path B — Brownfield Silent Repo Reconnaissance (`brownfield-onboarder`, for existing repos): When run on an existing project, the agent NEVER asks lazy questions like "what framework do you use?" that are already answered in `package.json` or `pyproject.toml`. Instead, it silently scans your workspace first (`agent-harness inspect .`), reports grounded findings with exact file paths, preserves your existing `AGENTS.md`/`CLAUDE.md` non-destructively, and discusses only unknowns: fragile legacy zones, noisy tests, and your preferred testing-vs-context trade-off.

OPEN-SOURCE TOOLKIT · AGENT-HARNESS-KIT V2.0

Run `npx github:RolePlayingTech/agent-harness-kit inspect .` and `init .` in any new or existing repository — works with Claude Code, Google Antigravity, Cursor, GitHub Copilot, Windsurf, and OpenAI Codex.

Dual-Mode CLI & Conversational Setup with RolePlayingTech/agent-harness-kit v2.0
# 1. Silently inspect any empty (Greenfield) or existing (Brownfield) repository:
npx github:RolePlayingTech/agent-harness-kit inspect .

# 2. Preview the recommended interview path & Testing-vs-Context trade-off matrix:
npx github:RolePlayingTech/agent-harness-kit plan .

# 3. Non-destructively install the 6 skills + generate harness & blueprint files:
npx github:RolePlayingTech/agent-harness-kit init . --profile smart-tiered --blueprint

# 4. Run strict deterministic verification (checks YAML schema, line caps & broken paths):
npx github:RolePlayingTech/agent-harness-kit verify . --strict
ARCHITECTURE COMPARISON

Evolution of AI Workflows: Raw Chat vs. Bloated Agent vs. Engineered Harness

DimensionRaw Chat / Unmanaged AgentEngineered AI Harness (`Model + Harness`)
Root instructions (`AGENTS.md`)Either missing OR a bloated 1,500-line encyclopedia burning tokens on every turnLean <60-line routing map + on-demand `SKILL.md` modules (Progressive Disclosure)
Codebase explorationMain agent reads 30 full files into primary chat, causing Context Rot (>70% full)Isolated Scout Subagent explores in a disposable window and returns a 200-word summary
Quality verificationHuman manually copies code, finds syntax errors, and pastes stack traces backDeterministic sensors (types, linter, tests) run automatically inside the self-healing loop
Long-running reliabilitySingle marathon thread forgets rules and declares premature "early victory"Multi-session `feature_list.json` ledger + Fresh-Context Evaluator with Default-FAIL contract
Safety & mutationsUnbounded execution or accidental live side effectsHard red lines in harness + two-phase `Prepare → Confirm` token gates
INTERACTIVE SYSTEM LAB · MUMRO STUDIO

Context Window & Harness Simulator: Raw Chat vs. Bloated Context vs. Lean Harness

Interactive blueprint
1. WORKING DESK (CONTEXT WINDOW RAM) & CODE QUALITY SIMULATOR
CONTEXT WINDOW UTILIZATION: 18% (CLEAN CORE · -86% TOKEN SAVING)~19,000 tokens / session

18% Clean Desk Breakdown: 2% AGENTS.md Map (<50 lines, ~390 tokens) · 4% One Just-In-Time SKILL.md · 2% Scout Subagent synthesis · 10% Active code & test result.

How Context Saving Works

Progressive Disclosure loads only the active SKILL.md, while a Scout Subagent inspects 30 files in a disposable window.

Code Quality Impact

100% sharp model attention + deterministic sensors (tsc, tests, verify-harness.mjs) and Default-FAIL Evaluator.

2. INTERACTIVE ANATOMY: INSPECT EACH LAYER OF AGENT = MODEL + HARNESS
CORE · RENTED ENGINE (CLAUDE / GPT / GEMINI)

In plain English: A brilliant brain locked in a soundproof room. It reasons and predicts tokens, but on its own it cannot see your files, cannot run a terminal, and forgets everything when the tab closes.

FREE OPEN-SOURCE TOOLKIT · ROLEPLAYINGTECH / AGENT-HARNESS-KIT

Want to scaffold this exact harness in your new or existing project? Grab our `harness-architect` skill on GitHub—your agent will ask 5 questions about your stack and generate a lean AGENTS.md, skills, and context budget linter.

Open agent-harness-kit on GitHub ↗
WORKSPACE CASE STUDYOpen-Source `agent-harness-kit` & MUMRO Workspace Engine

Bootstrapping a Codebase Harness in 30 Seconds & Running the Same Pattern for Brand Content

Whether an agent is writing TypeScript in your IDE or orchestrating a multi-channel marketing campaign in MUMRO, the architectural problem is identical: an un-harnessed LLM hallucinates facts, burns context, and risks unsafe mutations.

  1. In software repos: Developer runs `install-harness-kit.mjs` from `RolePlayingTech/agent-harness-kit` and invokes `harness-architect`. The agent conducts a 5-question interview and scaffolds a 45-line `AGENTS.md`, on-demand skills, and `verify-harness.mjs` (~390 always-on tokens instead of 12,000).
  2. During coding: The primary agent delegates multi-file searches to a `research` subagent (keeping main context at 18%) and runs `npm run verify` + Playwright tests before finishing.
  3. In MUMRO brand operations: MUMRO acts as the production Harness for content agents—exposing Progressive Disclosure context via MCP (`get_workspace_context`), isolated draft scratchpads (`create_publication_draft`), Brand Voice negative boundaries, and deterministic `prepare_*` → `confirm_*` safety brakes.

Workflow outcome: 80%+ reduction in wasted context tokens, zero silent regressions, and complete human control across both code and content.

HOW MUMRO IMPLEMENTS THIS

MUMRO is a complete production Harness for Brand, Content & Marketing Agents.

Just as Claude Code or Antigravity wraps a coding model in file tools, linters, and `AGENTS.md`, MUMRO wraps AI agents in a deterministic Brand Operating Harness—combining on-demand workspace context, Brand Voice guardrails, safe draft workspaces, and two-phase confirmation gates.

  • On-demand context delivery (`get_workspace_context`, `get_content_strategy`, cohort analytics) instead of pasting giant PDFs into chat (`AG01`).
  • Safe workspace scratchpads (`create_publication_draft`, `create_article_draft`) separated from live publishing via `prepare_*` → `confirm_*` gates (`AG02`, `AG03`).
  • Hard operational guardrails: 3 graduated autonomy modes, quiet hours (`22:00–07:00`), monthly credit caps, and an instant kill switch (`AG05`–`AG08`).
INTERACTIVE READINESS AUDIT

Lesson 13 Self-Audit Checklist (Harness & Context Readiness)

0 / 4
MUMRO ACADEMY / RolePlayingTech

Let’s connect your brand.

Talk to RolePlayingTech about MUMRO, your content workflow, and access for your brand or agency team.

Contact RolePlayingTech

Continue to roleplayingtech.com to get in touch.