A Developer's Guide to Jinja, apply_chat_template, and Rendering Model-Ready Prompts
Tell us what's wrong and we'll take a look.
Thanks – we got it. We'll review and follow up if needed.
Your model passed every benchmark, then quietly got worse in production - and nothing in your logs says why.
The culprit is almost always the same invisible layer: the chat template, the code that turns your list of messages into the exact tokens the model was trained on. Get it wrong and the model still answers. It just answers worse, silently, with no error to chase. This book is about that layer, and how to make it correct, portable, tested, and safe.
WHY CHAT TEMPLATES MATTER MORE THAN EVER
Nearly every instruct model on Hugging Face ships a Jinja chat template. In Transformers, apply_chat_template() renders it; vLLM and SGLang render it on the server; llama.cpp runs its own copy embedded in the GGUF. As models gained tools, reasoning, and vision, that template grew from ten lines into a hundred-line program that breaks in new ways - and renders differently across engines. The role-tagged message list is the one stable contract underneath every modern LLM API. This book teaches you to render it correctly, everywhere.
NEW IN EDITION 1.2
Earlier printings tested the chat template as a rendered string. Edition 1.2 follows it down to the token IDs the model receives, on the engine you serve it from. A new chapter, The Template Behind the Endpoint, covers everything a server does between a request's JSON and those token IDs, with examples run against a live llama.cpp server. The chapters on authoring and training, tool calling, multimodal templates, and debugging have grown, and the errors in earlier printings are corrected.
WHAT YOU WILL LEARN
INSIDE THE BOOK
Thirteen chapters and six appendices, including a glossary of the book's sixteen named concepts, template reference cards, and an engine compatibility matrix.
WHO THIS BOOK IS FOR
Equal parts guide and reference, The Chat Templates Handbook turns the most overlooked layer in your LLM stack into one you can read, test, and trust. Every chapter leaves you with runnable code from Template Studio, the open-source toolkit built across the book.
Dr. Ranjan Kumar is a Professor in the Department of Physics at King Abdulaziz University, Jeddah, Kingdom of Saudi Arabia - on leave from Panjab University, Chandigarh, India. Dr. Kumar's field of specialization is theoretical condensed matter physics, and his research interests include: Fullerenes and carbon materials, Heusler alloys for spintronic applications, dilute magnetic semiconductors, electronic and thermoelectric properties of materials, and superconductors.
Readwise Books is an ebook & audiobook platform that automatically logs how much you and your friends read, making it fun and easy to read more. Learn more →
Enter your email to continue. We'll create your account if you're new here.