How to Design and Build a Custom Agent Harness for Your Business

True agent customization is not about chasing the strongest model, but engineering a harness tailored to your business task through minimal primitives and composable middleware.

By Sydney Runkle09-14 · 8 min read

When building an AI agent for a specific business use case, the critical question developers must decide is rarely "which model is strongest." Instead, it comes down to clarifying the operational requirements of the specific task at hand: the context it needs, the failures it'll encounter, the policies it must enforce, and the environment it operates in. These four dimensions are precisely the core criteria explicitly identified by the original source when defining Task-Harness Fit.

Building truly practical agents is fundamentally about customization—how to connect the agent to the requisite context, data, and execution environment for a specific task. The Agent Harness wrapped around the model is the exact engineering entity responsible for establishing this connection. It should be noted that the source does not provide an out-of-the-box, end-to-end business implementation tutorial; rather, it systematically explains an architectural path for designing and tailoring a harness to your business using minimal primitives and composable middleware.

The engineering core of production agents: Custom Harness

Conceptual Decoupling: What Is a Harness? Why Does Agent = Model + Harness?

At the most fundamental level, an agent is simply a model invoking external tools in a loop until the task is complete and a result is returned:

The basic tool-calling loop of an agent

This operational mechanism can be expressed clearly with a core formula:

Agent = Model + Harness

Here, the Harness represents the entire scaffolding engineering wrapped around the large language model that connects it to the real world.

The composite architecture of Agent equals Model plus Harness

This architectural understanding is grounded in two premises:

  1. An agent is only as good as the context provided to the model;
  2. The job of a harness is to provide context to the model at every step.

Therefore, building a genuinely viable agent requires constructing a harness capable of delivering the right context to the model on time and on demand for a given task.


The Fork Between Minimal Primitives and Pre-assembled Harnesses: Why create_agent?

Within the LangChain ecosystem, the foundational primitive for constructing a custom harness is create_agent. Simply pass in a model, a set of tools, and a system prompt to obtain a fully functional agent:

from langchain.agents import create_agent

agent = create_agent(
    model="anthropic:claude-sonnet-4-6",
    tools=tools,
    system_prompt="you are a helpful assistant..."
)

When evaluating architectural options, two distinct approaches exist in the ecosystem:

  • Pre-assembled Harnesses: Such as Deep Agents or Claude Agent SDK. These come pre-packaged with an opinionated middleware stack covering components like memory, context management, and sandboxing. Such harnesses aim to get teams to production-ready agents quickly and work well for most standard scenarios. However, many agents require finer-grained customization control than pre-assembled harnesses offer—such as proprietary prompts, specialized business logic, and custom guardrails.
  • The Purposefully Minimalistic Primitive Route: create_agent adopts a different philosophy, similar to the highly configurable coding agent harness Pi. create_agent implements only the bare core agent loop and exposes Middleware as its customization primitive.

Middleware Mechanics: Fine-Tuning a Harness Across Four Levers

Middleware serves as the primary mechanism for deeply customizing a harness. Middleware can hook directly into various stages of an agent's execution loop: before and after model calls, before and after tool calls, and at agent startup and termination. Each middleware handles a single concern independently and can be freely composed and assembled with others:

Middleware interception mechanisms inside a harness

