Leanpub Header

Skip to main content

Running, Training, and Securing Large Language Models

How to test, secure, and pay for a model in production, and train one of your own

Running, Training, and Securing Large Language Models

Your LLM feature works in the demo. This book makes it production-grade: tested, evaluated, defended against prompt injection, metered to the cent, and running on a model you fine-tuned yourself, all from Laravel.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

Getting a model to do something useful from Laravel is the easy part. The hard part is shipping it: tests that mean something when the output is never the same twice, a bill that doesn’t surprise anyone, and an assistant that holds up when a customer types “ignore your instructions.” This book covers that gap, and then it shows you how to train a model of your own.

It starts with just enough Python to run the tools where training and evaluation happen, and with the patterns that let a Laravel app and a Python service share work. Next comes production discipline. You’ll test nondeterministic systems with fakes, golden datasets, and LLM-as-judge, run evaluation continuously, and trace every prompt, tool call, and retry. You’ll defend against direct and indirect prompt injection, put guardrails and least privilege around tools, and redact personal data before it leaves your app. On the cost side, you’ll take apart real surprise bills, build a live cost ledger with budget kill switches, and route each request to the cheapest model that can handle it.

Then you train. You’ll decide when fine-tuning is worth it, build a consented and redacted dataset from your own app, and fine-tune three ways: through hosted APIs, on a unified-memory machine with Apple MLX or AMD ROCm, and on NVIDIA GPUs with Unsloth and Axolotl. Google Colab’s free tier is an option for readers without a GPU. After that you’ll quantize and serve the result, train a small model from scratch, and distill a large model into a small one. Last, you’ll open up the Laravel AI packages, including the Laravel AI SDK and Neuron AI, so you can see exactly what they abstract now that you’ve built the same things by hand.

Everything runs locally by default, with cloud providers as options. The companion app, Ledgerly, starts from a complete checkout at the beginning of the volume and has a tag for every chapter. You don’t need Volume 1: Chapter 0 gives you the mental model and the setup.

Volume 2 takes you from intermediate to expert across 27 chapters (Chapter 0 and Chapters 26 to 51). It includes reference appendices, a glossary, and answers to every self-check question. It ends with a full sample chapter from Volume 3.

The series opens with The Foundations of Large Language Models (Volume 1) and closes with Projects, Lifecycle, and Reference (Volume 3).

Author

About the Author

Jeremy Kavuncuoglu

Jeremy Kavuncuoglu is a software developer and technical lead who builds production AI systems in Laravel. He leads development on a set of platforms at a healthcare technology company, ranging from patient outreach and calling to AI analytics that process tens of thousands of call transcripts a day.

He runs large language models in production himself. He serves open-weight models on his own GPU infrastructure, builds retrieval pipelines with pgvector and cross-encoder re-ranking, and fine-tunes models for real workloads. He does it on a small engineering team, where every dollar and every security boundary counts. His approach is principles first: understand what a system actually does before trusting any abstraction built on top of it.

His day-to-day stack is Laravel, PHP, Vue 3, and AWS. He also founded Scribex, a bring-your-own-key AI SEO platform, and maintains multiple open-source tools. Visit https://kavuncuoglu.com to learn more

Contents

Table of Contents

  • Preface
  • Getting the companion code
  • Chapter 0 — Starting here
  • Section 5 — Bridging to the Python ecosystem
    • Chapter 26 — Python essentials for this book
    • Chapter 27 — PHP–Python interop patterns
    • Chapter 28 — The Python AI stack, mapped
  • Section 6A — Production discipline and evaluation
    • Chapter 29 — Testing nondeterministic systems and synthetic flywheels
    • Chapter 30 — Evaluation as an ongoing practice
    • Chapter 31 — Observability, distributed tracing, and audit logs
  • Section 6B — Application security and privacy engineering
    • Chapter 32 — Direct and indirect prompt injection
    • Chapter 33 — Tool guardrails, sandboxing, and least privilege
    • Chapter 34 — Data privacy, PII redaction, and compliance boundaries
  • Section 6C — Cost engineering, governance, and FinOps
    • Chapter 35 — The anatomy of an AI surprise bill
    • Chapter 36 — Real-time cost observability and budget governance
    • Chapter 37 — Cost optimization and context engineering
  • Section 7 — Training and fine-tuning
    • Chapter 38 — When to fine-tune
    • Chapter 39 — Fine-tuning methods
    • Chapter 40 — Building a training dataset
    • Chapter 41 — Fine-tuning walkthrough: hosted APIs
    • Chapter 42 — Fine-tuning on unified memory: Apple MLX and AMD ROCm APUs
    • Chapter 43 — Fine-tuning on CUDA: Unsloth, Axolotl, and cloud GPUs
    • Chapter 44 — Quantizing and serving a fine-tuned model
    • Chapter 45 — Training a small model from scratch
    • Chapter 46 — Distillation and small-model strategies
  • Section 8 — The Laravel ecosystem and tooling landscape
    • Chapter 47 — The Laravel package landscape: core packages
    • Chapter 48 — Specialized and emerging packages
    • Chapter 49 — AI-assisted Laravel development
    • Chapter 50 — Keeping current
    • Chapter 51 — Responsible deployment
  • Appendices
    • Appendix C — Model reference (Volume 2 subset)
    • Appendix E — Hardware sizing for training (Volume 2 subset)
    • Appendix F — Fine-tuning command and config reference
    • Appendix G — Serving and quantization for fine-tuned models (Volume 2 subset)
    • Appendix H — Evaluation tool reference
    • Appendix I — Security test fixtures and detection rules
    • Appendix J — Python quick reference for PHP developers
    • Appendix K — Glossary
    • Appendix L — Companion app, Python, and workstation setup for Volume 2
    • Appendix N — Self-check answers
  • Where to go from here: Volume 3
  • Sample chapter from Volume 3 — Chapter 52: The support assistant, end to end

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

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