Technical Deep-Dive
What is an Agent Harness?The layer that makes a model useful
An agent harness is everything around a model that turns text generation into action: system prompts, tools, memory, sandboxing, middleware and deterministic guards. The model decides what to do; the harness makes it happen and keeps it reliable.
An agent harness is the entire execution layer surrounding an AI model — the system prompts, tools, memory, sandboxes, middleware, and orchestration logic that turn a language model into a working agent. If you're not the model, you're the harness.
The Big Idea
If you are not the model, you are the harness.
A model turns text into text. A harness turns that text into action — with tools, memory and deterministic guardrails around every step.
Harness Anatomy
Inside the Harness
A raw model takes text in and produces text out. It cannot access files, call APIs, maintain state, or execute code. The harness provides all of these capabilities — it is the scaffolding that makes model intelligence useful.
System Prompt
Defines the agent's role, capabilities, and behavioral constraints.
The system prompt is the harness's first layer of control — it shapes how the model interprets every user request and which strategies it applies.
Tools & Skills
APIs, databases, web search, file systems, code execution — anything the agent can use.
Tools are registered with descriptions so the model knows what's available. Skills bundle related tools and instructions for progressive disclosure.
Orchestration Logic
The agent loop, subagent spawning, model routing, and task decomposition.
The harness runs the ReAct loop — reason, act, observe, repeat. It manages when to spawn subagents, which model to route to, and when to stop.
Memory & State
Conversation history, context files, and persistent knowledge across sessions.
The filesystem is the foundational memory primitive. Files like AGENTS.md let agents durably store and retrieve knowledge across sessions.
Sandboxes
Isolated execution environments for running agent-generated code safely.
Sandboxes give agents a secure workspace to run code, install dependencies, and inspect results without risking the host environment.
Middleware & Hooks
Deterministic logic that fires before/after model calls and tool executions.
Middleware handles compaction, retries, guardrails, human-in-the-loop approvals, and cost controls — anything that needs to run at a specific point in the loop.
How It Works
The Agent Loop
At its core, an agent is a model calling tools in a loop until the task is complete. The harness manages this loop — providing context at each step, executing tool calls, handling errors, and enforcing constraints.
- User sends a requestThe harness injects the system prompt, conversation history, and relevant memory into the model's context window.
- Model reasons and decidesThe LLM interprets the request, plans its approach, and decides whether it needs to use a tool or can answer directly.
- Harness executes tool callsWhen the model requests a tool, the harness routes the call to the appropriate API, sandbox, or function and returns the result.
- Model observes the resultThe tool output is injected back into context. The model evaluates whether it needs more actions or can produce a final answer.
- Loop continues until doneThe harness manages the loop — enforcing iteration limits, running middleware between steps, and handling context overflow via compaction.
- Final answer is returnedThe harness applies output filters, logs the interaction, and delivers the result to the user.
# The harness runs the agent loop
def run_agent(model, tools, user_message):
messages = [system_prompt, user_message]
while True:
# 1. Harness provides context to the model
response = model.generate(messages, tools=tools)
if response.tool_calls:
# 2. Harness executes tool calls
for call in response.tool_calls:
result = execute_tool(call)
messages.append(result)
else:
# 3. Model is done — return answer
return response.text
The model decides what to do. The harness decides how to do it. This separation is what makes agents flexible, testable, and swappable.
— Vivek Trivedy, "The Anatomy of an Agent Harness" (LangChain, 2026)
Customization Layer
Middleware: How You Shape the Harness
Middleware hooks into the agent loop at each step — before and after model calls, before and after tool calls, at startup and teardown. Each piece handles one concern and composes freely with any other. This is how you tailor a harness to your specific task.
Context Management
Prevent context overflow and keep the model focused on what matters.
- Summarization middleware compacts long conversations
- Context editing removes noisy or irrelevant messages
- Tool output offloading keeps large results out of context
Memory & Learning
Load knowledge at startup, persist learnings at the end of a run.
- AGENTS.md files injected into context on start
- Skills loaded progressively as needed
- Filesystem middleware for durable state
Safety & Guardrails
Enforce policies that must fire on every call regardless of model behavior.
- PII detection and redaction
- Human-in-the-loop approval gates
- Output content filters and action limits
Cost Controls
Prevent token spend from accumulating unchecked.
- Model call limits per task
- Tool call rate limiting
- Prompt caching for long-running tasks
Error Recovery
Handle transient failures with retry logic and fallbacks.
- Tool retry with exponential backoff
- Model fallback when primary is unavailable
- Stream error handlers
Observability
See exactly what the agent is doing at every step.
- Trace logging for every model call and tool invocation
- Stream handlers that route events to different consumers
- Custom state tracking across middleware hooks
from langchain import create_agent
from langchain.middleware import (
SummarizationMiddleware,
HumanInTheLoopMiddleware,
ToolRetryMiddleware,
)
agent = create_agent(
model="anthropic:claude-sonnet-4-6",
tools=tools,
middleware=[
SummarizationMiddleware(),
HumanInTheLoopMiddleware(approval_tools=["send_email"]),
ToolRetryMiddleware(max_retries=3),
],
)
Middleware bundles related logic in composable, sharable units. The same middleware can be reused across every agent in an organization — new agents inherit battle-tested behavior without rebuilding it.
— Sydney Runkle, "How to Build a Custom Agent Harness" (LangChain, 2026)
Feedforward & Feedback
Guides vs. Sensors
A well-built harness serves two goals: it increases the probability that the agent gets it right in the first place (guides), and it provides feedback loops that self-correct issues before they reach human eyes (sensors).
Guides (Feedforward)
Steer the agent before it acts. Guides increase the probability of good results on the first attempt.
- AGENTS.md — Coding conventions, project structure, and behavioral rules injected into context.Inferential
- Skills — Reusable instruction bundles loaded progressively as the agent tackles different tasks.Inferential
- LSP Integration — Language servers provide type checking, go-to-definition, and structural analysis as guidance.Computational
- Architecture Docs — Module boundaries, dependency rules, and fitness functions that shape agent decisions.Computational
Sensors (Feedback)
Observe after the agent acts and help it self-correct. Especially powerful when optimized for LLM consumption.
- Linters & Type Checkers — Fast, deterministic checks that catch style violations, type errors, and structural drift.Computational
- Test Suites — Unit and integration tests that verify correctness and catch regressions.Computational
- Code Review Agents — AI-powered review that catches semantic issues — over-engineering, redundant code, brute-force fixes.Inferential
- Mutation Testing — Verifies test quality by introducing mutations and checking if tests catch them.Computational
Separately, you get either an agent that keeps repeating the same mistakes (feedback-only) or an agent that encodes rules but never finds out whether they worked (feedforward-only). You need both.
— Birgitta Böckeler, "Harness Engineering" (Martin Fowler, 2026)
Why This Matters
Why Harness Engineering?
Task-Harness Fit
A harness for a customer service agent looks very different from one built for a long-running coding agent. The best agents aren't just built with capable models — they're built with harnesses that tightly fit the task.
Model Independence
Because the harness is separate from the model, you can swap between GPT-5, Claude, Gemini, or open-source models without rewriting agent logic. The harness abstracts the model layer.
Composability
Middleware, tools, and skills compose freely. Each piece handles one concern. The same memory middleware works across every agent. New capabilities are one middleware away.
Observability & Trust
Every decision is traceable. See what the model reasoned, which tools it invoked, what data it accessed, and why. Essential for debugging, compliance, and building trust in autonomous systems.
Common Questions
Frequently Asked Questions
What is the difference between a model and a harness?
Why not just put everything in the system prompt?
What is middleware in an agent harness?
Can I use the same harness with different models?
What are guides and sensors in harness engineering?
What is task-harness fit?
Build With QuotyAI
Ready to Build with Harnesses?
Create an AI agent with a custom harness tailored to your business. Connect your tools, configure middleware, and let the agent handle the rest.
- No credit card required
- Connect your tools in minutes
- Cancel anytime