AiGENTiA InsightsAgent Engineering
Prompt / Context / Loop / Harness Engineering
30 August 2026
Four distinct disciplines get collapsed into the word prompting, and that collapse is why so much agent work fails in ways nobody can diagnose.
In 2023, a team of researchers from Stanford and elsewhere ran a simple test on language models. They asked a question and gave the model twenty documents, only one of which held the answer. Then they moved that one document around: first in the pile, last, or somewhere in the middle.
When the answer sat at the beginning or the end, the models did well. When it sat in the middle, accuracy dropped sharply. For one widely used model, it dropped below the score the same model got with no documents at all. Giving it the answer, in the wrong place, was worse than giving it nothing.
The paper was called “Lost in the Middle”, and it explains a scene that repeats in almost every company building AI agents. An agent fails. The team rewrites the prompt. The agent fails differently. The team rewrites the prompt again. Nobody asks whether the problem was ever in the prompt. So the question here is: when an agent fails, how do you know which part of the system to fix?
One word, four jobs
The word prompting has come to mean everything a developer does with a language model. In practice it covers four different jobs, at four different layers, and each one fails in its own way.
A theatre is a helpful picture. An actor on stage needs four things. A script that says what the character wants and how they speak. The right props on stage at the right moment, and nothing that is in the way. A stage manager who calls the cues, decides when a scene ends, and knows what to do when something goes wrong. And the theatre itself: lights, doors, a working stage, a fire exit.
Map that onto an agent:
- Prompt engineering is the script: instructions, role, examples and output format. It answers one question — does the model understand the task?
- Context engineering is the props: what information the model can see at each step, how much of it, and in what order.
- Loop engineering is the stage manager: the code around the model that decides how many steps to take, when to stop, and how to recover from an error.
- Harness engineering is the building: the sandbox the agent runs in, its tools, its files, its permissions, and the way results are reported back to it.
The analogy has a limit. A good actor will improvise around a missing prop and finish the scene. A model will also improvise — but it may invent the prop, and say so with confidence. That is why each layer has to be designed, not hoped for.
The script: necessary, and limited
Prompts matter. Clear instructions, good examples and a defined output format make a real difference, and studies of prompt sensitivity show that small changes in wording can change a model’s answers more than you would expect.
But a script cannot fix a broken stage. If the agent fails because a tool returned an error code the model could not interpret, or because its context is full of thousands of lines of old terminal output, rewriting the instructions changes nothing that matters. It may change the output enough to pass one test, which is worse, because it hides the real fault.
The props: more is not better
Bigger context windows tempted many teams to give the model everything. “Lost in the Middle” is the reason that fails. Models do not attend evenly to everything they are given. In the same study, retrieving 50 documents instead of 20 improved accuracy by only about one to one and a half percentage points, while the model had to read more than twice as much text.
Context engineering is the discipline of choosing what the model sees: removing stale output, summarising long histories, placing the most important information where the model is most likely to use it, and keeping the working set small. Anthropic’s engineering guidance on context makes the same point in practical terms: an agent rarely needs its whole history to take its next step; it needs a clean, current picture of the situation.
The SWE-agent project at Princeton showed this for coding agents in 2024. Instead of giving a model a raw terminal, the researchers built a simpler interface designed for the model — commands that show a short, focused window of a file, and outputs trimmed of noise. The same models solved noticeably more real software issues through that interface than through a plain shell.
The stage manager: knowing when to stop
Loop engineering lives outside the model, in ordinary code. It decides how many steps an agent may take, what counts as finished, what happens after an error, and when to hand the problem to a person.
Without it, agents fail in a characteristic way: they repeat. A tool call fails, the model tries the same call again, it fails again, and nothing in the system notices. The fixes are old and unglamorous — step limits, retry limits with increasing waits between attempts, detection of repeated identical calls, and a hard stop on spending. They are the same fixes engineers use for any distributed system, and they work for the same reasons.
There is a real debate about how much freedom to give the model. Some builders prefer open-ended loops in which the model plans everything. Anthropic’s 2024 guide to building agents recommends starting with the simplest structure that works, using fixed workflows where the task is predictable, and adding autonomy only where it clearly helps. In practice, most reliable systems put the model’s reasoning inside a structure that ordinary code controls.
The building: the ceiling on what is possible
The harness is the environment the agent works in: the sandbox, the file system, the tools, the tests, and the way errors are reported. It sets the ceiling on what any model can achieve inside it.
Evidence for this is accumulating. LangChain has reported that changing only the harness around a fixed model on the Terminal Bench 2.0 benchmark raised its task success rate from about 53% to about 67%. Anthropic has described a harness for long-running coding agents in which one agent prepares the workspace and a written list of features, and a second agent completes one feature per session, runs the tests, commits the work and records its progress in a file before stopping. The file, not the model’s memory, carries the project from one session to the next.
The lesson in both cases is the same: the model was not changed. The building was.
A diagnosis, layer by layer
Here is a hypothetical but typical failure. A coding agent is asked to fix a bug. It sends the same malformed edit command fifteen times, then stops with an empty answer. Where is the fault?
- Script: the instructions never showed a valid example of the edit command. Fix the prompt.
- Props: the error message that explained the problem was buried in the middle of a very long context. Trim and reorder the context.
- Stage manager: nothing noticed fifteen identical calls in a row. Add repeat detection and a retry limit.
- Building: the sandbox passed back a bare error code instead of a readable message. Wrap tool errors in plain, structured feedback.
Any one of these could cause the failure. Often several do at once. A team that only knows how to rewrite prompts will fix the first and leave the other three in place, and the agent will fail again the next time the context grows.
What this framework does not settle
These four layers are a way of thinking, not a law of nature. The boundaries blur: a summary of earlier steps is both context and loop design. Better models also reduce the need for some of this scaffolding; tasks that needed careful harnesses two years ago are easier now. And several of the strongest numbers in this field come from company blogs about their own systems, which are useful but not independent.
Still, the Stanford result from the opening has held up: what a model is given, and where, changes what it can do. The answer was in the pile all along. The model just could not find it in the middle — and no rewrite of the question would have helped.
Sources
- Liu et al. (2023), Lost in the Middle: how language models use long contexts
- Yang et al. (2024), SWE-agent: agent-computer interfaces enable automated software engineering
- Anthropic (2024), Building effective agents
- Anthropic, Effective context engineering for AI agents
- Anthropic, Effective harnesses for long-running agents
- LangChain, Improving deep agents with harness engineering