Black Friday Deals For Authors Are On Now!

Leanpub Header

Skip to main content

AI Agents from Scratch: Build, Test and Ship a Working Agent in Python

Build, Test and Ship a Working Agent in Python

AI Agents from Scratch: Build, Test and Ship a Working Agent in Python
This book is 100% completeLast updated on 2026-11-24

Build a complete AI agent in a little over a thousand lines of standard-library Python: model client, tools, loop, guardrails, memory, tracing, tests and evaluation. No framework, and every listing comes from code that passes its own test suite.

Minimum price

$12.00

$19.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

Most agent tutorials start by installing a framework. When the agent misbehaves, you are left guessing what the framework did.

This book takes the opposite route. You build every part yourself, in an order that gives you something running at the end of each chapter:

- a small model interface, with a scripted model for tests and an HTTP client for any chat-completions service, including local models served by Ollama

- a tool system that turns a typed, documented Python function into something a model can call, with argument validation and errors the model can recover from

- the agent loop, with four explicit stop conditions

- guardrails: a sandboxed workspace, approval for risky actions, step, repeat and token limits, and a defence against instructions hidden in tool output

- memory: context trimming that never separates a tool call from its result, and a persistent note store

- JSONL tracing and run summaries

- an evaluation harness for measuring pass rates against a real model

- a command-line program, and a pre-release checklist

- a second agent (customer support) built on the same kit in about a hundred lines

The package has no dependencies. Its 51 tests run in a few seconds with no model and no network. Every code listing in the book is extracted from the tested source, and every output block was produced by running the command shown.

## Who it is for

Python developers who want to understand how agents work underneath, and anyone who has used an agent framework and wants to be able to debug it.

## What you need

Python 3.10 or newer, and pytest. From Chapter 11, a model that supports tool calling (hosted or local).

## What is included

- The book: 80 pages, PDF and EPUB

- The complete source code with tests and examples (MIT licence)

## Table of contents

Before You Begin

Part 1: Foundations

1. What an Agent Is

2. Talking to a Model

3. Tools

Part 2: The Agent

4. The Agent Loop

5. Built-in Tools and the Workspace

6. Guardrails

7. Memory

Part 3: Test and Ship

8. Testing Without a Model

9. Tracing

10. Evaluating With a Real Model

11. Shipping

12. A Second Agent on the Same Kit

13. Where to Go Next

Appendix A: Command Reference

Appendix B: Troubleshooting

Author

About the Author

Benjamin App

Benjamin is a lecturer in Computer Science at prestigious university, and holds a PhD in software engineering and active research in cybersecurity and applied AI. After years of watching capable students give up on AI because the textbooks assumed too much, he wrote the book he wished he could hand them. It explains each concept the way a child would learn it, through a character named Alex, while keeping the full mathematics, formulas and code, so that readers finish with real understanding instead of memorised terms.

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub