Guide · Updated October 2026
How to build an agent harness
An agent is a model plus the code around it: the loop, the tools, the memory, the permissions and the checks. That code is the harness. Most agent failures come from the harness, not the model.
First, use one before you build one
Building a harness from scratch is rarely the right first move. These already handle the loop, tools and sessions, so you can spend your time on the tools and tests that are specific to your business:
- Claude Agent SDK (the harness behind Claude Code, as a library): docs
- OpenAI Agents SDK (Python): docs
- Codex CLI, open source under Apache-2.0, scriptable with
codex exec: repo - Amazon Bedrock AgentCore, if you run on AWS: a managed harness, runtime, memory, identity, a tool gateway, policy and evaluations, working with most frameworks and models (docs). Its docs note that AgentCore may use and store your content to improve your service experience, so check that against your data rules first.
Build your own when you need full control of the loop, run fully on-prem, or have requirements none of these meet.
The seven parts of a working harness
1. The loop, with stop conditions
The model picks an action, the harness runs it, the result goes back to the model, and the cycle repeats. Every loop needs three exits: the task is done (checked by code, not just claimed by the model), the step limit is hit, or the budget runs out. A loop without exits is how a $5 task becomes a $50 one.
2. Tools: few, narrow and typed
Give the agent the smallest set of tools that does the job, each with a plain-English description, typed inputs and short outputs. MCP (Model Context Protocol) is the standard way to package tools so any agent can use them. A tool that returns a whole database table wastes context; return only the fields the next step needs.
3. Context: short instructions, retrieval for the rest
Standing instructions are paid for on every call. Keep them to what the agent must always know, and fetch reference material when it's needed. A shared, plain-file memory (an AGENTS.md or CLAUDE.md entry file pointing to project notes) gives every tool and every session the same starting point.
4. Permissions: read-only by default
Start every agent read-only. Writes, deletes, payments and outbound messages need an explicit approval step or a narrow allow-list. Run code in a sandbox. Credentials come from the environment, never from the model's output.
5. Budgets and model routing
Staff the agent like a crew: the strongest model plans and makes hard calls, a mid-tier model does most steps, and a small model handles high-volume sorting and checks. Escalate to a bigger model only when a check fails. Put a budget on each agent and each run.
6. Logs and traces
Record every model call and tool call with its input, output, cost and time. When something goes wrong, the trace is the only way to see why. Open-source tracing tools such as Langfuse do this without a vendor contract.
7. Evals: a test set that runs on every change
Collect 20 to 50 real tasks with known good outcomes, and run them whenever you change a prompt, a tool or a model. promptfoo and similar tools run these in CI. Without a test set, every change is a guess.
Build order
- Write the test set first: 20 to 50 real tasks with known good outcomes.
- Start read-only with one or two tools, and log everything from day one.
- Add stop conditions: a done check, a step limit and a budget.
- Run the tests on the cheapest model that might work, and escalate only where it fails.
- Add write tools one at a time, each behind an approval step.
- Re-run the tests on every change to a prompt, tool or model.
What usually breaks
- Endless loops. The model keeps “trying one more thing.” Fix: step limits, and a done check run by code.
- Context bloat. Tool results and long histories crowd out the instructions. Fix: shorter tool outputs, and summarize or restart long sessions.
- Silent cost growth. A prompt change doubles the tokens and nobody notices. Fix: cost per run in the logs, and a budget alert.
- Over-broad tools. A “run any SQL” tool does something nobody intended. Fix: narrow tools, read-only first.
- No tests. A model upgrade quietly changes behavior. Fix: the test set, run before every change ships.