Zenaique

Your input is 9,000 tokens but max_length is 8,192. What are your truncation options?

Flashcard·Easy·4.0 · 0·~30s·Asked atPhonepeQdrantTata Digital·Relevant atOpenAI
Attempt it
TL;DR

Truncation drops tokens from an over-long input so it fits max_length; HF strategies are longest_first, only_first, only_second, do_not_truncate. Silent truncation is the recurring production bug.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine a backpack that fits ten books. Someone hands you fifteen. You have to leave some behind, but you cannot leave any specific book at random. A smart traveler thinks about which books matter: the map at the top stays, the trip notes at the bottom stay, and the duplicates in the middle get left at the hotel. Truncation in tokenization is the same calculation. The backpack is the model's context window. The books are tokens. The strategy is the rule for which tokens to leave behind. And if you never tell the traveler the bag was overstuffed, they leave things behind silently and you only notice at the destination.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Truncation is the tokenizer's answer to a hard constraint. The model has a maximum context length, and any input over that limit has to either be cut or rejected. The key choices are which side to cut from, which API surface implements the cut, and how to make sure the cut does not happen silently.

What truncation is and what forces it

Every model has a maximum context length. For Llama 3 it is 8k for the base 8B model and 128k for the long-context fine-tunes. GPT-5.5 and Claude Opus 4.7 offer 1M tokens through the API, and Gemini 3.1 Pro offers about 1M. These are hard architectural and serving limits, not soft suggestions: an input that exceeds the limit cannot be processed at all.

The tokenizer is the layer that enforces this. The application calls tokenizer(text, truncation=True, max_length=N) and the tokenizer guarantees the returned input_ids will be at most N. To honor that guarantee, it drops tokens above N.

Truncation also applies to budgets smaller than the model's full limit. Production systems usually keep some headroom for the response. Even a 128k model might be configured to truncate inputs at 100k to leave 28k for generation. The same machinery applies.

The four named HuggingFace strategies
Manual middle truncation for chat
Silent truncation is the production bug class
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • HuggingFace tokenizer(text, truncation=True, max_length=8192) uses longest_first by default and returns overflowing_tokens if you ask for them.
  • LangChain and LlamaIndex chat memory implementations apply manual middle truncation against a token budget computed from the model's context window.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy is longest_first the default and when is it the wrong choice?
A

It was designed for question plus context pairs in pretraining era encoder workloads. There, both sides are roughly equally important and you want them both present. It is wrong when one side is structurally precious (the question) and the other side is the place to absorb cuts (the context).

1 more follow-up an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Calling tokenizer(text, truncation=True) and never logging when truncation fires. The tokenizer silently drops tokens past max_length and answers degrade with no obvious signal.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Define truncation and why max_length makes it necessary.

  • Name the four HuggingFace truncation strategies and what each one does.

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Why use subword tokens, and what is wrong with word level or character level?
Flashcard·Easy