Harness Engineering for Agentic Coding Systems

langchain · Tech · 2026 · source Agentic: yes · promotional bias: med · source-backed · extracted with prompt 0.5.6 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: agentic coding harness optimization

LangChain needed to improve the reliability and benchmark performance of deepagents-cli without changing the underlying coding model, by engineering the surrounding prompts, tools, middleware, tracing, and execution flow that determine how the agent works.

Constraints:

Architecture

LangChain optimized deepagents-cli by instrumenting every agent action in LangSmith, using Harbor and Daytona to run Terminal Bench tasks, analyzing traces through an agent skill, and then changing only the harness. The improved harness focused on system prompts that require build-verify-fix behavior, LocalContextMiddleware that injects environment and tooling context, PreCompletionChecklistMiddleware that forces verification before exit, LoopDetectionMiddleware that interrupts repeated broken edits, and a reasoning-budget strategy that spends more compute on planning and verification than implementation.

Diagram confidence: explicit (topology described by the source).

flowchart LR
  terminal_bench_20["Terminal Bench 2.0"] --> harbor_orchestrator["Harbor orchestrator"]
  harbor_orchestrator --> daytona_sandboxes["Daytona sandboxes"]
  harbor_orchestrator --> deepagents_cli_agent_loop["deepagents-cli agent loop"]
  daytona_sandboxes --> deepagents_cli_agent_loop
  system_prompt["System prompt"] --> deepagents_cli_agent_loop
  localcontextmiddleware["LocalContextMiddleware"] --> deepagents_cli_agent_loop
  precompletionchecklistmiddleware["PreCompletionChecklistMiddleware"] --> deepagents_cli_agent_loop
  loopdetectionmiddleware["LoopDetectionMiddleware"] --> deepagents_cli_agent_loop
  reasoning_budget_policy["Reasoning budget policy"] --> deepagents_cli_agent_loop
  deepagents_cli_agent_loop --> langsmith_tracing["LangSmith tracing"]
  harbor_orchestrator --> langsmith_tracing
  langsmith_tracing --> trace_analyzer_skill["Trace analyzer skill"]
  trace_analyzer_skill --> human_verification["Human verification"]
  human_verification --> system_prompt
  human_verification --> localcontextmiddleware
  human_verification --> precompletionchecklistmiddleware
  human_verification --> loopdetectionmiddleware
  human_verification --> reasoning_budget_policy
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
Terminal Bench 2.0 Benchmark suite with agentic coding tasks, strict timeouts, verification, and scoring. Harbor orchestrator
Harbor orchestrator Runs Terminal Bench experiments, spins up sandboxes, interacts with the agent loop, and manages verification and scoring. Daytona sandboxes, deepagents-cli agent loop, LangSmith tracing
Daytona sandboxes Isolated execution environments for Terminal Bench tasks. Harbor orchestrator, deepagents-cli agent loop
deepagents-cli agent loop The coding agent being evaluated and improved through harness changes while keeping gpt-5.2-codex fixed. System prompt, LocalContextMiddleware, PreCompletionChecklistMiddleware, LoopDetectionMiddleware, Reasoning budget policy, LangSmith tracing
System prompt Guides the agent through planning, discovery, building with tests, verification, and fixing against the original task spec. deepagents-cli agent loop
LocalContextMiddleware Injects directory structure, parent/child directory context, tools, and environment facts at agent start. deepagents-cli agent loop
PreCompletionChecklistMiddleware Intercepts the agent before exit and reminds it to verify against the task spec. deepagents-cli agent loop
LoopDetectionMiddleware Tracks repeated edits to the same file and injects reconsideration context when the agent appears stuck. deepagents-cli agent loop
Reasoning budget policy Allocates high reasoning to planning and verification while avoiding timeout-prone maximum reasoning everywhere. deepagents-cli agent loop
LangSmith tracing Stores every agent action plus latency, token, and cost metrics for trace analysis. Trace analyzer skill, Harbor orchestrator, deepagents-cli agent loop
Trace analyzer skill Fetches traces, runs parallel analysis agents, synthesizes failure modes, and suggests harness changes. LangSmith tracing, Human verification
Human verification Reviews proposed harness changes to avoid task overfitting and regressions. Trace analyzer skill, System prompt, LocalContextMiddleware, PreCompletionChecklistMiddleware, LoopDetectionMiddleware, Reasoning budget policy

Data flow