Through middleware, developers can leverage four engineering levers that frequently work in concert to tailor the harness to business needs:

  1. Deterministic Logic:
    • Design Problem Solved: Business rules, policy enforcement, and dynamic runtime control need to trigger at specific points in the execution loop and should not be left entirely to prompts.
    • Mechanism and Source Examples: Deterministic code executing at specific points in the loop. Key scenarios explicitly highlighted by the source involve dynamic runtime control: swapping the model based on task complexity, adjusting the prompt, and updating the agent's message history, during compaction, for example. Any logic that cannot or should not be written into a prompt belongs in this category.
  2. Tools (Lifecycle Governance):
    • Design Problem Solved: When tools carry external dependencies, require initialization, or need teardown upon completion, directly binding tools to an agent scatters configuration and governance logic.
    • Mechanism and Source Examples: Rather than bluntly registering tools directly onto the agent, middleware assumes end-to-end governance over the full tool lifecycle—setup, teardown, and dynamic registration, while managing tool dependencies to deliver a clean, structured set of available tools to the agent. This ensures tool configuration stays cohesive with its underlying control logic, preventing configuration drift.
  3. Custom State:
    • Design Problem Solved: Middleware needs to continuously record and share business state across different lifecycle hooks.
    • Mechanism and Source Examples: Extend the agent's state with custom properties. The source explicitly notes that this allows middleware to maintain counters, flags, or other persistent values across the entire execution cycle, seamlessly sharing data across different hooks.
  4. Stream Handlers:
    • Design Problem Solved: Different events in an agent's output stream need to be handled by distinct consumers such as user interfaces, audit logging, and monitoring.
    • Mechanism and Source Examples: Middleware can intercept and reshape the output stream—filtering events, injecting metadata, and routing different event types to downstream consumers. Typical business scenarios provided by the source include: a frontend UI consuming token deltas for real-time typing rendering, an audit logging system capturing tool calls for compliance records, and a monitoring system tracking latency to ensure service stability.

Key engineering benefits of the middleware mechanism:

  1. Enables seamless customization at any interception point in the agent lifecycle;
  2. Encapsulates related engineering logic into decoupled, composable, and reusable units of code across projects.

LangChain provides Prebuilt Middleware covering common patterns; for proprietary requirements, teams can also implement Custom Middleware. Because each middleware operates in isolation, individual components can be reused across an organization, enabling new agents to inherit battle-tested behaviors.


Working Backward from Business Tasks: Eight Capability Needs and Middleware Mapping

The ultimate mission of a harness is to provide the model with context precisely tailored to the current task at every step of the loop.

Building a harness is not about mounting every available middleware component. The source explicitly highlights that the final combination of middleware an agent employs depends on the core requirements of the task: is it long running?, how complex are the tasks?, and how sensitive are the agent's actions? A customer support agent requires a markedly different harness than a long-running coding agent, though the source does not prescribe rigid configurations for either.

Business Design Method: Working Backward from Task Requirements to Assemble a Harness

