`max_seq_len` controls one bound, what happens to examples that exceed it?
max_seq_len caps token count per training sequence; overlong examples are right-truncated by default, which silently chops the assistant response and leaves the loss with no real labels.
Imagine packing lunches for a school trip into identical lunch boxes. Each box has a fixed capacity, and the rule is that whatever does not fit gets thrown away from the top. You pack the sandwich first, then the fruit on top, then the dessert. When a lunch is too big and the box overflows, the dessert disappears first. If the dessert was the part the child actually loves, the lunch is now useless even though the box still looks full. max_seq_len is the lunch box size for training. The assistant answer is the dessert, the part the model is supposed to learn from. When the loader trims the end, the answer goes first while the system prompt and user message stay. The training step still runs, the loss number still looks fine, but the model learns nothing from that example.
Detailed answer & concept explanation~10 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: define max_seq_len, walk default right truncation behavior on chat data, show how it silently zeros the loss mask, and cover the defensive recipe of percentile-based caps, truncation logging, left truncation, and packing.
Real products, models, and research that use this idea.
- Hugging Face TRL SFTTrainer exposes max_seq_length and defaults to right truncation, which is why Llama 4 instruction tuning recipes also log the percentage of examples that hit the cap each epoch.
- Axolotl provides a packed sequence option that bundles multiple short examples up to max_seq_len with cross-document attention masking, a default in the Mistral and Gemma 4 SFT configs.
- DeepSeek V4 distillation recipes pick max_seq_len at the 95th percentile of full example length and use left truncation when the prompt tail is dispensable but the response must survive.
- Unsloth tutorials walk users through inspecting the tokenized example length histogram before choosing max_seq_len for Llama 4 fine-tuning to avoid silent loss masking failures.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is logging truncation rate per epoch a critical signal, not just a nice to have metric?
QWhen does left truncation help and when does it hurt?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Setting max_seq_len based on average example length instead of the 95th percentile. You silently lose the assistant response from your longest examples and train on sequences that contribute nothing to the loss.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.