Harness Engineering: from CC to AI coding
A full book on harness engineering ('from Claude Code source code to AI coding') with Chinese and English editions in book/ and book-en/. The single best conceptual source in this corpus: chapters on prompt assembly, tool design, context management, verification loops, and sub-agent patterns.
View source on GitHubKey takeaways
- 01
The harness - not the model - is where coding-agent quality is won
- 02
Chapters map 1:1 to buildable subsystems: prompts, tools, context, verification
- 03
Verification loops are treated as non-negotiable, not optional polish
Flows built on this research
Harness Engineering
Verification Loops: Never Trust an Unrun Edit
Wire verification into your agent so every edit is checked by machines - syntax, tests, behavior - before it counts as done.
4 steps · 60-100 minutes
Agent Architecture
Error Recovery and Retry Policy for the Agent Loop
Design the retry, backoff, and give-up behavior that separates production agents from demos that loop forever.
4 steps · 60-90 minutes
Harness Engineering
Cost Control: Model Routing and Effort Tiers
Route work to the cheapest model that can do it and match reasoning effort to task difficulty - with quality guardrails.
4 steps · 90-120 minutes
Agent Architecture
From CLI Harness to Product
Grow a working agent loop into a product: sessions, config, resumability, and the packaging polish the successful terminal agents share.
5 steps · 90-150 minutes
Agent Architecture
Assemble a System Prompt Like the Pros
Structure your agent's system prompt the way leading harnesses do: layered sections, dynamic context, and enforceable rules.
4 steps · 60-90 minutes
From the archive
Verbatim excerpts mined from our local archive of this repository — the prompts, schemas, and patterns worth stealing.
A code-review agent exposed as an MCP tool
/// Review a unified diff file and return structured findings.
#[tool(description = "Review a unified diff file for bugs, security issues, and code quality. Returns structured JSON findings with file, line, severity, and suggestions.")]
async fn review_diff(
&self,
Parameters(req): Parameters<ReviewDiffRequest>,
) -> String { ... }
pub struct ReviewDiffRequest {
#[schemars(description = "Absolute path to a unified diff file (from git diff)")]
pub diff_path: String,
#[schemars(description = "Maximum total tokens across all files (default: 50000)")]
pub max_tokens: Option<usize>,
#[schemars(description = "Maximum tokens per file (default: 5000)")]
pub max_file_tokens: Option<usize>,
} From the harness-engineering book's companion code: an agent that is itself a tool - Claude Code can call review_diff on its own output, with token budgets in the schema.
examples/code-review-agent/src/mcp.rs
Explicit token budgets with conservative estimation
pub struct ContextBudget {
pub max_total_tokens: usize,
pub max_file_tokens: usize,
pub used_tokens: usize,
}
/// Try to consume tokens. Returns true if within budget, false otherwise.
pub fn try_consume(&mut self, tokens: usize) -> bool { ... }
/// Estimate token count from text using a conservative bytes/4 heuristic.
/// Uses byte length, not character count. For multi-byte UTF-8 content
/// (e.g., CJK), this overestimates, which is intentionally conservative.
pub fn estimate_tokens(text: &str) -> usize {
(text.len() + 3) / 4
} Context management as an accounting problem: a hard budget object with try_consume semantics, and an estimator that deliberately overestimates rather than risks overflow.
examples/code-review-agent/src/context.rs