Terminal Bench tasks are orchestrated by Harbor in Daytona sandboxes and executed by deepagents-cli. Every action, metric, and trace is sent to LangSmith. A trace analyzer skill fetches experiment traces, spawns parallel error-analysis agents, synthesizes findings, and proposes targeted harness changes. Humans may verify the suggested changes to avoid overfitting, then the next harness version is rerun against the benchmark and the loop repeats.

3 supporting quotes
“It spins up sandboxes (Daytona), interacts with our agent loop, and runs verification + scoring.”article, stated
“Every agent action is stored in LangSmith. It also includes metrics like latency, token counts, and costs.”article, stated
“Spawn parallel error analysis agents → main agent synthesizes findings + suggestions”article, stated

Why this architecture

LangChain chose harness optimization because a model's behavior in autonomous coding depends heavily on context, tools, prompts, middleware, verification loops, and reasoning-budget choices. Tracing makes failures observable at scale, while middleware and context injection compensate for current model tendencies such as weak self-verification, poor time estimation, environment-discovery errors, and doom loops.

3 supporting quotes
“The goal of a harness is to mold the inherently spiky intelligence of a model for tasks we care about.”article, stated
“we can see their inputs and outputs in text space which we then use in our improvement loops.”article, stated
“The more that agents know about their environment, constraints, and evaluation criteria, the better they can autonomously self-direct their work.”article, stated

Models used: gpt-5.2-codex · Claude Opus 4.6 · reasoning models · parallel error analysis agents · main synthesis agent

Scale

Alternatives considered (roads not taken)

Improve performance by changing the underlying model

Rejected because: The experiment intentionally kept gpt-5.2-codex fixed and improved performance only by changing the harness.

“We only tweaked the harness and kept the model fixed, gpt-5.2-codex.”article, stated
“We only changed the harness.”article, stated

Optimize every possible harness knob at once

Rejected because: LangChain deliberately compressed the design space to system prompts, tools, and middleware for this iteration.

“An agent harness has a lot of knobs: system prompts, tools, hooks/middleware, skills, sub-agent delegation, memory systems, and more.”article, stated
“We deliberately compress the optimization space and focus on three: System Prompt, Tools, and Middleware”article, stated

Let the agent decide when its work is done

Rejected because: Agents naturally stopped after superficial self-review, so the harness added explicit build-verify-fix instructions and pre-completion verification middleware.

