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