def _update_prefix(prefix_len, current_prefix, new_word):
    """Remove first word from a prefix, and adds a new word to the end.

    If the prefix contains fewer than prefix_len words,
    as it will at the very beginning of the chain,
    don't pop off the first word.
    """
    prefix_words = current_prefix.split()
    # pop off first word, unless prefix is shorter than prefix_len
    if len(prefix_words) == prefix_len:
        prefix_words.pop(0)
    elif len(prefix_words) > prefix_len:
        raise ValueError("Prefix is too long!")

    # add current word to prefix
    prefix_words.append(new_word)
    new_prefix = " ".join(prefix_words)

    return new_prefix.strip()