Harness Engineering: Making a Probabilistic Model Safe for Production (with the Claude Agent SDK)
Most AI agent demos fall apart the moment they touch production. I spent the last few weeks figuring out why — and built a working harness on top of the Claude Agent SDK to fix it. Every layer below exists as runnable
Most AI agent demos fall apart the moment they touch production. I spent the last few weeks figuring out why — and built a working harness on top of the Claude Agent SDK to fix it.
Every layer below exists as runnable code. The SDK claims were verified against the shipped package (
claude-agent-sdk0.2.152), not written from memory.
The problem: a mismatch of kinds
Models are probabilistic. Production systems are deterministic.
A 90%-accurate model wired straight into your stack breaks it one time in ten — and you hear about it from your users, not your tests.
You don't fix this with a better prompt. You fix it with a harness.
Three eras
| Question | Controls | |
|---|---|---|
| Prompt engineering | How do I phrase the instruction? | Language |
| Context engineering | What knowledge does it see? | Knowledge |
| Harness engineering | How do we control execution? | Execution |
The core idea
You don't try to make the model reliable. You build a system that's reliable despite the model.
The model iterates in a sandbox against a real test suite until a deterministic checker says the work is correct — and only then does anything leave the box. The model's 90% becomes the search strategy; the harness supplies the guarantee.
An agent that tried four approaches, hit the loop guard, and stopped with "I'm blocked because the test fixture doesn't exist" has not failed. That's the harness working. The real failure is merging the plausible-looking wrong answer.
Which SDK? (this trips everyone up)
-
anthropic— HTTP client. Messages in, messages out. You write the tool loop yourself. -
claude-agent-sdk— wraps the Claude Code CLI over stdio and runs the agent loop for you: full toolset (Read, Write, Edit, Bash, Glob, Grep, WebSearch, WebFetch), sessions, permissions, hooks. CLI is bundled in the wheel.
Harness engineering controls an execution loop, so use the SDK that has one.
pip install claude-agent-sdk # Python 3.10+, CLI bundled
query() is one-shot. ClaudeSDKClient is a live session — and the only path that supports custom tools and hooks. A self-healing loop pushes a follow-up turn into a session that still holds context, so everything real uses ClaudeSDKClient.
The five stages of maturity
| Stage | What it looks like | Example |
|---|---|---|
| 1. Vibe execution | Prompting in a web UI. 100% manual review. | examples/01_bare_query.py |
| 2. Tool calling | Model reaches real systems. No sandbox, no loop prevention. | examples/02_custom_tools.py |
| 3. Deterministic harness | Sandbox, automated gates, RBAC, state. | examples/03_hooks_and_permissions.py |
| 4. Autonomous self-correction | Gate output feeds back; agent repairs its own work. | examples/04_full_harness.py |
| 5. The flywheel | Failures patch the environment. | harness/flywheel.py |
The jump that matters is 2 → 3. Stage 2 demos beautifully and is where most projects stall.
What's in the harness
- Routing — scout / worker / architect tiers picked by rules, not another LLM. No bootstrapping, no latency tax.
- Context mapping — a map, not an encyclopedia. Past a point, more context makes agents worse.
-
Sandboxing — path jail that blocks
../../etc/passwdand symlink escapes, plus an opt-in OS sandbox (seatbelt/macOS, bubblewrap/Linux). - Doom-loop prevention — catches the alternating read/test/read/test cycle, resets on real progress.
- Verification gates — format → lint → types → tests. The gate decides success, not the model.
- Human-in-the-loop — approval happens before the destructive tool runs, not after.
- Flywheel — failures patch the environment itself, so every future run gets better.
Runs behind FastAPI (SSE streaming) + a Streamlit console, with 13 progressive examples from bare prompting to a self-healing autonomous run.
Takeaway
If you're stuck at "great demo, can't ship it," the 2 → 3 jump — tool-calling to a deterministic harness — is the whole game.
Stop trying to make the model reliable. Build the system that's reliable despite it.
Code + 13 examples 👉 https://github.com/anjijava16/AgentHarness-claude-sdk
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.