“The most common failure pattern was that the agent wrote a solution, re-read its own code, confirmed it looks ok, and stopped.”article, stated
“PreCompletionChecklistMiddleware` that intercepts the agent before it exits and reminds it to run a verification pass against the Task spec.”article, stated

Trust the agent to discover its environment by search

Rejected because: Context discovery and search are error-prone, so the harness injects directory and tooling context up front.

“Context discovery and search are error prone, so injecting context reduces this error surface and helps onboard the agent into its environment.”article, stated
“A LocalContextMiddleware runs on agent start to map the cwd and other parent+children directories.”article, stated

Run every task at maximum reasoning

Rejected because: Maximum reasoning caused timeouts and lower score, so the team used a planning/implementation/verification reasoning sandwich.

“You can use the max reasoning budget on every task, but most work can benefit from optimizing reasoning compute spend.”article, stated
“we choose a xhigh-high-xhigh "reasoning sandwich" as a baseline.”article, stated

Automatically apply trace-analyzer suggestions without human review

Rejected because: Humans are useful for preventing task overfitting and regressions in other tasks.

“A human can be pretty helpful in Step 3 (though not required) to verify and discuss proposed changes.”article, stated
“Changes that overfit to a task are bad for generalization and can lead to regressions in other Tasks.”article, stated

Patterns

context-engineering (proposed harness discipline)

LangChain explicitly treats context assembly and delivery as a harness responsibility, using LocalContextMiddleware, directory/tool discovery, time-budget warnings, testing criteria, and problem-solving strategies to onboard agents into the environment.

“Part of harness engineering is building a good delivery mechanism for context engineering.”article, stated
“The purpose of the harness engineer: prepare and deliver context so agents can autonomously complete work.”article, stated

eval-driven-development (benchmark and verification loop)

The team used Terminal Bench scoring, verification runs, self-verification prompts, pre-completion checks, and trace-derived failure analysis to drive each harness iteration.

“It has 89 tasks across domains like machine learning, debugging, and biology.”article, stated
“Self-verification allows agents to self-improve via feedback within a run.”article, stated

observability-tracing (agent failure analysis)

Every action and metric is stored in LangSmith, then trace-analysis agents mine those traces for recurring failure modes and harness changes.

“to understand agent failure modes at scale.”article, stated
“Every agent action is stored in LangSmith. It also includes metrics like latency, token counts, and costs.”article, stated

tool-use-orchestration (sandboxed coding execution)

Harbor, Daytona, deepagents-cli, middleware, bash tool discovery, and verification/scoring are orchestrated around the coding agent so it can execute tasks autonomously.

“It spins up sandboxes (Daytona), interacts with our agent loop, and runs verification + scoring.”article, stated
“We run bash commands to find tools like Python installations.”article, stated

multi-agent-orchestration (trace-analysis workflow)

The trace analyzer skill spawns parallel error-analysis agents and uses a main agent to synthesize findings and suggestions.

“Spawn parallel error analysis agents → main agent synthesizes findings + suggestions”article, stated
“Aggregate feedback and make targeted changes to the harness.”article, stated

human-in-the-loop-gate (generalization review)

Human review is kept in the trace-improvement loop to verify proposed changes and prevent overfitting-induced regressions.

“A human can be pretty helpful in Step 3 (though not required) to verify and discuss proposed changes.”article, stated
“Changes that overfit to a task are bad for generalization and can lead to regressions in other Tasks.”article, stated

model-routing (reasoning compute allocation)

The harness varies reasoning budget across planning, implementation, and verification and discusses future multi-model handoffs from large planning models to smaller implementation models.

“gpt-5.2-codex has 4 reasoning modes, low, medium, high, and xhigh.”article, stated
“balancing reasoning budgets could play out as using a large model for planning and handing off to a smaller model for implementation.”article, stated

Tradeoffs

Optimize the harness, not the model.

Chose System prompts, tools, middleware, tracing, and execution flow changes over Changing or fine-tuning gpt-5.2-codex.

Rationale: The benchmark improvement isolates the value of systems engineering around a fixed model.

“We only tweaked the harness and kept the model fixed, gpt-5.2-codex.”article, stated
“Harness Engineering is about systems, you’re building tooling around the model to optimize goals like task performance, token efficiency, latency, etc.”article, stated

Narrow the initial harness optimization surface.

Chose System prompt, tools, and middleware over All possible harness knobs at once.

Rationale: A compressed design space made the experiment tractable while still improving benchmark performance significantly.

“We deliberately compress the optimization space and focus on three: System Prompt, Tools, and Middleware”article, stated
“An agent harness has a lot of knobs: system prompts, tools, hooks/middleware, skills, sub-agent delegation, memory systems, and more.”article, stated

Force self-verification before exit.

Chose PreCompletionChecklistMiddleware plus explicit build-verify-fix guidance over Trusting the agent's natural stopping behavior.

Rationale: The most common failure was premature exit after superficial code review, and tests provide a feedback signal for hill climbing.

“Testing is a key part of autonomous agentic coding. It helps test for overall correctness and simultaneously gives agents signal to hill-climb against.”article, stated
“PreCompletionChecklistMiddleware` that intercepts the agent before it exits and reminds it to run a verification pass against the Task spec.”article, stated

Inject context instead of relying on search.

Chose LocalContextMiddleware and deterministic environment onboarding over Agent-led discovery of directories, tools, and constraints.

Rationale: Search and context discovery are error-prone, and explicit context lowers avoidable planning failures.

“A LocalContextMiddleware runs on agent start to map the cwd and other parent+children directories.”article, stated
“injecting context reduces this error surface and helps onboard the agent into its environment.”article, stated

Spend reasoning selectively.

Chose xhigh-high-xhigh reasoning sandwich over xhigh reasoning for every phase.

Rationale: Planning and verification benefit from deeper reasoning, while maximum reasoning everywhere causes timeouts and worse score.

“found that reasoning helps with planning to fully understand the problem, some Terminal Bench tasks are very difficult. A good plan helps get to a working solution more quickly.”article, stated
“Running only at xhigh scored poorly at 53.9% due to agent timeouts compared to 63.6% at high.”article, stated

Keep a human check in the trace-improvement loop.

Chose Human verification of proposed harness changes over Fully automated trace-analyzer-driven harness updates.

Rationale: Human review reduces the risk that suggested changes overfit individual tasks and harm generalization.

