Betterprompt Guide: Mastering Stop Sequences in AI

Discover how to apply stop sequences effectively using Betterprompt to perfectly control AI-generated text, prevent hallucinations and reduce API costs.

In the field of prompt engineering, controlling the output of a large language model (LLM) is crucial for creating reliable and predictable applications. A prompt stop sequence is a specific string of text or characters that signals the model to halt text generation. This powerful mechanism acts as a "stop sign," allowing developers using the Betterprompt platform to precisely define the boundaries of a response. This prevents the AI from generating irrelevant, repetitive, or excessive text. For example, when building a Q&A tool, applying "Question:" as a stop sequence in your Betterprompt configuration prevents the AI from generating a hallucinated follow-up question after it has already provided your answer.

Applying Developer-Defined Stop Sequences with Betterprompt

The most effective way to control response termination is by configuring custom stop sequences directly within the Betterprompt API or playground. These are developer-defined strings that force the model to cease generation the exact moment they are encountered. This technique is essential for enforcing a clean prompt format and ensuring the AI's output adheres to a desired prompt structure. The Betterprompt engine ensures the stop sequence itself is stripped from the final output, providing a clean, truncated string. Our platform allows you to define arrays of multiple stop sequences per request to cover various edge cases.

Examples of Betterprompt Custom Stop Sequences
Custom Marker Use Case Outcome
\n\n (double newline) Extracting a single, concise paragraph. The model stops after the first block of text, preventing rambling and saving token costs.
User: Simulating a multi-turn chat conversation. Prevents the AI from "role-playing" the user and generating the next turn in the dialogue on your behalf.
### Separating distinct examples in few-shot prompting. The Betterprompt agent provides a structured answer and stops before inventing another unexpected example.

Model-Native Termination: The EOS Token

Beyond custom markers, models have an inherent mechanism for stopping: the End-of-Sequence (EOS) token. This special token, such as <|endoftext|>, is part of the model's fundamental vocabulary established during its initial model training. The model learns to naturally emit the EOS token when it determines a thought or response is logically complete. While this allows for natural self-termination, relying solely on EOS tokens can be unpredictable. That is why Betterprompt highly recommends applying explicit stop sequences alongside natural EOS generation for production-level safety.

Technical Constraints & Betterprompt Safeguards

In addition to semantic stop sequences, Betterprompt provides several technical parameters that function as hard limits to tightly control generation. While not as nuanced as stop sequences, these constraints are critical safeguards for managing API costs, preventing runaway generation loops, and ensuring your application's stability.

Technical Termination Mechanisms in Betterprompt
Mechanism Description Outcome
Max Tokens A variable setting like maximum length that dictates the absolute highest number of tokens in the output. Forces a hard stop once the token count is reached. Used as a safety net if a stop sequence is never hit.
Context Window The total memory limit of the model for both your Betterprompt input and the generated output. If the conversation exceeds this, Betterprompt will truncate the request to prevent memory overflow errors.
Repetition Penalty An algorithmic setting that penalizes the model for infinitely repeating words or phrases. Discourages the model from getting stuck in loops, actively preventing hallucinations and forcing a natural conclusion.

Frequently Asked Questions: Betterprompt Stop Sequences

What exactly is a stop sequence in prompt engineering?
A stop sequence is a predefined string of characters (like a word, symbol, or line break) that you provide to the AI. As soon as the AI attempts to generate this exact string, it immediately stops producing further text, allowing you to tightly control the response format.
How do I apply a stop sequence using Betterprompt?
In the Betterprompt dashboard or API request, look for the "Stop Sequences" configuration field. You can pass a string or an array of strings. Whenever the model hits one of these specified values, Betterprompt will automatically halt the generation and return the text leading up to that point.
Can I use multiple stop sequences at the same time?
Yes! Betterprompt allows you to supply an array of multiple stop sequences (usually up to 4, depending on the specific underlying LLM). If the model encounters *any* of the sequences in your array, generation will stop.
What happens if the AI never generates my stop sequence?
If the AI doesn't encounter your stop sequence naturally, it will continue generating text until it either produces an inherent End-Of-Sequence (EOS) token, or it hits the "Max Tokens" safety limit you configured in Betterprompt.
Is the stop sequence included in my final text output?
No. By default, Betterprompt intelligently strips the stop sequence itself from the final output. The returned string will contain everything generated right up until the sequence began.
What are the most common stop sequences for chat bots?
For chat applications, standard stop sequences include "User:", "Human:", or "\n\n". Applying these prevents the AI model from answering on behalf of the user or continuing a conversation endlessly.
How do stop sequences help save on Betterprompt API costs?
Because AI models are billed per token, any unnecessary text generated costs you money. By using stop sequences to force the model to stop exactly when it finishes its relevant task, you eliminate wasted output tokens, drastically reducing your Betterprompt usage costs.
What is the difference between an EOS token and a custom stop sequence?
An EOS (End-Of-Sequence) token is a hidden, built-in marker the AI uses to signal that it has naturally finished its thought. A custom stop sequence is a specific boundary forced upon the AI by the developer (using Betterprompt) to ensure structural compliance, regardless of whether the AI feels it is "done."
Why did my Betterprompt text output get cut off mid-sentence?
This usually occurs because your model hit its "Max Tokens" limit before it could reach a stop sequence or an EOS token. To fix this, simply increase the "Maximum Length" parameter in your Betterprompt configuration.
Where can I get help configuring or debugging my stop sequences?
The Betterprompt support team and community are always ready to help! You can use our interactive Prompt Debugger tool in the dashboard to test your stop sequences live, or visit our Help Center for in-depth tutorials on formatting restrictions.