作者: Jakub Rusinowski · 最后更新: 2026年7月10日
Founder, LLM Configurator — AI educator & workshop leader on local LLM deployment
The same sentence again. The same sentence again. The same sentence again. The same sentence ag
The model answers normally for a while, then starts echoing the same phrase, sentence, or whole paragraph — sometimes drifting into a shorter and shorter cycle until it emits one word forever. It happens mid-generation, with no error message, on any hardware. Ollama, LM Studio, and llama.cpp are all affected because the cause lives in generation settings, not in the runtime.
LLMs pick each token from a probability distribution, and repetition is self-reinforcing: once a phrase appears twice, its tokens look even more likely the third time. Three things break the cycle in normal setups — the repeat penalty, healthy sampling randomness, and a correct stop-token configuration. When you see looping, one of those three is off: repeat_penalty is disabled or too weak, temperature/top_p are set so low that the model becomes deterministic, the conversation silently overflowed the context window (the model literally lost the thread), or a bad quant/template makes the model never emit its stop token. It is almost never a VRAM or GPU problem.
If looping starts only after a conversation has gone on for a while, you've likely overflowed the context window: the oldest messages get truncated away and the model, having lost the plot, falls back on echoing recent tokens. Ollama's default context is only 2048–4096 tokens depending on version and model — long chats blow past that fast. Raise it (at a VRAM cost) and the 'model got dumb and repetitive after 20 minutes' symptom disappears. Check how much context your VRAM can actually afford before setting it huge.
# Ollama: raise the context window for the session
ollama run llama3.1
>>> /set parameter num_ctx 8192
# or permanently in a Modelfile
PARAMETER num_ctx 8192
The repeat penalty multiplies down the probability of tokens that already appeared. 1.0 means disabled — if your setup ships with 1.0 (some LM Studio presets and raw llama.cpp defaults do), even good models loop. 1.1 is the sane default, 1.15–1.2 for loop-prone small models. Above ~1.3 you get a new disease: the model avoids normal words it already used and starts writing thesaurus soup. repeat_last_n controls how far back the penalty looks — 64 (a common default) only sees ~50 words; 256 catches paragraph-level cycles.
# Ollama (REPL or Modelfile)
>>> /set parameter repeat_penalty 1.15
>>> /set parameter repeat_last_n 256
# llama.cpp
./llama-cli -m model.gguf --repeat-penalty 1.15 --repeat-last-n 256
Temperature near 0 plus a tight top_p makes the model pick the single most likely token every step — and 'most likely next token' after a repeated phrase is more of the same phrase. If you turned temperature down to make answers more factual, don't go below ~0.5 for open-ended chat (temperature 0 is for one-shot extraction/classification tasks, not conversations). Sane chat baseline: temperature 0.7–0.8, top_p 0.9.
>>> /set parameter temperature 0.7
>>> /set parameter top_p 0.9
A model that finishes its answer and then keeps going — appending fake user turns, re-answering, degrading into loops — isn't repeating so much as failing to stop. That means the runtime isn't catching the model's end-of-turn token: classic with hand-built Modelfiles, community GGUFs with unusual templates, and fine-tunes. Make sure the template and stop parameter match the model family (for a fine-tune you converted yourself, see the GGUF conversion guide's Modelfile section).
# See what a correctly configured model uses:
ollama show llama3.1 --template
ollama show llama3.1 --parameters
# Modelfile for Llama-3-family models needs:
PARAMETER stop <|eot_id|>
Q2_K and unofficial "frankenquants" of small models are measurably more loop-prone — degraded weights flatten the distribution the sampler works with. If the same prompt loops at Q2/Q3 but not at Q4_K_M from an official source, the quant is your culprit. Re-download from the official Ollama library or a reputable Hugging Face repo, and treat sub-Q4 quants of sub-7B models as experimental.
Self-reinforcing token probabilities with nothing to break the cycle: repeat_penalty disabled or at 1.0, temperature too low, an overflowed context window, or a stop-token/template mismatch. Set repeat_penalty 1.1–1.15, temperature ~0.7, and num_ctx large enough for your conversation, and looping disappears in almost every case.
No — looping is a generation-settings problem, not a hardware one. The exception proving the rule: genuinely corrupted VRAM produces gibberish and crashes rather than clean repetition. If you see coherent text repeating, your hardware is fine.
1.1 as a default, 1.15–1.2 for small (under 7B) or heavily quantized models that loop more readily. Avoid 1.3+: the model starts dodging legitimate repeated words — names, technical terms — and quality drops in a different way.
That's the context window overflowing. Once the chat exceeds num_ctx, the runtime silently drops the oldest messages; the model loses coherence and leans on repeating recent text. Raise num_ctx (costs VRAM) or start fresh conversations more often.
Yes — LM Studio exposes the identical knobs (Repeat Penalty, Temperature, Top P, Context Length) in the chat settings sidebar, because both tools run llama.cpp underneath. Check its presets: some ship with repeat penalty at 1.0, which is "off."