LLM Agents From Scratch
The companion library for Build a Multi-Agent System — With MCP and A2A (Manning). Learn how LLM agents work by building one yourself, from first principles, step by step.
Available now through Manning's Early Access Program (MEAP) — buy today and get each chapter as it's completed. Buy the Book →
About
Multi-agent systems and the LLM agents that power them are among the most discussed topics in AI today. There are already many capable frameworks out there — the goal of this book isn't to replace them, but to help you deeply understand how they work by having you build one yourself, from scratch.
All the code lives in the book's own hand-rolled agent framework, primarily designed for educational purposes rather than production deployment. It will give you the foundation to work more confidently with any other LLM agent framework of your choosing, or even to build your own specialised solutions.
From the Book
Each chapter builds on the last, progressively deepening your understanding from core concepts to full multi-agent systems.
Part 1 — Build Your First LLM Agent
| Ch | Title | Notebook |
|---|---|---|
| 1 | What Are LLM Agents and Multi-Agent Systems? | — |
| 2 | Working with Tools | Ch 2 |
| 3 | Working with LLMs | Ch 3 |
| 4 | The LLM Agent Class | Ch 4 |
Part 2 — Enhance Your LLM Agent
| Ch | Title | Notebook |
|---|---|---|
| 5 | MCP Tools | Ch 5 |
| 6 | Skills | Ch 6 |
| 7 | Memory | Ch 7 |
| 8 | Human in the Loop | Ch 8 |
Part 3 — Building Multi-Agent Systems
| Ch | Title | Notebook |
|---|---|---|
| 9 | Assembling MAS with Subagents | Ch 9 |
| 10 | Assembling Interoperable MAS with A2A | Ch 10 |
Capstone Projects
Capstones are larger, end-to-end projects that pull together what you have built in the book and apply it to something closer to a real-world system.
| Capstone | Description | Notebook |
|---|---|---|
| I. Monte Carlo Estimation of Pi | Orchestrate parallel tool calls to estimate π using the Monte Carlo method. | Open |
| II. Revisiting Monte Carlo Estimation of Pi | Estimate the standard error of Capstone I's algorithm, using Chapter 9 subagents to contain the context sprawl of running it many times. | Open |
| III. Evaluating MAS and Single-Agent Systems | Coming soon. | — |
| IV. Peer-to-Peer MAS | Coming soon. | — |
| V. Graph Engineering with Subagents and A2A Agents | Coming soon. | — |
Running the Notebooks
I recommend running the notebooks on Ollama Cloud, which serves larger open-source models on Ollama's own hardware. These models are much more capable than what an everyday laptop can run, and Ollama keeps rotating in the newest, best-performing ones.
Option A — Ollama Cloud (recommended)
- Create an API key at https://ollama.com/settings/keys.
- Export it:
export OLLAMA_API_KEY=<your key>.
Then launch Jupyter from the project root:
uv run --with jupyter jupyter lab
The notebooks detect the key and use the default cloud model. I pick that
model by benchmarking the available Ollama Cloud models on how well each one
runs the notebooks end to end, and an automated run checks every notebook
against it weekly (see the
notebook status page). To try
another cloud model from https://ollama.com/search?c=cloud, set the
OLLAMA_MODEL environment variable to its name.
Option B — Local Ollama
Install Ollama and pull the model:
ollama pull qwen3:14b
With no OLLAMA_API_KEY set, the notebooks start the Ollama service for you,
so there's no need to run ollama serve. A 14B model needs about 16 GB of RAM
and a dedicated GPU. You can set OLLAMA_MODEL to a smaller model (such as
qwen3:4b), but smaller models may not follow tool-use instructions
reliably, especially in later chapters.
Option C — Lightning AI (local models on a free GPU)
Lightning AI offers about 22 free GPU compute hours a month, enough for several sessions on an L4.
- Create a free account if you don't have one.
- Open the book template.
- Click Clone and make sure it's the latest version.
- Launch the Studio with an L4 GPU.
- Open any notebook. Ollama and the model are already set up.
OpenAI or Anthropic
The notebooks also run on OpenAI and Anthropic models. Install the extra
(uv sync --extra openai or uv sync --extra anthropic), export
OPENAI_API_KEY or ANTHROPIC_API_KEY, and set LLM_PROVIDER=openai (or
anthropic). The "Setting up the LLM" section at the top of each notebook
has the details.
Getting Started
Prerequisites
- Python 3.11+
- An Ollama Cloud API key (recommended) or a local Ollama install; see Running the Notebooks
- uv for dependency management
Installation
Clone the repository:
# SSH
git clone git@github.com:nerdai/llm-agents-from-scratch.git
# HTTPS
git clone https://github.com/nerdai/llm-agents-from-scratch.git
cd llm-agents-from-scratch
Install dependencies:
uv sync --all-extras --dev
Quick Start
from llm_agents_from_scratch.llms import OllamaLLM
from llm_agents_from_scratch.agent import LLMAgentBuilder
from llm_agents_from_scratch.tools import SimpleFunctionTool
def add(a: int, b: int) -> int:
return a + b
llm = OllamaLLM(model="llama3.2")
tool = SimpleFunctionTool(fn=add)
agent = (
LLMAgentBuilder()
.with_llm(llm)
.with_tools([tool])
.build()
)
result = await agent.run("What is 3 + 5?")
print(result)
Development
# Run all tests
make test
# Lint and format
make lint
make format
# Coverage report
make coverage-report
See CLAUDE.md for full development guidance.
Contributing
Bug reports, feature requests, and community project submissions are welcome. See CONTRIBUTING.md for details.
- Found a bug? Open an issue
- Built something cool? Share it on GitHub Discussions
License
Apache 2.0 — see LICENSE for details.
Metadata
Release files for llm-agents-from-scratch 0.0.27
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| llm_agents_from_scratch-0.0.27.tar.gz | 2.4 MB | Without provenance | Details for llm_agents_from_scratch-0.0.27.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llm_agents_from_scratch-0.0.27-py3-none-any.whl | Python 3 | none | any | Details for llm_agents_from_scratch-0.0.27-py3-none-any.whl |
Total release size: 2.5 MB
Release files / llm_agents_from_scratch-0.0.27.tar.gz
| Download URL | llm_agents_from_scratch-0.0.27.tar.gz |
|---|---|
| Size | 2.4 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3b42067a8b5efd6c1eb21f22f1bee18c2f7d083df420b96cb2c7222f954b2f98
|
|
BLAKE2b-256 checksum How to use checksums |
e4c8302a8ffc68c1f8601c1e299f288294c53b663da80b57e047f515fb42a0a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / llm_agents_from_scratch-0.0.27-py3-none-any.whl
| Download URL | llm_agents_from_scratch-0.0.27-py3-none-any.whl |
|---|---|
| Size | 120.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
85114120f8dd9468567b931041c5f5fe8b5d2cf3fe4dc0bd324917c38bc2b99b
|
|
BLAKE2b-256 checksum How to use checksums |
ce91ebd13487f1761f0bc0b7f15308d093555196053a9634d4b0d56691bcc37b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for llm-agents-from-scratch 0.0.27
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.