1. How to use this book

Why this book exists

You already run agents. You have a Hermes config with roles for primary, fallback, and aux models; you have llama-server processes on straylight and dixie with tuned slot counts and quantized KV caches; you have watched Claude Code compact a session and lose the one fact that mattered. What you do not yet have is an agent where you can answer “why did it do that?” at every layer: why this token was sampled, why the tool call parsed, why the memory recall surfaced that fact and not another, why the policy engine let the request through.

The end state is your own persistent agent daemon, aecd, a peer to Hermes or OpenClaw rather than a wrapper around one. The rule that shapes everything else: nothing in the capstone is a black box. Every component aecd depends on has a chapter in which you build a working version of it. Where your version is a toy (a transformer trained on one CPU is not going to replace a 35B model on the main loop), you build the toy far enough to understand the real thing, then swap in the real thing and measure the difference. Where your version is viable (the vector index, the memory service, the policy engine, and the router on a host that has none of its own), the capstone runs your code.

The stack this book builds Read from the bottom up: Part I, Mathematics; Part II, Language model; Part III, Serving; Part IV, Agent loop; Part V, Context and memory; Part VII, Daemon, with Part VIII, Security, beside it; Part IX, Capstone; Part X, At work, on top. Part VI, Evaluation, runs up the right-hand side beside Parts IV through X. X · At work specs, review loops, factory gates, aec-work IX · Capstone tracing, tool-use fine-tune, aecd on NixOS VII · Daemon aecd, gateways, cron, sandbox, MCP, subagents VIII · Security injection corpus, policy, audit log V · Context and memory token budget, compaction, HNSW index, memsvc, skills IV · Agent loop LLM client, tool calls, provider routing III · Serving HTTP API, constrained decoding, batching, aec-router II · Language model BPE tokenizer, transformer, KV cache, quantization I · Mathematics vectors, matrices, probability, calculus, autodiff VI · Evaluation model matrix, calibrated judge

The stack builds bottom-up. Part I gives you the math every later layer uses. Part II turns that math into a language model you train yourself. Part III puts a model behind an HTTP server and a router, which is the point where the Python work hands off to Go. Parts IV and V build the agent proper: the loop, its tools, and the context and memory systems that keep it coherent across turns and sessions. Part VII turns the agent into a long-running daemon, and Part VIII puts a security layer beside it, because a daemon that reads your chat channels and executes code is an attack surface first and an assistant second. Part VI runs alongside everything from Part IV up: evaluation is how you decide whether a change to any of those layers made things better, and it is what picks the capstone’s models. Part IX assembles the pieces, and Part X puts aecd to work on a repository (a committed fixture that stands in for yours; running it on your own is your step): specs, review loops and gated pipelines.

How the book is built

Two codebases

The reference code lives in two trees in this repository.

  • py/tinygpt is a Python package that grows through chapters 2–21, with one later addition in chapter 50: vector and matrix helpers, a probability module, an autograd engine, a BPE tokenizer, bigram, MLP, transformer and mixture-of-experts models, a sampler with a KV cache, a chat fine-tune, quantization, speculative decoding, encoder, state-space, diffusion and vision models, an OpenAI-compatible inference server with constrained decoding and batching, calibration, and decision models; chapter 50 adds lora.py, LoRA and NF4 quantization as numpy code. Its only runtime dependency is numpy. It is managed by uv from py/pyproject.toml. Chapter 50’s real training run lives beside it in py/finetune/; it needs PyTorch and PEFT from the finetune dependency group, which make check never installs.
  • go/ is the Go module aec, which grows through chapters 22–55: cmd/aec-router, cmd/aec (the agent CLI), cmd/aecd (the daemon) and cmd/aec-work (Part X’s work loop and factory, a client of the daemon), with tools such as aec-doctor, aec-eval and aec-stats beside them, and shared packages under go/internal/ for the router and its proxy, the LLM client, the decision-model runtime, tools, the loop, context management, vecdb, memsvc, skills, evals, gateways, the scheduler, the sandbox, MCP, policy, audit, observability, and Part X’s specs, loop and gates. It is stdlib-first; any third-party dependency gets a justification in DECISIONS.md.

The split falls where the work changes character. Chapters 2–16 are numerical and exploratory, and numpy with a REPL is the right tool. Chapters 17–21 stay in Python because they build on what Parts I–II taught: the inference server, constrained decoding and batching wrap tinygpt’s models, and calibration and decision models are numpy work of the same kind. From chapter 22, the router, the work is long-lived networked processes with concurrency, cancellation, and supervision, which is what Go is for. Chapter 50 returns to Python once, because the training libraries are Python’s and the derivations it checks need a transformer with autograd.

