Phase 0: Agent Harness · ~90 minutes · Python (stdlib)
Verification Gates and the Observation Budget
An agent harness without a verification layer is a wish in a trenchcoat.
What you will learn
- Build a `VerificationGate` protocol with a deterministic `evaluate(call)` method.
- Compose budget, recency, whitelist, and regex gates into a chain with short-circuit semantics.
- Track every observation through an `ObservationLedger` keyed by tool and turn.
- Refuse a tool call when the cumulative observation budget would be exceeded.
- Surface a structured `GateDecision` record that downstream observability can ingest.
Introduction
This lesson builds the deterministic gate chain that decides whether a tool call is allowed to fire, how much of its output the agent is allowed to see, and when the loop has to stop because the agent has read too much. The chain is a function of small, named gates plus an observation ledger that tracks every token the model has been shown.
- Type: Build
- Languages: Python (stdlib)
- Prerequisites: Agent Harness Loop Contract, Instructions as constraints, Scope contracts, Verification gates
- Time: ~90 minutes
Objective
Learning objectives
- Build a
VerificationGate protocol with a deterministic evaluate(call) method. - Compose budget, recency, whitelist, and regex gates into a chain with short-circuit semantics.
- Track every observation through an
ObservationLedger keyed by tool and turn. - Refuse a tool call when the cumulative observation budget would be exceeded.
- Surface a structured
GateDecision record that downstream observability can ingest.
The Problem
When an agent harness lets the model call tools freely, three classes of bug appear within the first hour of real use.
The first is unbounded observation. A grep across a 200K-line repo dumps half a million tokens of output into the next turn. The model sees one match per kilobyte and the rest of the context is wasted. The token bill is large and the agent is now worse, not better, at the task.
The second is stale recency. A long-running task accumulates fifty tool calls. The model rereads the first read_file from turn three as if it were live state. Edits made on turn forty-seven never show up because the prompt builder serialized the earliest observations first.
The third is privilege creep. A research task starts by calling web_search, then somehow ends up running shell because the model invented a tool name and the harness defaulted to permissive. By the time anyone reads the trace, a junk file is sitting in /tmp and a curl ran against a private API.
A verification gate is the harness component that says no. It is not a model. It is not a judge. It is a deterministic function of (call, history, ledger) that returns either ALLOW or DENY with a reason. The reason is logged. The model is told. The loop continues or aborts.
The Concept
A gate is anything with an evaluate(call, ctx) -> GateDecision method. The chain is an ordered list. Evaluation short-circuits on the first deny. Order matters: cheap structural gates run before expensive token-counting gates.
This lesson ships four gates:
WhitelistGate. Allowed tool names are an explicit set. Anything outside is denied. This is the cheapest gate and runs first.RegexGate. Tool arguments are matched against a regex. Useful for refusing shell calls with rm -rf in them, or HTTP calls to internal IPs. Pure on the call payload.RecencyGate. The model only sees observations from the last N turns. Older observations are masked. The gate refuses a tool call whose result would extend an observation window that has already aged out.BudgetGate. The cumulative tokens the model has read across the session has a ceiling. When the ledger says the ceiling is reached, every further tool call is denied.
The observation ledger is the bookkeeping. Every successful tool call writes one row: tool name, turn, tokens emitted, cumulative. The ledger answers two questions: how much has the model seen total, and how much has it seen of tool X. The budget gate reads the first. A per-tool budget gate, which you will write as an exercise, reads the second.
This lesson is free
You've read the first 4 sections. The rest covers Build, Check Yourself, Key Terms & Next. A free account opens all of it, with its practice questions. No card.
Create a free account — the opening phases of 29 of 35 courses, and the first lesson of every phase, are free, no credit card.
Browse all courses · View pricing · DeVenture Academy