“A human can be pretty helpful in Step 3 (though not required) to verify and discuss proposed changes.”article, stated
“Changes that overfit to a task are bad for generalization and can lead to regressions in other Tasks.”article, stated

Gotchas (what bit them)

Agents may stop after superficial self-review.

Root cause: The agent wrote a solution, re-read its own code, decided it looked okay, and exited without running meaningful verification.

Resolution: Add explicit build-verify-fix guidance and a PreCompletionChecklistMiddleware that forces a final verification pass against the task spec.

“The most common failure pattern was that the agent wrote a solution, re-read its own code, confirmed it looks ok, and stopped.”article, stated
“it powers the changes in every iteration. We found that alongside prompting, deterministic context injection helps agents verify their work.”article, stated

Autonomous environment discovery is a reliability risk.

Root cause: Terminal Bench environments contain directories, tools, and constraints that agents may discover poorly or too late.

Resolution: Inject directory context, tool availability, testing expectations, file-path requirements, edge-case guidance, and time-budget warnings.

“Context discovery and search are error prone, so injecting context reduces this error surface”article, stated
“Agents are famously bad at time estimation so this heuristic helps in this environment.”article, stated

Agents can get stuck in doom loops.

Root cause: After committing to a flawed plan, agents repeat small edits to the same file rather than reconsidering the approach.

Resolution: Track per-file edit counts with LoopDetectionMiddleware and inject reconsideration context after repeated edits.

“Agents can be myopic once they’ve decided on a plan which results in “doom loops””article, stated
“LoopDetectionMiddleware` that tracks per-file edit counts via tool call hooks.”article, stated

More reasoning is not always better.

Root cause: Maximum reasoning can burn time and tokens under benchmark timeouts, preventing task completion.

Resolution: Use a reasoning sandwich that spends more reasoning on planning and verification while using high rather than xhigh reasoning for implementation.

“More reasoning helps agents evaluate each step, but can burn over 2x more tokens/time.”article, stated
“Running only at xhigh scored poorly at 53.9% due to agent timeouts compared to 63.6% at high.”article, stated

Automated harness optimization can overfit.

Root cause: Trace-derived suggestions can improve specific failed tasks while creating regressions elsewhere.

Resolution: Keep human verification in the improvement loop before accepting targeted harness changes.

“Changes that overfit to a task are bad for generalization and can lead to regressions in other Tasks.”article, stated
“A human can be pretty helpful in Step 3 (though not required) to verify and discuss proposed changes.”article, stated

Harnesses do not transfer perfectly across models.

Root cause: Different models require different prompting and harness iterations, even when general principles transfer.

Resolution: Run several rounds of harness iteration for each task/model pair and track which changes generalize.

“Many principles generalize like good context preparation and a focus on verification”article, stated
“A test run with Claude Opus 4.6 scored 59.6% with an earlier harness version”article, stated

Evaluation

LangChain evaluated harness changes by running deepagents-cli on Terminal Bench 2.0 through Harbor/Daytona, storing every action in LangSmith, analyzing trace failure modes, and comparing benchmark scores, timeouts, reasoning-budget settings, token counts, latency, cost, and cross-model results.

Metrics: Terminal Bench 2.0 score improved from 52.8 to 66.5 · absolute improvement was 13.7 points · ranking moved from just outside Top 30 to Top 5 · Terminal Bench 2.0 contains 89 tasks · xhigh-only reasoning scored 53.9% · high reasoning scored 63.6% · Claude Opus 4.6 scored 59.6% with an earlier harness version · more reasoning can burn over 2x more tokens/time · doom loops repeated small variations 10+ times in some traces

3 supporting quotes
“Every agent action is stored in LangSmith. It also includes metrics like latency, token counts, and costs.”article, stated
“This scores 52.8% with GPT-5.2-Codex. A solid score, just outside the Top 30 of the leaderboard today”article, stated
“There weren’t large differences in trial runs across reasoning budget splits so we stuck with our approach which pushed the score to 66.5%.”article, stated

Outcomes

The case reports 89 benchmark tasks, every agent action stored with latency/token/cost metrics, a 13.7-point score gain, 10+ repeated edits in some doom-loop traces, reasoning settings from low to xhigh, and open trace data for the community.

3 supporting quotes
“Our coding agent went from Top 30 to Top 5 on Terminal Bench 2.0.”article, stated
“Automated trace analysis saves hours of time and made it easy to quickly try experiments.”article, stated
“We created a dataset of our Traces to share with the community.”article, stated