Leanpub Header

Skip to main content

Fine-Tuning Small Language Models for Local AI Agents

A Practical Guide to Building Specialized Agentic Systems on Your Own Hardware

This book is 100% completeLast updated on 2026-08-12

Turn a small language model into an AI agent that runs on your own hardware. This practical guide takes you from choosing a base model to fine-tuning, tool use, evaluation and deployment. With reproducible code and real configurations throughout, you'll learn how to build specialized local agents that are capable, efficient and truly yours.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
142
Pages
About

About

About the Book

This book teaches software engineers and ML practitioners how to select, fine-tune, evaluate, and deploy small language models as capable local AI agents. You will learn the foundations of transformer architectures and tokenization, master supervised fine-tuning and parameter-efficient methods like LoRA and QLoRA, build agentic capabilities for tool use and structured output, and ship production-ready systems on consumer hardware. Every chapter includes reproducible code, concrete configurations, and evidence-based comparisons so you can move from an unmodified base model to a specialized agent running entirely on your own machine.

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

A Practical Guide to Building Specialized Agentic Systems on Your Own Hardware

Introduction: Why Fine-Tune Small Models Locally?

Chapter 1: Foundations of Small Language Models

  1. What Is a “Small” Language Model?
  2. Transformer Architecture Refresher
  3. Tokenization and Vocabulary
  4. Context Windows and Memory
  5. Embeddings and Representations
  6. The Size–Capability–Cost Trade-off Curve

Chapter 2: The Local AI Ecosystem and Model Selection

  1. Major Small Language Model Families
  2. Base Models Versus Instruction-Tuned Models
  3. Quantization Formats and Trade-offs
  4. Inference Engines and Runtimes
  5. Hardware Realities: GPUs, CPUs, and Memory
  6. Selecting Your Stack: A Decision Framework

Chapter 3: Data Strategy for Fine-Tuning

  1. Fine-Tuning Versus Prompting
  2. Fine-Tuning Versus RAG
  3. Fine-Tuning Versus Continued Pretraining
  4. Fine-Tuning Versus Distillation
  5. The Data Quality Hierarchy
  6. Licensing, Provenance, and Legal Considerations

Chapter 4: Building Fine-Tuning Datasets

  1. Discovering Existing Datasets
  2. Instruction Dataset Formats
  3. Building Domain-Specific Datasets
  4. Tool-Use and Function-Calling Datasets
  5. Synthetic Data Generation
  6. Dataset Cleaning, Validation, and Versioning

Chapter 5: Supervised Fine-Tuning Fundamentals

  1. How Supervised Fine-Tuning Works
  2. Setting Up Your Training Environment
  3. The Hugging Face Ecosystem for Fine-Tuning
  4. Configuring a Training Run
  5. Estimating Resource Requirements
  6. Running Your First Fine-Tuning Job

Chapter 6: Parameter-Efficient Fine-Tuning Techniques

  1. Why Parameter-Efficient Fine-Tuning?
  2. LoRA: Low-Rank Adaptation
  3. QLoRA: Quantized Low-Rank Adaptation
  4. Other Adapter Methods
  5. Configuring PEFT for Different Tasks
  6. Merging and Exporting Adapters

Chapter 7: Training Operations, Experiment Management, and Debugging

  1. Hyperparameter Selection Strategies
  2. Training Stability and Convergence
  3. Experiment Tracking and Logging
  4. Checkpoint Management
  5. Debugging Common Failure Modes
  6. Reproducibility Practices

Chapter 8: Evaluating Fine-Tuned Models

  1. Why Systematic Evaluation Matters
  2. Automated Benchmark Suites
  3. Task-Specific Evaluation Design
  4. Tool-Calling and Structured Output Evaluation
  5. Regression Testing and Ablation Studies
  6. Human Evaluation When It Counts

Chapter 9: Specializing Models for Agentic Behavior

  1. What Makes a Model “Agentic”?
  2. Training for Tool Use and Function Calling
  3. Training for Structured Outputs
  4. Reasoning Specialization
  5. Routing and Intent Classification
  6. Multi-Agent Training Patterns

Chapter 10: Building Local AI Agents with Fine-Tuned Models

  1. The Agent Loop Architecture
  2. Tool Interfaces and MCP
  3. Memory and State Management
  4. Retrieval-Augmented Generation Integration
  5. Constrained Decoding and Output Validation
  6. Security, Sandboxing, and Permissions

Chapter 11: Production Deployment and Optimization

  1. Model Packaging and Distribution
  2. Serving Architectures
  3. Latency and Throughput Optimization
  4. Memory Optimization in Production
  5. Monitoring and Observability
  6. Performance Profiling and Bottleneck Analysis

Chapter 12: Long-Term Maintenance, Security, and Governance

  1. Iterative Fine-Tuning and Continual Learning
  2. Catastrophic Forgetting: Causes and Mitigation
  3. Dataset Drift and Model Decay
  4. Security Auditing Fine-Tuned Models
  5. Model Provenance and Compliance
  6. Organizational Practices for Model Operations

Conclusion: The Framework for Local Agentic Systems

References

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