Part Chapters Codebase What exists at the end
0 1 go/ make doctor, make check
I 2–6 py/tinygpt vectors, matrices, probability, gradient descent, an autograd engine
II 7–16 py/tinygpt BPE, transformer and MoE, sampler, KV cache, chat tuning, quantization, speculation, encoder/SSM/diffusion/vision models
III 17–23 py/tinygpt, then go/ OpenAI-compatible server, constrained decoding, batching, calibration, decision models; aec-router, its proxy and metrics
IV 24–28 go/ internal/llm client, tool-call decoder, the agent loop cmd/aec, provider routing
V 29–36 go/ context budget, compaction, internal/vecdb (HNSW), internal/memsvc, skills
VI 37–39 go/ internal/eval, a model-selection matrix with confidence intervals, a calibrated judge
VII 40–45 go/ aecd, a Mattermost gateway, cron and heartbeats, sandboxed code execution, MCP, subagents
VIII 46–48 go/ injection corpus, internal/policy, hash-chained audit log
IX 49–51 go/, py/, nix/ tracing and cost accounting, a tool-use fine-tune, aecd as a NixOS module
X 52–55 go/ aecd at work on a repository (a committed fixture; yours is your step): specs, review loops, gated pipelines

Listings cannot drift

Every code listing in a chapter is a copy of a marked region of a real source file that compiles and passes its tests. In the source file, the region is bracketed by comment markers: # listing:NAME and # end-listing:NAME in Python, // listing:NAME and // end-listing:NAME in Go. In the chapter, a directive of the form <!-- listing: PATH#NAME --> sits on the line immediately before a fenced code block, where PATH is relative to the repository root.

tools/listings is a small Go program that walks every Markdown file under book/, finds each directive, extracts the named region from the source file (dropping marker lines and removing common indentation), and compares it byte-for-byte with the fenced block. make listings runs it in check mode: any mismatch is reported as FILE:LINE: listing PATH#NAME is out of date and the command exits non-zero. make sync rewrites the stale blocks from source. The same run also checks that every chapter linked from SUMMARY.md exists.

A listing in this book is never a paraphrase of the reference code, and never a version of it from three refactors ago. If the listing and the test suite disagree, make check fails before the book builds. When you read a listing, you are reading the code the tests ran against.

The chapter template

Every chapter after this one has the same six sections, in order:

  1. Why this layer exists. What breaks without it, and where you have already met it: a llama-server flag, a Hermes config key, a Claude Code behaviour.
  2. Mechanism. The idea, with the math derived at the point it is used, and every tensor’s shape stated.
  3. Walkthrough. Listings from the reference source, with commentary on the decisions that are not obvious from the code.
  4. Exercises. Short problems. Answers are in collapsed blocks; open one after you have an answer of your own.
  5. Lab. A build task with starter files and acceptance tests.
  6. Further reading. Primary sources (papers, specifications, and code or documentation at a pinned commit), each cited by title and year and linked to its canonical source: a DOI, an arXiv page, the proceedings, or a repository at a pinned commit.

Labs

Python labs live in py/labs/chNN/, one directory per chapter. Each has at least these three kinds of file:

  • __init__.py sets REF_MODULE, the dotted name of the reference module the lab corresponds to (for chapter 2, a module under tinygpt).
  • starter.py is yours. It declares the same public names as the reference module, with identical function signatures, and every body you are meant to write raises NotImplementedError. A starter may also ship code the chapter does not ask you to write: chapter 6’s comes with Tensor’s constructor and shape properties and a complete gradcheck, which a test keeps identical to the reference. A test in py/tests/test_starters.py enforces that the starter’s public API matches the reference, so the stubs are always a faithful outline of what you need to write.
  • test_lab.py holds the acceptance tests. Each test takes an impl fixture and calls functions on it, never importing an implementation directly. Chapter 21 splits its tests across several test_*.py files (chapter 20’s is test_scoring.py), and chapters 15, 20 and 21 point REF_MODULE at a reference.py beside the starter.

The impl fixture, defined in py/labs/conftest.py, reads the AEC_IMPL environment variable. With ref it returns the reference module; with mine it returns your starter.py. The Makefile sets AEC_IMPL from IMPL:

make lab CH=02 IMPL=mine   # your starter.py
make lab CH=02 IMPL=ref    # the reference implementation
make lab CH=02             # IMPL defaults to ref

Run IMPL=ref once first. It should pass, and it proves the test harness works on your machine, so that when IMPL=mine fails you know the failure is in your code. Then edit starter.py until IMPL=mine passes too.

The tests are the specification. The chapter explains the mechanism; the tests define exactly what your implementation must do, including edge cases the prose does not dwell on. When the chapter and a test seem to disagree, read the test: it is the part of the contract that is executed. Read the lab’s tests before you start writing, the way you would read an RFC’s MUST clauses before implementing a protocol.

