Section

Run it

Every chapter’s program from the companion repository, with what it shows, its size and test count computed from the code, and the command that runs it offline from recorded model replies.

  1. ch01: A scripted agent loop Run a complete agent loop that fixes a failing Go test, driven by a script of replies recorded from a real model. No API key and no network, with tests that run in milliseconds.chapter 1
  2. ch04: A real agent fixes a failing test The same 30-line loop, now driven by a real model’s recorded replies, with tools that run go test for real on your machine. Replay two models’ different paths to the same fix, and a run that recovers from three bad tool calls.chapter 4
  3. Example Streaming to a browser A server written with net/http alone sends an agent’s events, as they happen, to a page that reads them with EventSource. The headers, the flush, a deadline per write, a heartbeat, a client that leaves, and a shutdown that ends open streams, from a tested example.chapter 6

Every example runs without an API key. With no OPENROUTER_API_KEY set, a program replays model replies recorded from a real run, while its tools, including go test, run for real on your machine. You need Go 1.27 or later; older go commands from 1.21 on download it when they read the module’s go.mod.

$ git clone https://github.com/blackwell-systems/go-agents-book-code
$ cd go-agents-book-code/code

Or download the repository as a zip of the main branch and run the same commands from its code directory.

Every chapter’s program

Each chapter’s directory is a full snapshot of the agent at that point, so any row runs without the ones above it. The last two rows are examples beyond the chapters, built on chapter 6’s agent: the workflow shapes chapter 5 names, and an agent’s progress streamed to a browser. The counts are computed from the code each time this site is built: Go files, lines in those files, and top-level test functions, leaving out the fixtures under testdata. In all, the repository holds 1,596 Go files and 2,684 test functions in 209 packages.

CodeWhat it showsGo filesLines of GoTestsRun itGuide
ch01/The loop with a scripted model, fixing a failing test1094610go run ./ch01Guide
ch02/A chat program on the chat completions API, over net/http142,00020go run ./ch02none yet
ch03/A toy language model, a tokenizer, embeddings, and attention243,04937go run ./ch03 toynone yet
ch04/A real model with four tools fixes the test193,17834go run ./ch04Guide
ch05/Stop reasons, budgets, and a reply the loop refuses223,94049go run ./ch05 -replay dup-idnone yet
ch06/Streaming, and a stream that fails partway285,30467go run ./ch06 -replay stream-errornone yet
ch07/The crash-triage agent on one catalog task517,93177go run ./ch07none yet
ch08/Context policies on a batch of 99 crash reports6510,201104go run ./ch08 -task batchnone yet
ch09/Parallel tool calls, sub-agents, and one agent per crash group8112,623131go run ./ch09 -task g3-mixed-batch -groups -parallel 4none yet
ch10/An MCP client and server, with pinned tool definitions10116,264168go run ./ch10none yet
ch11/Other providers’ APIs and official SDKs10918,545190go run ./ch11none yet
ch12/Retries, traces, and cost, with injected 503s13322,132224go run ./ch12 -trace -faults 503none yet
ch13/Evals graded by code, a gate, and a reviewer14524,977247go test ./ch13/evals/none yet
ch14/Sandboxed tools, injection defenses, and approval18530,886303go run ./ch14none yet
ch15/Checkpoints, idempotency keys, and the crash sweep21837,920377go test ./ch15/durable/ -run TestCrashSweep -vnone yet
ch16/One task on the book’s harness, Eino, and Genkit (its own module)171,7768cd ch16 && go run ./cmd/harnessnone yet
ch17/The finished crash-triage agent26549,943475go run ./ch17none yet
catalog/The crash catalog’s tasks and its grader71,64914go test ./catalog/none yet
tracker/The small issue tracker the agent files issues in69629go test ./tracker/...none yet
remind/The practice service, with bugs seeded on purpose (its own module)567,657112cd remind && go test ./...none yet
shapes/The workflow shapes: a chain, a router, a vote, and check and revise121,22915go run ./shapesGuide
ssebrowser/An agent’s events streamed to a browser page689810go run ./ssebrowserGuide

A few rows end on purpose with an error or a pause:

  • ch05 and ch06 replay failures, so they exit with the error they demonstrate.
  • ch14 and ch17 stop when the agent asks to file an issue, because filing waits for a person’s approval; the program prints the call it is waiting on.
  • ssebrowser is a server: it prints an address to open and runs until you press Ctrl-C.
  • ch08 -task batch replays a whole batch and grades it, which takes a few minutes; the other rows finish within about two minutes.

With OPENROUTER_API_KEY set, the same commands call a live model and cost money; each chapter of the book says what its runs cost when they were recorded.