Skip to content
Flows
Tier 2 · Harness engineering Harness engineering · fetched 2026-07-02

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 GitHub

Key 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

From the archive

Verbatim excerpts mined from our local archive of this repository — the prompts, schemas, and patterns worth stealing.

MCP & tools

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

Agent pattern

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