Leanpub Header

Skip to main content

Building AI Agents with Ollama

Design, Deploy, and Scale Reliable Local AI Systems

This book is 100% completeLast updated on 2026-07-29

Build powerful AI agents that run entirely on your own hardware with Ollama. Learn practical patterns for RAG, multi-agent systems, security and deployment through clear explanations and production-ready code you can use right away. From first setup to reliable real-world applications, this book helps you build with confidence.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
399
Pages
About

About

About the Book

This book teaches you how to design, build, deploy, and maintain production-grade AI agents that run entirely on your own hardware using Ollama as the inference engine. You will learn everything from installation and model selection through advanced multi-agent architectures, RAG pipelines, security hardening, and observability. Every chapter includes complete, working code examples that you can copy, build, and run without modification. Whether you are building personal assistants, coding agents, document analysis tools, or autonomous workflow systems, this book provides the patterns, implementations, and engineering practices needed to make them work reliably in the real world.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books. Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 420 engineers and researchers. We spend far more time validating technical accuracy and keeping our content up to date than generating text. We are always interested in working with experienced professionals who have deep expertise in a particular technology or domain. If you would like to publish a book with us or help review an existing manuscript, we'd love to hear from you. Send us a message describing your area of expertise. We are especially interested in niche technologies, specialized skills and emerging topics that are underrepresented in existing technical literature.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

Design, Deploy, and Scale Reliable Local AI Systems

Introduction: The Case for Local Agents

  1. Why Local Matters Now
  2. What Is an AI Agent
  3. The Reliability Gap
  4. How This Book Is Structured
  5. Key Takeaways

Chapter 1: Foundations of Local LLMs and Ollama

  1. How Local Inference Works
  2. Installing and Configuring Ollama
  3. Understanding the Ollama API
  4. Model Formats and GGUF
  5. First Agent: A Minimal Example
  6. Key Takeaways

Chapter 2: Choosing and Managing Models

  1. The Local Model Landscape
  2. Performance vs Size Tradeoffs
  3. Quantization Explained
  4. Multi-Model Strategies
  5. Key Takeaways

Chapter 3: Prompt Engineering for Agents

  1. From Prompts to Specifications
  2. Structured Prompt Design Patterns
  3. System Prompts and Role Definitions
  4. Few-Shot and Example-Based Prompting
  5. Testing and Iterating Prompts
  6. Key Takeaways

Chapter 4: Structured Outputs and Tool Calling

  1. JSON Mode and Schema Enforcement
  2. Pydantic Models for Type Safety
  3. Function Calling Patterns
  4. Error Recovery for Bad Outputs
  5. Building a Tool Registry
  6. Browser Automation Tools
  7. Key Takeaways

Chapter 5: Memory Systems and Context Management

  1. Chapter Dependencies and Project Structure
  2. The Memory Problem
  3. Short-Term Conversation Memory
  4. Long-Term Memory Architectures
  5. Context Window Optimization
  6. Implementing a Memory Manager
  7. Key Takeaways

Chapter 6: Retrieval-Augmented Generation and Vector Search

  1. Chapter Dependencies and Project Structure
  2. Why RAG Is Essential for Agents
  3. Embeddings and Vector Spaces
  4. Vector Database Options
  5. Document Ingestion Pipelines
  6. Hybrid Search and Re-Ranking
  7. Key Takeaways

Chapter 7: Planning, Reasoning, and Complex Tasks

  1. Beyond Single-Turn Responses
  2. Chain of Thought and Reasoning
  3. The ReAct Pattern
  4. Task Decomposition Strategies
  5. Key Takeaways

Chapter 8: Multi-Agent Architectures

  1. Why Multiple Agents
  2. Specialization and Roles
  3. Communication Patterns
  4. Orchestration Strategies
  5. Building an Agent Team
  6. Key Takeaways

Chapter 9: Workflows, State Machines, and Orchestration

  1. Agents as Workflow Nodes
  2. State Machine Design
  3. Async Processing and Concurrency
  4. Retries, Timeouts, and Circuit Breakers
  5. Key Takeaways

Chapter 10: Testing, Debugging, and Observability

  1. Chapter Dependencies and Project Structure
  2. The Testing Challenge
  3. Unit Testing Agents and Tools
  4. Integration and End-to-End Tests
  5. Logging and Tracing
  6. Monitoring and Alerting
  7. Key Takeaways

Chapter 11: Performance, Resources, and Optimization

  1. Understanding Resource Consumption
  2. GPU Acceleration and Configuration
  3. Caching Strategies
  4. Batch Processing and Throughput
  5. Scaling Local Inference
  6. Key Takeaways

Chapter 12: Security, Privacy, and Sandboxing

  1. The Threat Model for Local Agents
  2. Prompt Injection Attacks
  3. Tool Sandboxing
  4. Data Privacy and Compliance
  5. Hardening Your Deployment
  6. Key Takeaways

Chapter 13: Deployment, APIs, and Real-World Integration

  1. Chapter Dependencies and Project Structure
  2. Packaging Agents as Services
  3. RESTful Agent APIs
  4. Web Interfaces and Dashboards
  5. Desktop and System Integration
  6. Production Deployment Patterns
  7. Key Takeaways

Chapter 14: Capstone Project — Building a Production Coding Agent

  1. Project Overview: The CodePilot Agent
  2. Project Structure
  3. Step 1: Configuration and Dependencies
  4. Step 2: Tool Registry and Implementation
  5. Step 3: Memory System Integration
  6. Step 4: RAG Pipeline for Codebase Indexing
  7. Step 5: Agent Core with Prompts and Reasoning
  8. Step 6: Security Layer
  9. Step 7: Observability Setup
  10. Step 8: FastAPI Service Layer
  11. Step 9: Docker Deployment
  12. Step 10: Web Interface
  13. Step 11: Testing
  14. Running the Complete System
  15. Architecture Summary
  16. Key Takeaways

Conclusion: The Future of Local Agents

  1. What We Have Built
  2. The Trajectory Ahead
  3. Principles That Will Endure
  4. Key Takeaways

References

Appendix A: Ollama Environment Variables and Configuration Reference

  1. Network and Binding Variables
  2. Model Storage Variables
  3. Concurrency and Queue Variables
  4. GPU and Hardware Variables
  5. Context and Inference Variables
  6. Setting Environment Variables by Platform
  7. Recommended Production Configurations

Appendix B: Troubleshooting Common GPU, Driver, and OOM Errors

  1. Enabling Debug Logging
  2. NVIDIA GPU Not Detected
  3. AMD GPU Not Detected
  4. CUDA Out of Memory (OOM) Errors
  5. CPU Fallback (Silent or Explicit)
  6. Model Loading Fails or Hangs
  7. Connection Refused Errors
  8. Apple Silicon Specific Issues
  9. Diagnostic Checklist

Appendix C: Model Quantization Cheat Sheet

  1. Quick Decision Guide
  2. Quantization Level Comparison
  3. Key Principles
  4. VRAM Estimation Formula
  5. Model Tag Naming Convention
  6. Speed vs Quantization

Appendix D: Reusable Code Patterns

  1. Pattern 1: Retry with Exponential Backoff and Jitter
  2. Pattern 2: Circuit Breaker for External Services
  3. Pattern 3: Structured JSON Logger
  4. Pattern 4: Token Counter Utility
  5. Pattern 5: Rate Limiter for API Calls
  6. Pattern 6: Context Window Manager

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

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.)

In fact, authors have earned over $15 million writing, publishing and selling 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