Guides · Series · 13 parts
Chat Templates in Practice
A chat template is the contract between your messages and the exact token stream the model was trained on. Get it wrong and nothing raises — the model just gets quietly worse.
The bug that never raises
Most prompt bugs are loud. A wrong chat template is not. Render a conversation into the wrong format — a missing generation prompt, a stray newline, a system turn the model never saw in training — and apply_chat_template returns a string, the model returns an answer, and every log line looks healthy. The only symptom is that the model is a little worse than its benchmark, and nobody on the team can say why.
That is the gap this series is about: between the list of messages your code holds and the single token stream a specific model actually receives. The chat template sits in that gap. It is a Jinja program that ships with the weights, runs on every request, and is trusted completely by everything around it.
How the series works
Each part is a tutorial you run, not an essay you read. You start from the real template of a small open model, build one working piece — a renderer, a validator, a probe, a parser, a cross-engine diff, a golden-token test — and see the failure it catches in your own output.
Every part maps to one chapter of The Chat Templates Handbook. The tutorial teaches the core idea and the runnable slice; the chapter carries the variants, the edge cases, and the full module, and its tag in the companion repo holds the finished code.
Which part solves my problem
The series reads in order, but if something is already broken, start here.
- “The model is worse through my code than in its own demo, and nothing errors.”Part 1 →
- “A conversation renders fine and the model still answers as if a turn were missing.”Part 2 →
- “My local render and the production render differ by whitespace I cannot explain.”Part 3 →
- “I need to know what a model’s template does without reading 200 lines of Jinja.”Part 4 →
- “My agent leaks raw tool-call JSON to users instead of calling the tool.”Part 5 →
- “Multi-turn latency jumped after we turned on thinking mode.”Part 6 →
- “The vision model fails with a placeholder or image-count mismatch.”Part 7 →
- “My fine-tuned model behaves differently when served than it did in training.”Part 8 →
- “The same model gives different answers in llama.cpp and in Transformers.”Part 9 →
- “I cannot prove which token IDs my inference server actually sent the model.”Part 10 →
- “A tokenizer or template update shipped and nobody noticed the prompt changed.”Part 11 →
- “I pulled a model from the Hub and never reviewed the code in its template.”Part 12 →
- “I want one CI check that fails when anything about rendering changes.”Part 13 →
The series
13 of 13 published
How to See What apply_chat_template Sends Your Model
Render a real conversation through Qwen3's own chat template, make every newline visible, and find the exact character where a hand-built prompt goes wrong.
How to Validate Chat Messages Before apply_chat_template
Watch Qwen3's chat template silently drop a turn whose role is "User", then build a validator and a safe_render that refuse a malformed conversation before it reaches the model.
How to Render a Chat Template with Jinja Like Transformers
Rebuild the Jinja environment apply_chat_template uses, watch one missing dash add a token to every assistant turn, and prove your local render matches Qwen3 byte for byte.
How to Check if a Chat Template Supports a System Prompt
Build a Python probe that inspects a Hugging Face chat template without reading its Jinja: the generation prompt, and whether a system or developer message gets its own turn, on Qwen3, Llama 3 and Gemma 3.
How to Pass Tools to apply_chat_template and Parse the Calls
Build a Python module that sends tools and tool calls through apply_chat_template, then reads the model's calls back out: with the model's own response template when it ships one, and with a parser you choose when it does not.
How to Pass reasoning_content Back Through a Chat Template
Build a Python module that turns thinking on and off per model, splits a reply into reasoning and answer, stores the reasoning where the template reads it, and proves with token IDs that the next request repeats what the model saw.
How to Pass Images to apply_chat_template with a Processor
Build a Python module that renders image-and-text conversations through a Hugging Face processor, validates every content part, rewrites OpenAI image_url parts without touching your data, and checks that placeholders and images pair up before the model sees them.
How to Write a Chat Template for a Fine-Tuned Model and Test It
Write a chat template from the exact text your model was fine-tuned on, then prove it with string, token, mask, label and stop-token checks against a real TRL SFTTrainer batch.
How to Test a Chat Template Across Jinja Engines
Render one conversation with CPython Jinja2 and with @huggingface/jinja, find the first character where they differ, and lint a template for the constructs that do not port.
How to Check the Token IDs llama-server Sends the Model
Ask llama-server for the token IDs it builds from your conversation, compare them with apply_chat_template, and catch a doubled BOS token and a broken prefix cache before your users do.
How to Catch Chat Template Regressions with Golden-Token Tests
Record what a chat template renders and the token IDs your engine sends for a fixed set of conversations, then fail CI on any change: a whitespace edit, a misspelled switch, or a doubled BOS.
How to Detect Chat Template Backdoors and Forged Tokens
Pin a model's chat template by fingerprint, scan its syntax tree for backdoor shapes, and stop control tokens in user and tool content from becoming a fake turn.
How to Gate Chat Template Changes in CI with One Audit
Compose golden tokens, a fingerprint pin, a backdoor scan and Jinja parity into one audit and one CI exit code, so adding a model is a config entry, not a new rendering path.
The companion code
Template Studio is the toolkit the book builds one module per chapter — render, validate, probe, parse, diff across engines, golden-token tests, and a CI gate. Every chapter has its own tag, so each tutorial points at the exact code it ends with.
View the companion repository on GitHub →Other guide series
GenAI Architectures
There are eight architectures and seven boundaries between them. Every boundary is a yes-or-no question about the task — not a judgment call about the design.
Agentic AI with LangGraph
An eight-part guide to the part of agent engineering nobody demos: making the thing survive a restart, a concurrent write, a bad tool call, and a Tuesday deploy.
Harness Engineering
AI systems do not fail at the model layer. They fail in the code around it — and that code is a designable layer with its own architecture.
Harness Engineering: The Book
The harness is a control system. Every component in it is a sensor, a comparator, a gate, or a bound — and anything else is application code.
RAG Engineering
Most RAG failures happen upstream of the LLM, accumulate invisibly, and only surface as an incident. This series names each one.
Claude Code Playbook
Claude Code has four layers — context, skills, subagents, hooks — and the variable that decides which one you need is enforceability, not capability.
AI Control Plane
A fleet of agents needs a layer that is not itself an agent — observability, policy, orchestration, versioning, cost, and audit, built as infrastructure.
Agentic AI in the Data Center
In physical infrastructure, an agent’s autonomy is capped by reversibility and jurisdiction — not by how good the model is.
Codebase to Architecture Doc
A three-pass methodology for compressing any codebase into validated diagrams and decisions — and then a LangGraph pipeline that runs it for you.
Federated AI Agents
Every call that crosses from one owner to another is a seam, and a seam has five properties — contract, authority, state, failure, and accountability — that are free inside one deployment and have to be built across it.






