agent-infra-cookbook
Health Warn
- License — License: MIT
- Description — Repository has a description
- Active repo — Last push 0 days ago
- Low visibility — Only 6 GitHub stars
Code Pass
- Code scan — Scanned 12 files during light audit, no dangerous patterns found
Permissions Pass
- Permissions — No dangerous permissions requested
No AI report is available for this listing yet.
Learn how agents work by building one: an agent runtime in Python's standard library, one layer at a time, with interactive lessons that need no API key.
Learn how agents work by building one: an agent runtime in Python's standard library, one layer at a time.
Quickstart · The Agent Lab · Chapters · Code layout · Community
The cookbook builds the infrastructure behind an agent: HTTP model calls, streaming, tools, the agent loop,
sessions and compaction, sandboxes, subagents, MCP, durable runs, and agents that improve their own prompts and code.
Each chapter adds one layer, with a doc, runnable examples and tests. The agent package has no runtime dependencies
and no provider SDK, so every line between your prompt and the model is code you can read.
Quickstart
You need Python 3.10+ and uv.
git clone https://github.com/fishmingyu/agent-infra-cookbook.git
cd agent-infra-cookbook
uv sync
uv run python -m examples.04_agent_loop --offline
A scripted model calls real Python tools and answers (2 + 3) + (4 + 5) = 14. in three turns, with no API key.
Without uv, run python -m examples.04_agent_loop --offline in your own environment.
Use a live model
Put an OpenRouter key in a .env file at the repository root; git ignores it.
OPENROUTER_API_KEY=sk-or-...
uv run python -m examples.01_chat # one request
uv run python -m agent # the CLI
The CLI streams the answer, tool calls and reasoning, and saves each conversation in sessions.db. Shell commands run
in bubblewrap, Seatbelt or Docker, read-only and offline; with --sandbox none, it asks before each one.
| Option | What it does |
|---|---|
--write |
Let commands write to the workspace |
--network, --allow-domain D |
Let commands reach the network, or only domain D |
--session-id ID |
Reopen a conversation; in the chat, /session lists them and /session ID switches |
--memory memory.json |
Keep facts between conversations, asking before each save |
--mcp .mcp.json |
Connect MCP servers and load their tool schemas on demand (how) |
OPENROUTER_MODEL picks the model and OPENROUTER_BASE_URL another Chat Completions endpoint; --help lists
every option.
The Agent Lab
The Agent Lab is the course built on this code: 52 interactive
lessons in ten stages, from one model request to an agent that improves itself; the
learning path splits them into a core path and optional deep dives. Each lesson has a figure
you can play, the code behind it and an example to run, and they all follow one report assistant as it grows. The
lessons need no API key.
Start with Talk to an LLM: compose a request,
then read its JSON body, the response and the stream of events. To build and serve the book yourself, see
book/README.md.
Chapters by stage
Each chapter's file name keeps a phase number, which records the order the code was built in; the book teaches the
same code in ten stages. Each row below names a chapter that teaches part of a stage, what it builds and the examples
it runs, so a chapter can appear in more than one stage. For a first read, the learning path
lists the core lessons in order.
| Stage | Chapter | What you build | Examples |
|---|---|---|---|
| 1. Basic agent | Model requests | Requests, response parsing, message history, streaming and retries | 01 02 66 67 54 |
| Tools | Function schemas and tool execution | 03 |
|
| The agent loop | Feed tool results back until the model stops, under one system prompt | 04 05 47 |
|
| Context engineering | A final answer that matches a JSON schema | 13 |
|
| 2. Context and memory | Context and sessions | Keep sessions, compact them, fork histories and resume saved summaries | 06 07 28 37 42 |
| Context engineering | A prefix the prompt cache can reuse, and facts kept between conversations | 55 14 |
|
| Retrieval memory | Scoped search over saved facts and documents, and search indexes | 19 49 58 |
|
| 3. Extensions | Context engineering | Hooks that change one request or tool call, and the request pipeline | 12 48 |
| Skills and discovery | Skills the model reads only when it needs them | 17 |
|
| MCP client | MCP connections and on-demand tool schemas | 11 25 |
|
| 4. Safety and permissions | Sandbox and safety | Tool approval, address checks, sandboxed commands and isolation levels | 08 70 |
| Injection and authority | Track untrusted data and check capabilities when a tool runs | 23 |
|
| 5. Tools at scale | Skills and discovery | Tool definitions the model gets only from a search | 69 |
| Programmatic tools | Let bounded code call tools, under the same tool policy | 20 |
|
| Asynchronous tools | Background tools whose results arrive at the next request | 15 |
|
| 6. Multi-agent | Subagents | Children that work in the background and report to their parent | 10 56 32 |
| Agent swarm | One task over many items, a few children at a time | 51 |
|
| Handoffs | Pass one conversation between roles with their own prompts and tools | 52 |
|
| Agent coordination | Dispatch dependent work to persistent worker slots | 22 27 34 |
|
| Agent teams | A lead and named teammates with mail and a shared task list | 53 |
|
| 7. Long-running work | Plans and verification | Steps that unlock once their dependencies are verified | 18 |
| Durable execution | A journal that lets a run, its children and its summaries survive a restart | 16 31 38 39 40 41 43 44 45 46 |
|
| Durable approvals | Save a pending approval, bound to the exact operation it allows | 21 |
|
| Always-on agents | Wake on a schedule, an event or a finished child | 57 |
|
| Outcomes and resource evidence | Traces that follow a run across processes | 36 |
|
| 8. Coding agents | Coding workspace | Read, edit and run commands in one workspace, with undo | 50 59 68 |
| 9. Evaluation and cost | Evaluation | Versioned tasks, repeated trials, pass rates and usage | 09 |
| Outcomes and resource evidence | Grades, shared budgets and rebuilt requests | 24 29 33 |
|
| Benchmarks | Grade a patch, or the final state a run leaves | 64 65 |
|
| 10. Self-improvement | Prompts and skills from experience | Learn prompts and skills from past runs; keep what a held-out check confirms | 60 61 |
| Evolve programs and tools | Evolve code, even the agent's own tool, against a grader it cannot reach; keep one of each kind in a MAP-Elites grid | 62 63 71 |
Decision models, validated decisions whose results survive a restart, has no lesson; its
examples are 26 3035. The learning path lists it with the rest of the
runtime the course does not teach.
Run an example by its file name, such as uv run python -m examples.04_agent_loop. All of them run offline except01_chat and 02_stream; 03 to 06, 09 and 47 take --offline, and without it they call the live model.
How the code is organized
agent/
errors.py messages.py http.py the vocabulary every layer shares
model/ talk to the model: HTTP client, response parsing, streaming
tools/ what the model can do: registry, web, workspace files, shell/ with its sandboxes
mcp/ MCP servers over stdio or HTTP: their tools, resources and prompts
context/ what the model sees: sessions and branches, compaction and its publication receipts,
request configuration, control checkpoints
loop.py one run: request, run the tool calls, feed the results back
runtime.py Agent: the loop, with a session and context control
ext/ built on the runtime, a package for each kind of work:
memory/ saved facts, retrieval and search indexes
safety/ capabilities, and provenance for untrusted data
scale/ skills and tool discovery, programs, asynchronous tools
agents/ subagents, swarms, handoffs, teams, coordination
long_running/ durable tools and approvals, steering, plans, always-on agents, tracing
workspace/ checkpoints and layers
evaluation/ tasks and trials, coding benchmarks, budgets, routing
improve/ prompts, skills and programs kept against a grader
scheduling/ durable phase tasks, owned children, swarm dispatch
recovery/ request and response evidence, model steps, summary jobs
decisions/ native decisions and their journal
cli.py python -m agent
Each layer imports only from the layers listed before it, and test_layers.py checks this.
The design says what each layer owns, where values are checked and where to extend it.
Run the tests with uv run python -m pytest. They use scripted models and local HTTP servers, so they need no key.
Community
Ask a question, share what you built on the runtime, or tell us which lesson lost you or what was hard to read on
Discord. Bugs and doc fixes go in
issues.
Contributions are welcome, and new lessons, phases and examples most of all. CONTRIBUTING.md says
how to set up and which constraints a pull request keeps.
License
MIT. The book's fonts and icons keep their own licences:
the fonts' OFL and Lucide's.
Reviews (0)
Sign in to leave a review.
Leave a reviewNo results found