English | 中文

Yù Yú: Decoding Agent Harness

A Deep Architectural Analysis of Claude Code


“Of all instruments, the chariot demands the most hands to build.”Kǎo Gōng Jì (Rites of Zhou, c. 300 BC)

In ancient China, the chariot was the most complex system ever engineered. The (舆, carriage) bears the rider; the shaft sets direction; the spokes transmit force; the linchpin constrains the wheel. Each part has its duty — only together can the vehicle move.

Today, building an AI Agent is no different. The conversation loop is the shaft, the tool system the spokes, the permission pipeline the linchpin, and the runtime framework that bears it all — the Agent Harness — is the .

This book is thus known as the “Yú Shū” (舆书, The Chariot Book).


While everyone else teaches you how to use AI Agents — This book dissects one.


Read Online

GitHub Stars
GitHub Forks
License
中文
English
Last Commit


Decoding Agent Harness — A Deep Architectural Analysis of Claude Code

How does the dialog loop drive execution? Why is the permission system a four-stage pipeline? How does context compression operate within token budgets? How do sub-agents inherit parent context through Fork?

Understand Claude Code’s design decisions, and you gain a mental model transferable to any Agent framework.


What Makes This Book Different

Not a usage tutorial. Not a list of prompt tricks.

The market is saturated with guides on “how to write better prompts” and “how to call Agent APIs.” But if you want to understand the skeleton of a production-grade Agent system — there’s almost nothing to consult. This book fills that gap.

FeatureDescription
Architecture Analysis, Not API DocsNot “how to call,” but “why designed this way” — tracing motivations, analyzing trade-offs, identifying anti-patterns
Design Philosophy, Not TutorialsFrom async generators to circuit breakers, every chapter distills transferable design principles
Transferable Cognitive ModelsWhether you use LangChain, AutoGen, CrewAI, or build from scratch — 139 architecture diagrams apply directly
By the Numbers
MetricCount
Total word count420K characters (Chinese) / 75K+ words (English)
Main chapters15 chapters + 4 appendices
Mermaid architecture/flow/state diagrams139
Core subsystems coveredTool system, permission pipeline, context compression, memory system, hook system, sub-agent dispatch, MCP integration, skill plugins, streaming architecture, Plan mode
Design decisions analyzed50+ “why designed this way”
Glossary terms (bilingual)100
Feature flags89
Registered tools50+

Disclaimer: This book is based on architectural analysis of Claude Code’s public documentation and product behavior. No unpublished or unauthorized source code was used. Claude Code is a product of Anthropic PBC. This book is not affiliated with, authorized by, or representative of Anthropic.


Quick Navigation

Short on time? 01 → 02 → 04 → 15 — get the core insights and hands-on skills

Experienced? Jump to Part 2 + Part 3, backtrack to Part 1 for concept gaps

Systematic study? Cover to cover with exercises, build your Harness in Ch15 (~2–3 weeks)

Just need reference? Go straight to Appendices — A (modules) / B (tools) / C (flags) / D (glossary)


Table of Contents

Part 1. Foundations — Building Mental Models

Understand the paradigm shift in Agent programming and establish a holistic cognitive framework.

#ChapterCore Content
01The New Paradigm of Agent ProgrammingCopilot → Claude Code evolution; five design principles; Bun + React/Ink + Zod v4 stack
02The Dialog Loop — Agent’s Heartbeatwhile(true) async generator loop; five yield events; ten termination reasons; QueryDeps DI
03The Tool System — Agent’s HandsTool<I,O,P> five-element protocol; fail-safe buildTool factory; 45+ tools × 12 categories; concurrent partitioning
04The Permission Pipeline — Agent’s GuardrailsFour-stage pipeline; five permission modes; Bash rule matching; speculative classifier 2s Promise.race

Part 2. Core Systems — Deep Into Subsystems

Dissect the four core subsystems — configuration, memory, context, and hooks.

#ChapterCore Content
05Settings & Configuration — Agent’s DNASix-layer config priority chain; merge rules; security boundary & supply chain defense; dual-layer feature gating
06The Memory System — Agent’s Long-Term MemoryFour closed memory types; “only save non-derivable info”; MEMORY.md index; Fork memory mechanism
07Context Management — Agent’s Working MemoryEffective window formula; four-level compression (Snip→MicroCompact→Collapse→AutoCompact); circuit breaker
08The Hook System — Agent’s Lifecycle Extension PointsFive hook types; 26 lifecycle events; JSON response protocol; six-layer priority; three-layer security

Part 3. Advanced Patterns — Composition & Extension

Explore how Agents compose, orchestrate, and extend — from sub-agents to MCP protocol bridging.

#ChapterCore Content
09Sub-Agents and the Fork PatternThree Agent sources; four built-in Agents; byte-level Fork context inheritance; recursive Fork protection
10The Coordinator Pattern — Multi-Agent OrchestrationCoordinator-Worker dual gating; “orchestrate-only” constraint; four addressing modes; four-stage workflow
11The Skill System & Plugin Architecture11 core skills; SKILL.md frontmatter; three-level parameter substitution; layered loading; plugin cache
12MCP Integration & External Protocols8 transport protocols; five-state connection management; three-part tool naming; Bridge bidirectional comms

Part 4. Engineering Practice — From Principles to Construction

Performance optimization details and a practical roadmap for building a complete Harness from scratch.

#ChapterCore Content
13Streaming Architecture & Performance OptimizationQueryEngine lifecycle; concurrency control; startup optimization 160ms→65ms (-59%); lazy loading
14Plan Mode & Structured Workflows”Think before you act” philosophy; plan file three-layer recovery; local scheduling & remote triggers
15Building Your Own Agent HarnessSix-step implementation roadmap; circular dependency solutions; four-layer observability; security threat model

Appendix — Reference Quick-Lookup

Content
AArchitecture Navigation Map — 16 core modules, dependency tree, 6 data flow paths, 10 design patterns
BComplete Tool Inventory — 50+ tools × 12 categories, readOnly/destructive/concurrencySafe attributes
CFeature Flag Reference — 89 flags × 13 categories, compile-time/runtime types, dependency graphs
DGlossary — 100 bilingual term definitions with cross-references and chapter locations

Who Is This For

ReaderWhat You’ll Gain
ArchitectsComplete Agent design space map and engineering trade-off analysis
Senior EngineersUnderlying mechanisms of tool invocation, streaming, and permission control
ResearchersPublishable-quality Agent system implementation analysis
Claude Code UsersUnderstand design intent and maximize capabilities

Background

On March 31, 2026, security researcher Chaofan Shou (@Fried_rice) discovered that the @anthropic-ai/claude-code package on npm contained a build configuration error where source map files referenced an unprotected Cloudflare R2 storage bucket. The disclosure tweet received over 17 million views, sparking unprecedented community discussion about Agent architecture.

This book was born from that discussion — when Agent architecture became a hot topic, we realized the need for a systematic book explaining the design principles of Agent Harness.


Contributing

Issues and PRs welcome — fix technical errors, supplement practical examples, improve chapter structure.

Acknowledgments

Linux.Do community


Star History

Star History Chart


CC BY-NC-SA 4.0

Free to share and adapt, with attribution, non-commercial use, and same-license sharing.