The reference implementation is in the same repository, one directory over, and nothing stops you from opening it. Opening it first defeats the lab. The point of the lab is the hour in which your attention layer produces the wrong shape and you work out why; reading the answer converts that hour into a minute of recognition that does not stick. Read the reference after your version passes, and diff the two.

Go labs from Part III on follow the same pattern under go/labs/chNN/; their chapters describe the mechanics.

Environment

The toolchain is pinned in two places. mise.toml at the repository root pins Go 1.27.1, Python 3.14, Node 26 and the latest uv. make check also calls pandoc (the tests of the ebook’s Lua filter) and, on Linux, bwrap (chapter 43’s sandbox tests skip their bubblewrap rows without it), and mise installs neither. The flake’s dev shell (nix develop, pinned by flake.lock) carries all of them, and the gate counts only when it runs there. Outside Nix, install pandoc and bubblewrap from your distribution before make check. From a fresh clone:

mise install          # installs the pinned go, python, node, uv
cd py && uv sync      # creates py/.venv with numpy and pytest
cd ..
make book             # runs npm install on first use, then builds the book into _site/
make doctor           # toolchain and endpoint check
make check            # everything: Python, Go and JS tests, ebook tooling, listings, book build

make serve runs HonKit’s development server with live reload on port 4000; use it while reading, since the math renders through the KaTeX plugin and does not display properly in a plain Markdown viewer.

What make doctor reports

make doctor runs go/cmd/aec-doctor, which prints one line per check. On talos (an Intel i3-1315U laptop), with everything reachable, the output looks like this:

ok   go         go version go1.27.1 linux/amd64
ok   uv         uv 0.12.19 (x86_64-unknown-linux-gnu)
ok   node       v26.8.2
ok   straylight http://straylight:11434/v1 reachable, HTTP 200
ok   dixie      http://dixie:11434/v1 reachable, HTTP 200
ok   z.ai       https://api.z.ai/api/coding/paas/v4 reachable, HTTP 200

The first three lines are tool checks. Each runs the tool’s version command and prints the first line of its output. These are required: if any of them is missing, its line reads FAIL and the command exits 1, because nothing in the course works without them.

The last three are endpoint checks. Each sends GET {base}/models to an OpenAI-compatible base URL with a 3-second timeout. Any HTTP response at all counts as reachable, because the question being answered is “can this machine talk to that server”, not “is the model loaded”. A 401 or 403 is still ok but gets (check the API key) appended; for z.ai that means ZAI_API_KEY is unset or wrong. A connection failure, DNS failure, or timeout prints warn with the underlying error. With AEC_DIXIE_URL=http://nonexistent-host.invalid:11434/v1, the dixie line reads:

warn dixie      Get "http://nonexistent-host.invalid:11434/v1/models": dial tcp: lookup nonexistent-host.invalid: no such host

Endpoint checks never fail the command. You can work through Parts I and II on a plane: no lab or test before Part III needs a live model, and every test in the repository runs offline by default. Warnings matter when you reach the chapters that use those endpoints.

The base URLs come from AEC_STRAYLIGHT_URL, AEC_DIXIE_URL, and AEC_ZAI_URL, with the defaults shown above. The z.ai key comes from ZAI_API_KEY and is sent as a bearer token. The key is read only from the environment and is never committed.

What make check runs

make check depends on six targets, and fails if any of them fails:

  • test-py: uv run pytest -q in py/: the unit tests under py/tests/ (the starter-consistency test among them) and every Python lab against the reference.
  • test-go: go vet ./... and go test ./... in go/, tools/listings/ and tools/theme/ (the generator of the site’s colour themes).
  • test-js: node --test for the HonKit plugin in tools/gitbook-plugin-aec/, which inlines the figures and curls the quotation marks.
  • test-ebook: the tooling of the offline editions in tools/ebook/: its node tests, which cover the baking of figures for print, and the tests of its Pandoc Lua filter.
  • listings: the drift check described above.
  • book: a full HonKit build.

It is the gate at every part boundary. If it is red, fix it before moving on; a listing that has drifted from its source or a lab whose reference no longer passes means the book is lying about something.

Infrastructure the course targets

The course is written against a specific set of machines on your tailnet:

