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.
- 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
- 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
- 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.
| Code | What it shows | Go files | Lines of Go | Tests | Run it | Guide |
|---|---|---|---|---|---|---|
ch01/ | The loop with a scripted model, fixing a failing test | 10 | 946 | 10 | go run ./ch01 | Guide |
ch02/ | A chat program on the chat completions API, over net/http | 14 | 2,000 | 20 | go run ./ch02 | none yet |
ch03/ | A toy language model, a tokenizer, embeddings, and attention | 24 | 3,049 | 37 | go run ./ch03 toy | none yet |
ch04/ | A real model with four tools fixes the test | 19 | 3,178 | 34 | go run ./ch04 | Guide |
ch05/ | Stop reasons, budgets, and a reply the loop refuses | 22 | 3,940 | 49 | go run ./ch05 -replay dup-id | none yet |
ch06/ | Streaming, and a stream that fails partway | 28 | 5,304 | 67 | go run ./ch06 -replay stream-error | none yet |
ch07/ | The crash-triage agent on one catalog task | 51 | 7,931 | 77 | go run ./ch07 | none yet |
ch08/ | Context policies on a batch of 99 crash reports | 65 | 10,201 | 104 | go run ./ch08 -task batch | none yet |
ch09/ | Parallel tool calls, sub-agents, and one agent per crash group | 81 | 12,623 | 131 | go run ./ch09 -task g3-mixed-batch -groups -parallel 4 | none yet |
ch10/ | An MCP client and server, with pinned tool definitions | 101 | 16,264 | 168 | go run ./ch10 | none yet |
ch11/ | Other providers’ APIs and official SDKs | 109 | 18,545 | 190 | go run ./ch11 | none yet |
ch12/ | Retries, traces, and cost, with injected 503s | 133 | 22,132 | 224 | go run ./ch12 -trace -faults 503 | none yet |
ch13/ | Evals graded by code, a gate, and a reviewer | 145 | 24,977 | 247 | go test ./ch13/evals/ | none yet |
ch14/ | Sandboxed tools, injection defenses, and approval | 185 | 30,886 | 303 | go run ./ch14 | none yet |
ch15/ | Checkpoints, idempotency keys, and the crash sweep | 218 | 37,920 | 377 | go test ./ch15/durable/ -run TestCrashSweep -v | none yet |
ch16/ | One task on the book’s harness, Eino, and Genkit (its own module) | 17 | 1,776 | 8 | cd ch16 && go run ./cmd/harness | none yet |
ch17/ | The finished crash-triage agent | 265 | 49,943 | 475 | go run ./ch17 | none yet |
catalog/ | The crash catalog’s tasks and its grader | 7 | 1,649 | 14 | go test ./catalog/ | none yet |
tracker/ | The small issue tracker the agent files issues in | 6 | 962 | 9 | go test ./tracker/... | none yet |
remind/ | The practice service, with bugs seeded on purpose (its own module) | 56 | 7,657 | 112 | cd remind && go test ./... | none yet |
shapes/ | The workflow shapes: a chain, a router, a vote, and check and revise | 12 | 1,229 | 15 | go run ./shapes | Guide |
ssebrowser/ | An agent’s events streamed to a browser page | 6 | 898 | 10 | go run ./ssebrowser | Guide |
A few rows end on purpose with an error or a pause:
ch05andch06replay failures, so they exit with the error they demonstrate.ch14andch17stop 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.ssebrowseris a server: it prints an address to open and runs until you press Ctrl-C.ch08 -task batchreplays 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.