Agent Harnesses and Runtime Structure
About 40 min
Goal
By the end of this lesson, you can explain the jobs of an agent harness and separate model proposals, controller state, tool execution, and durable records into clear runtime boundaries.
What the harness adds
Level 11 gave the agent a bounded loop. That is enough to reason about one run, but a real application needs more structure around that loop. A process can restart, a tool can time out, a user can return later, and an operator may need to inspect what happened. The agent harness is the application layer that keeps those concerns organized.
A useful harness has several distinct responsibilities. The model proposes a next action. The controller checks whether that action is legal. Tool adapters perform external work. A durable store records task state that must survive a process restart. A trace recorder records observable events. Keeping these roles separate makes failures easier to locate because each boundary has a specific job.
Consider a refund agent. The model may propose refund_order with an amount. The controller checks the user role and approval. The tool adapter sends the request. The durable task record stores whether the refund attempt started, completed, or needs reconciliation—checking durable or external evidence to determine what actually happened before deciding whether to repeat the operation. If all of those responsibilities are hidden inside one large function, a timeout can leave the program unable to tell whether the refund happened.
user task → model proposal → policy check → tool adapter → durable result
↓
trace
Keep recovery state outside the process
Temporary process memory and durable state are not the same thing. A local variable is useful while the process is alive. Durable state is information the system must recover later, such as task ID, current state, completed effect IDs, approval status, and retry count. If losing the process would make the information unsafe to reconstruct by guessing, that information belongs in durable state.
The harness also defines an execution boundary. Model output is data entering the controller, not executable authority. Tool results are observations entering state, not automatic permission to perform a new side effect. This is the same principle from Level 11, now arranged as a larger runtime structure.
One tempting design is a single agent function that calls the model, executes whatever tool it names, catches every exception, and loops. It looks simple, but it makes durability, testing, and recovery difficult. A smaller set of explicit components is easier to replay, substitute in tests, and inspect when a run stops unexpectedly.
Build a small explicit runtime
A practical harness can still be small. You might have a TaskRecord, a Policy, a ToolRegistry, an Executor, and a TraceSink. The exact class names are not important. What matters is that state ownership and side effects are visible. The controller should be able to answer what task is running, what operation is next, and what evidence justifies continuing.
Observability belongs beside execution rather than being added only after a failure. When the harness starts a task, chooses an action, calls a tool, retries, blocks for approval, or stops, it should emit structured events. These events will become important in L12.4 when you learn tracing in more detail.
Runtime structure is therefore not a framework brand or a special kind of model. It is an engineering arrangement. You can implement the same boundaries with simple Python, a workflow engine, or a larger distributed system. The learning target is the separation of responsibilities, because that survives changes in libraries and providers.
Predict
Run the local Lab
Run:
python labs/notebooks/level-12/l12-01-harness-structure.py
The script moves one task record through four steps: proposal, policy, execute, and complete. Only execute performs a real side effect. Partway through, the script simulates a controller crash: controller-local state is reconstructed only from the saved task record. A small external_system object stands in for the world outside the controller. It records which operation IDs have already been applied, plus an effect_count, so it is deliberately not reset by the controller restart.
- Run it unchanged.
RESTART_AFTER = "policy", so the saved record has'state': 'AUTHORIZED'and'operation_id': None. - Read
resume from: execute. The controller looks at the savedstateto decide which step comes next. The run then finishes witheffect_count: 1. - Open the script and change
RESTART_AFTER = "policy"toRESTART_AFTER = "execute". Rerun. - Now the saved record already has
'state': 'EXECUTED'and'operation_id': 'read-order-4172', and the output saysresume from: complete.effect_count:is still1, so the side effect did not run twice.
The script also checks a harder retry edge case: imagine the external system already applied read-order-4172, but the process crashed before that operation ID was written back to the durable task record. The saved record therefore still says AUTHORIZED with operation_id: None. Retrying execute must deterministically reconstruct read-order-4172, recognize that the external system already applied it, avoid a second external effect, and still advance the durable record to EXECUTED.
Answer this before moving on: which saved field tells the controller where to resume, and why must the controller be able to reconstruct the same operation ID after a crash before the durable write completes?
Loading lab…
Quick Check
Explain it back
Describe a four-part harness for a payment-support agent: model proposal, controller, tool adapter, and durable task record. State one fact each part should own and one fact it should not own.
Check your explanation
A strong answer mentions:
- Model: proposes a payment-support step, but cannot own permission/approval status.
- Controller: owns allowed transitions and action authority, not the model's unverified interpretation.
- Tool adapter: executes a permitted side effect and returns observations, but cannot grant new permissions.
- Durable task record: owns state, IDs, and recovery evidence that survive worker failure; it must not treat free-form model text as authoritative state.
Key Takeaways
- A harness organizes control around the agent loop.
- Durable state must survive process loss.
- Model proposals are not execution authority.
- Tool adapters isolate side effects from decision logic.
- Structured events should be emitted as part of normal execution.
Next Lesson
Next, L12.2 — Task Queues and Durable State explains how work can wait, resume, and survive a worker restart.
References
- OpenTelemetry, Specifications.
- Yao et al., ReAct: Synergizing Reasoning and Acting in Language Models.
Completion is stored locally on this device.