Role in course Target
Default lab model (main loop) straylight:11434, ornith-1.5-35b-a3b (OpenAI-compatible, Vulkan)
Small / aux model, embeddings dixie:11434, ornith-1.5-9b-uncensored, honcho-embed (Qwen3-Embedding-0.6B)
Cloud comparison z.ai, glm-5.3-flash (OpenAI-compatible)
Additional eval candidates trinity-mini and other catalog models on straylight
Fine-tuning (QLoRA) dixie, RTX 3060 12 GB
Web tools sift on orion (/v1/search, /v1/fetch)
Reference memory service Honcho (on rift): chapter 35 compares against its source and documentation; nothing in the book calls it
Gateway transport Mattermost (tailnet HTTPS)
Capstone deploy host an always-on host (straylight); never titan

Labs default to a local model on the main loop. An agent lab runs the loop many times, often with deliberately broken tools or injected failures, and a local model makes that iteration free at the margin and keeps every request on hardware you control. It is also a working assumption, not a conclusion: which model carries the capstone’s main loop is decided by the Part VI eval matrix, which runs the local models and z.ai against the same tasks and reports the differences with confidence intervals. If glm-5.3-flash wins on the tasks you care about by more than the noise, the capstone uses it.

Always address machines by MagicDNS name (straylight, dixie, rift, orion, or the fully qualified *.ts.net form), never by tailnet IP. IPs change when a node is re-registered; names do not, and a name in a config file or a test fixture tells the next reader what it points at. Two laptops appear beside the fleet: talos (an Intel i3-1315U), the working machine through Part VI, and wntrmute, which replaced it during Part VII (2026-10-13) and serves its own llama.cpp router on 127.0.0.1:8080. Captures from Part VII on, and a few re-runs in earlier chapters, ran on wntrmute against that router, and every capture names its machine.

Tests that talk to live endpoints are opt-in. From Part III on, AEC_LIVE names the one backend a live test may reach: straylight, dixie or zai; in chapters 41, 46 and 47 also wntrmute (that laptop’s own router on loopback), and in chapter 41 mattermost (the live gateway step). Without it, the same tests run against scripted fakes and recorded fixtures. Once dependencies are installed, make check never needs the network.

Reading the math

The notation is fixed for the whole book.

  • Scalars are italic lowercase: xx, η\eta, pp. A single entry of a vector or matrix is a scalar and is written the same way with indices: xix_i is entry ii of x\mathbf{x}, and WijW_{ij} is row ii, column jj of W\mathbf{W}.
  • Vectors are bold lowercase: x\mathbf{x}, q\mathbf{q}, h\mathbf{h}. Greek-letter vectors such as θ\theta stay italic and not bold (the book’s KaTeX cannot bold them); their shape is stated where they are introduced.
  • Matrices are bold uppercase: W\mathbf{W}, Q\mathbf{Q}, K\mathbf{K}. Tensors with more than two axes use the same style.
  • Shapes are written the way numpy prints them: (n, d) is a matrix with nn rows and dd columns, (d,) is a vector of length dd, and (B, T, d) is a batch of BB sequences of TT positions with dd features each. The mathematical form X∈Rn×d\mathbf{X} \in \mathbb{R}^{n \times d} means the same thing as shape (n, d); the book uses whichever reads better in context.
  • log\log is the natural logarithm, base ee. Where another base matters (entropy in bits, for instance), it is written explicitly as log2\log_2.

Every tensor expression is followed by its shape. If X\mathbf{X} has shape (n, d) and W\mathbf{W} has shape (d, k), then

Y=XW \mathbf{Y} = \mathbf{X}\mathbf{W}

has shape (n, k): the inner dimensions dd must match and are summed over, and the outer ones survive. A stated shape turns a shape bug into a mismatch you can see on the page, before the code raises it at runtime or, worse, broadcasts silently into the wrong answer.

Derivations do not skip steps. When a chapter needs a result, it starts from the intuition (what the quantity measures, or what the geometry looks like), then derives the equation one line at a time, then shows the code that computes it. If a step looks like it was skipped, it is a mistake in the book.

Lab

Both commands must succeed before you start chapter 2. Run them from the repository root:

make doctor
make check

make doctor must exit 0: all three tool lines ok. Endpoint lines may be warn if you are off the tailnet; that is fine for Parts I and II. If a tool line reads FAIL, run mise install again and check that mise’s shims are on your PATH (mise doctor will say if they are not).

make check must finish without errors. test-py runs the starter-consistency test and every lab’s tests against the reference; all must pass. test-go runs every Go package’s tests and every Go lab against the reference (chapters 22–55), among them the aec-doctor tests, which start local HTTP servers to exercise the reachable, unauthorized, unreachable and timeout cases without any network access, and the tests of the listing tool and the theme generator. test-js and test-ebook test the book’s own build tooling. listings checks every listing in the book and every SUMMARY.md link. book builds the site into _site/. Run make check inside nix develop. Outside the shell, test-ebook fails if pandoc is missing, and chapter 43’s bubblewrap rows skip.

results matching ""

    No results matching ""