Runs fine, output is wrongWindowsLinuxmacOSiPhone & iPadLast updated November 6, 2026

Local LLM repeating itself or stuck in a loop — how to fix it

It says the same sentence over and over until I stop it

Written by Jakub Rusinowski

The error

The same sentence again. The same sentence again. The same sentence again. The same sentence ag
Which one is it?
If you seeThe cause isGo to
Looping starts only after a long conversationThe context window overflowed and early messages were droppedFix 1: raise num_ctx
It loops from the first answer, repeating phrasesThe repeat penalty is off or too weakFix 2: repeat_penalty 1.1–1.2
Every regeneration produces the same text word for wordTemperature is at or near 0Fix 3: raise temperature and top_p
It finishes, then writes a fake user turn and continuesThe stop token is not being caughtFix 4: check the stop tokens
It loops at Q2 or Q3 but not at Q4_K_MThe low quant has degraded the modelFix 5: use a better file

When you see it

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.

  • A long chat on a small context window, well past the point where the oldest messages are dropped.
  • A raw llama.cpp command or LM Studio preset with the repeat penalty at 1.0.
  • Temperature set to 0 for a conversational task, to make answers more factual.
  • A small model at Q2 or Q3, or an unofficial quant of unknown origin.
  • A phone app with a short context, where conversations reach the limit sooner than on a desktop.

What's actually going on

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.

How to fix it

  1. 1

    Rule out context overflow first — the silent cause in long chatsMost common fix

    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 depends on the version: older releases used 2048 or 4096 tokens, and current ones choose 4K, 32K or 256K from your VRAM — so on a smaller card long chats still blow past it 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.

    bash
    # 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
    Did it work?The CONTEXT column of ollama ps shows the larger value, and a conversation of the same length no longer degrades.
    Check what fits your hardwarecheck how much context your VRAM allows for your modelOpen the VRAM checker →
  2. 2

    Set repeat_penalty to 1.1–1.2 (and repeat_last_n to 256)

    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.

    bash
    # 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
    Did it work?Regenerate the same prompt a few times: the phrase-level loop should not return. If you set the values in a Modelfile, ollama show <model> --parameters lists them.
  3. 3

    Raise temperature and top_p out of the determinism zone

    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.

    bash
    >>> /set parameter temperature 0.7
    >>> /set parameter top_p 0.9
    Did it work?Regenerating the same prompt now gives different wording each time while staying on topic.
  4. 4

    Check the stop tokens if the model rambles past a finished answer

    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).

    bash
    # 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|>
    Did it work?Answers end at the end of the assistant's turn, with no invented user messages after them.
  5. 5

    Suspect the file itself: very low quants and broken GGUFs loop more

    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.

If none of this worked

If none of the settings help and the output contains leaked template tags, the problem is the chat template rather than sampling. If the looping arrived together with crashes or out-of-memory errors, treat it as a memory problem first.

Include this when you report it
  • Output of ollama show <model> --parameters, or your LM Studio preset values
  • The model tag or file name, including the quant
  • The context length, and roughly how long the conversation was when looping began
  • A short excerpt showing where the repetition starts
Also affects: Linux · Apple

Related

Frequently asked questions

Why does my local LLM repeat the same sentence over and over?

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.

Is repetition a sign my GPU or VRAM is failing?

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.

What is the best repeat_penalty value?

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.

Why does the model only start looping late in long conversations?

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.

Do these settings work the same in LM Studio?

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."