Focusing on "how to design and build a custom harness for your own business," combining the four Task-Harness Fit dimensions with the official capability mapping yields a backward-design methodology for business developers (Note: This represents a practitioner workflow synthesizing the source's Task-Harness Fit considerations with its capability table, rather than an official four-step specification prescribed by LangChain):

  1. Define the Mission: Clearly articulate the agent's core business objective, operational boundaries, and expected deliverables.
  2. Audit Characteristics Across Four Dimensions (Context / Failures / Policies / Environment):
    • Context: Is the session a multi-turn, long-running interaction or a single-shot Q&A? Does it require a memory module to load external knowledge at startup and persist takeaways upon termination?
    • Failures: What unpredictable, transient failures might the model or external tools encounter?
    • Policies: Are there PII redaction requirements, compliance audits, or sensitive actions requiring human approval?
    • Environment: Does the agent operate in a restricted data environment, or does it require broader execution access to a filesystem, shell, or code interpreter?
  3. Select from Eight Core Capabilities on Demand: Based on the audited characteristics, select only the capabilities genuinely needed for the current business task from the eight capabilities below, avoiding unnecessary bloat.
  4. Map to Concrete Middleware Stacks: Direct the selected capabilities to prebuilt LangChain middleware or custom-written middleware, mounting them into the create_agent execution loop.

TASK → CAPABILITY → MIDDLEWARE

Assemble a Harness from Your Business Conditions

Select the conditions that actually exist in your business. The panel on the right will combine and deduplicate the corresponding middleware. The eight capabilities below remain fully readable without making a selection.

What does your business need?
01Prevent context overflowSummarization · ContextEditing
02Access and update memoryFilesystem · Memory · Skills
03Act in an environmentShellTool · Filesystem · CodeInterpreter
04Delegate complex tasksSubAgent · AsyncSubAgent · TodoList
05Handle transient failuresToolRetry · ModelRetry · ModelFallback
06Enforce policiesPII · HumanInTheLoop
07Steer and control the agentHumanInTheLoop
08Control operating costsModelCallLimit · ToolCallLimit · PromptCaching

The following table illustrates the eight core production capability needs, their underlying rationale, and their corresponding official prebuilt middleware mappings:

Capability Why it Matters Middleware
Prevent context overflow Long-running conversations rapidly accumulate message history. Without intervention, this leads to context window overflow. SummarizationMiddleware, ContextEditingMiddleware
Access and update memory Load relevant knowledge at startup and write back updates upon completion. Allows the agent to continually evolve over time through real-world usage. FilesystemMiddleware, MemoryMiddleware, SkillsMiddleware
Take actions in an environment A fixed toolset restricts the boundary of an agent's capabilities. Granting filesystem and execution environment access unlocks more creative solutions and is often significantly more token-efficient. ShellToolMiddleware, FilesystemMiddleware, CodeInterpreterMiddleware
Delegate tasks Subagents handle complex subtasks within separate, clean context windows. A todo list tracks overall task progress across long-running lifecycles. SubAgentMiddleware, AsyncSubAgentMiddleware, TodoListMiddleware
Handle transient failures Model and tool invocation failures are unpredictable. Production-grade agents require backoff and retry logic, along with automatic fallback strategies when primary models become unavailable. ToolRetryMiddleware, ModelRetryMiddleware, ModelFallbackMiddleware
Enforce policies PII redaction, compliance validation, approval checkpoints—regardless of what the model outputs, these checks must be enforced deterministically on every call; they must never rely on prompt instructions alone. PIIMiddleware, HumanInTheLoopMiddleware
Steer the agent Full autonomy is not always desirable. Pause execution before high-impact actions to await human approval, rejection, or redirection. HumanInTheLoopMiddleware
Control costs Prompt caching reduces token expenditures during long-running tasks. Call limits prevent costs from compounding unchecked. ModelCallLimitMiddleware, ToolCallLimitMiddleware, PromptCachingMiddleware

Complete definitions and usage documentation for prebuilt middleware can be reviewed in the official Prebuilt Middleware Reference.


Task-Harness Fit: Verifying Your Custom Harness

Task-harness fit evaluates how well a harness matches the actual demands of a business task: the context it needs, the failures it'll encounter, the policies it must enforce, and the environment it operates in. As noted earlier, the harness architecture for a customer support agent looks fundamentally different from that of a long-running coding agent.

LangChain highlights three internal agents built using create_agent, each assembled with a dedicated middleware stack tailored to its task:

Great agents rely not only on capable models, but equally on harnesses tightly aligned with the specific task. LangChain's direct approach is to build this custom harness using create_agent combined with composable middleware.

Custom Harness Minimum Design Checklist

When applying these principles to your own business scenarios, use this quick checklist derived from the source's four dimensions and capability matrix:

  1. Context & Memory:
    • Does the task involve long-running sessions? Is context summarization or editing middleware configured to prevent window overflow?
    • Does the agent need to load external knowledge or skills at startup and persist newly acquired experience back into memory upon completion?
  2. Failures & Costs:
    • When external models or tools encounter unpredictable transient failures, are backoff retry and model fallback middleware in place?
    • Is prompt caching configured to reduce long-task token costs? Are call limits set on models and tools to prevent runaway cost accumulation?
  3. Policies & Steering:
    • Does the business have PII redaction or compliance auditing requirements? Are they deterministically enforced across every call via middleware?
    • For high-impact or sensitive actions, is a human-in-the-loop mechanism configured to pause and await human approval, rejection, or redirection?
  4. Environment & Delegation:
    • Does the agent need to move beyond a fixed toolset and access a filesystem, shell, or code interpreter to improve action and token efficiency?
    • For complex, long-running tasks, does the harness decompose work to subagents with isolated, clean contexts or track milestones via a todo list?
    • Are tool dependencies, setup, and teardown governed by middleware?
    • Does the output stream require differentiated routing: frontend consuming token deltas, audit logging recording tool calls, and monitoring tracking latency?

Further Resources and References

Official Guides and Documentation

Source
How to Build a Custom Agent HarnessSydney Runkle·2026-06-03·Read the source
↑ End of article
✓ This article is free to read in full

Members can copy or download the full Markdown, and let their AI subscribe and ask.

Read the original ↗
📮 New explainers, straight to your inbox

Found this useful? Leave your email — every new explainer, the moment it's out. Unsubscribe anytime.