How I build agent harnesses: context, code, tools, then the model
The rules I follow when I build an agent harness, checked against the research: keep the context small, put control flow in code, use tools and small models before a large one, keep agents away from anything they can break, and change prompts only after tests pass.
Last week I wrote that the harness and checker around Google's Cogentic matter more than the model. This post is the practical side. An agent harness is the code around a model: what it sees, the tools it can call, the order of the steps and the checks on its output. I'd say a harness should spend the model only where judgement is needed. Keep the context small, put control flow in code, give exact work to tools and smaller models, keep the agent away from anything it can break, and let it improve only through tests. I'd change my mind if models stopped getting worse as their input grows and could reliably correct themselves without an outside check. Neither is true today. Put together, a run through a harness looks like this.
How a task moves through the harness: capped retries, the sandbox boundary and the improvement loop.
Context comes first. In July 2025 Chroma tested 18 models and found every one grew less reliable as its input grew, long before the window was full1. On one long-memory benchmark, every model family scored better on a focused prompt of about 300 tokens than on the full prompt of about 113,000 tokens1. So I keep the window lean. Instructions cover the goal, key requirements and guardrails. More prompt text mostly adds noise. My framework loads a skill, a folder of instructions and scripts, only when a task needs it. Documents stay on disk and go in by reference, and old tool output is cleared. Summarising the whole history is a backstop, since Anthropic warns it can lose "subtle but critical context"2. Sub-agents help because each one reads in a clean window and hands back a short summary2. They cost more. Anthropic said in June 2025 that its multi-agent research system used about 15 times the tokens of a chat3, and Cognition found that parallel sub-agents blind to each other's choices produce conflicting work4. In asdlc, my public Claude Code setup, sub-agents read and report back, and only the main session writes the plan and docs.
Control flow belongs in code. Every node and sub-agent gets a clear input and an output schema that code checks. In a 2025 Berkeley study of 1,642 traces from seven multi-agent frameworks, many failures came from the design. Agents repeated steps in 15.7% of traces and ignored task requirements in 11.8%5. Skills are fine when the model can choose the order. Once order, branching, retries or approvals matter, I put the workflow in a graph. I use LangGraph, an open-source graph library whose recursion limit stops endless loops6. My framework runs a deterministic check after key model calls and loops back on failure with a retry cap. The check sits outside the model, because models struggle to fix their own reasoning without outside feedback7.
Then I use the cheapest thing that gets the answer exactly right. If code can do it, code does it: maths, lookups, sorting and formatting. In 2022, program-aided language models, which write Python and let an interpreter do the sums, beat much larger models reasoning step by step across 13 maths and symbolic tasks8. Even text can be templated. Landfox, my property research app, builds its English reports with Python and templates, and uses a model only to translate a finished report into Chinese. When a task needs a learned model, a small trained model often wins. In a 2024 study, small fine-tuned models beat zero-shot GPT-4 and Claude Opus on every text classification task tested9. A 2022 study found one-layer linear models beat transformer forecasters on all nine datasets10. With few labels, I find a small LLM with structured output, a reply forced into a fixed schema, classifies well. TypeSafe AI claims its decision-only Jev model is up to 193.6 times faster than LLMs, based on its own tests11. An independent test published on 31 October 2026 found Jev matched Claude Haiku 4.5 and GPT-5.4 mini on accuracy at a lower cost, but its confidence scores were no better calibrated12. For most decisions inside a harness I don't need a dedicated model like Jev. A small open-weight model with a schema does the job, and I can run it myself. The large model comes last.
Safety rules hold however good the model gets. An agent should never edit live code or data in place. In July 2025 Replit's coding agent deleted a production database during a code freeze, and Replit responded by separating development and production databases13. Agents should work on a branch, a worktree or a container, and my framework pauses for human approval before consequential steps. OWASP, the software security foundation, calls this excessive agency, and its fix is the fewest tools, the narrowest permissions and human approval for high-impact actions14. Anything the agent reads is untrusted, because instructions hidden in a web page or document can steer it, an attack known as indirect prompt injection15. Encrypting .env files, a common fix, helps if a file leaks, but not when the agent's own process can decrypt it. Claude Code's documentation says its sandbox can, by default, read credential files such as ~/.ssh and inherits any secrets in its environment16. OWASP advises against environment variables for secrets unless nothing else is possible17. The better rule is to keep secrets out of the agent's reach. A secrets manager or proxy adds credentials at call time, tokens are short-lived and scoped, and credential paths are blocked from reading.
Last, the harness should improve through tests. Prompt optimisers work. GEPA, a 2025 method where a model reflects on failed runs and rewrites the prompt, beat reinforcement learning by 6% on average with up to 35 times fewer runs18. These methods score against a test set, and raw user approval makes a poor score. In April 2025 OpenAI added a reward from users' thumbs-up and thumbs-down, which it says may have helped tip GPT-4o into sycophancy, and it rolled the update back19. The harness should collect feedback and failures, propose a prompt change, and run it against a regression set before a person approves it. Anthropic suggests starting with 20 to 50 tasks drawn from real failures, graded by code wherever possible20. That needs a trace of every run, so failures become tests.
The obvious objection is that models keep improving and this scaffolding will fade. Anthropic itself says smarter models need less prescriptive engineering2. Some prompt-level scaffolding will go. Context rot still showed up in every model Chroma tested1, and a capable model with production access and your keys is still one poisoned document away from damage. I'd keep the graph, the tools, the checks and the permissions, and call the model only where judgement is needed.
Nothing here is financial, legal or investment advice. Every figure is published by its named source and carries the date it was published; figures may since have changed.
References
-
Chroma, Context Rot: How Increasing Input Tokens Impacts LLM Performance (14 July 2025) ↑
-
Anthropic, Effective context engineering for AI agents (29 September 2025) ↑
-
Anthropic, How we built our multi-agent research system (13 June 2025) ↑
-
Cognition, Don't Build Multi-Agents (12 June 2025) ↑
-
Cemri et al., UC Berkeley, Why Do Multi-Agent LLM Systems Fail? (arXiv, revised 26 October 2025) ↑
-
LangChain, GRAPH_RECURSION_LIMIT (LangGraph documentation) ↑
-
Huang et al., Google DeepMind, Large Language Models Cannot Self-Correct Reasoning Yet (arXiv, October 2023) ↑
-
Gao et al., PAL: Program-aided Language Models (arXiv, 18 November 2022) ↑
-
Bucher and Martini, Fine-Tuned 'Small' LLMs (Still) Significantly Outperform Zero-Shot Generative AI Models in Text Classification (arXiv, June 2024) ↑
-
Zeng et al., Are Transformers Effective for Time Series Forecasting? (arXiv, 26 May 2022) ↑
-
TypeSafe AI, Introducing System One Models & Jev (15 September 2026) ↑
-
4oh4/dev, Testing Jev against three small LLMs on text classification and calibration (published 31 October 2026; benchmark run 21 September 2026) ↑
-
Fortune, AI-powered coding tool wiped out a software company's database in 'catastrophic failure' (23 July 2025) ↑
-
OWASP Gen AI Security Project, LLM06:2025 Excessive Agency ↑
-
Greshake et al., Not what you've signed up for: Compromising Real-World LLM-Integrated Applications with Indirect Prompt Injection (arXiv, 23 February 2023) ↑
-
Anthropic, Configure the sandboxed Bash tool (Claude Code documentation) ↑
-
OWASP Cheat Sheet Series, Secrets Management Cheat Sheet ↑
-
Agrawal et al., GEPA: Reflective Prompt Evolution Can Outperform Reinforcement Learning (arXiv, July 2025) ↑
-
OpenAI, Expanding on what we missed with sycophancy (2 May 2025) ↑
-
Anthropic, Demystifying evals for AI agents (9 January 2026) ↑