N Completions and Stop Sequences
On this page
Generate multiple responses in a single request
To wrap up this section on generation parameters, we look at two practical control tools: the N completions parameter, which lets you request multiple responses from the same prompt in a single API call, and stop sequences, which determine where generation stops. The first helps you compare alternatives, increase your chances of getting an optimal response, or feed an automatic selection process; the second gives you control over the length and format of outputs.
The n parameter: how it works
When you set n=5, the model generates 5 independent responses for your prompt, each returned as a distinct choice in the response object. One billing point deserves your attention before using it heavily: input tokens—your prompt—are billed only once, but output tokens are billed for each completion. With n=5, you pay five times for output tokens, but only once for the prompt: for a long prompt and short responses, this is significantly more economical than five separate calls.
from mistralai.client import Mistral
client = Mistral(api_key="your-api-key")
response = client.chat.complete(
model="mistral-large-latest",
messages=[
{"role": "user", "content": "Propose a slogan for a French organic coffee brand."}
],
n=5,
temperature=0.8
)
for i, choice in enumerate(response.choices):
print(f"Option {i+1}: {choice.message.content}")
This code produces 5 different slogans, from which you choose the best. This is the first major use of n: creative generation, where getting multiple options for a slogan, title, or product name beats a single attempt. The second is classification with voting: you generate N classifications of the same text and keep the majority response, which smooths out occasional errors. You also use it for exploration—testing several approaches to the same problem—and for quality control, by checking model consistency by comparing its own responses.
Know three limits before deploying. First, not all Mistral models support n greater than 1: check the documentation for the model you use. Next, latency increases with n, since the model generates more tokens in total. Finally—and this is a classic beginner mistake—always combine n > 1 with sufficient temperature (0.5 and up): at temperature 0, your five responses will be nearly identical and you will have paid five times for the same sentence.
Stop sequences: control where generation ends
Stop sequences are character strings that force the model to stop as soon as it generates them. This is an essential mechanism to control the length and format of responses. Let’s start with a deliberately simplistic example to establish the concept:
response = client.chat.complete(
model="mistral-large-latest",
messages=[
{"role": "user", "content": "List the days of the week, one per line."}
],
stop=["Saturday"]
)
Here, the model stops as soon as it generates the word “Saturday,” producing a truncated list. In practice, stop sequences are most useful with structural delimiters. First use case: extract a single section from a structured response. If your prompt requests an analysis in three parts, stopping generation at the second heading gives you only the first:
response = client.chat.complete(
model="mistral-large-latest",
messages=[
{"role": "user", "content": """Analyze this product:
## Strengths
(your analysis here)
## Weaknesses
(your analysis here)
## Conclusion
(your analysis here)"""}
],
stop=["## Weaknesses"]
)
# The model stops after the strengths
Second case: limit generation to a JSON block, combining the stop sequence with a pre-fill of the assistant message that opens the brace:
response = client.chat.complete(
model="mistral-large-latest",
messages=[
{"role": "user", "content": "Return the metadata for this article in JSON."},
{"role": "assistant", "content": "{", "prefix": True}
],
stop=["}"]
)
# The model generates the JSON content and stops at the closing brace
Third case: prevent the model from continuing after its response. For a closed question, stopping at the first line break guarantees a dry “YES” or “NO,” without a justification paragraph you did not request:
response = client.chat.complete(
model="mistral-large-latest",
messages=[
{"role": "user", "content": "Answer the following question with YES or NO only.\nIs the Earth round?"}
],
stop=["\n"]
)
The max_tokens parameter
Complementary to stop sequences, max_tokens sets a hard limit on the number of output tokens:
response = client.chat.complete(
model="mistral-large-latest",
messages=[
{"role": "user", "content": "Summarize the history of France."}
],
max_tokens=200
)
Watch its behavior: max_tokens cuts the response abruptly, potentially mid-sentence—your summary might end with “and thus Napoleon.” Treat it as a safety net against endless responses, not as the primary way to control length. For that, prefer instructions in the prompt (“answer in 3 sentences”) combined with a generous max_tokens.
Combine parameters
These mechanisms shine when used together. The following example generates 5 different article titles, each limited to a single line thanks to the stop sequence, with a 50-token ceiling as a safeguard:
response = client.chat.complete(
model="mistral-large-latest",
messages=[
{
"role": "system",
"content": "You generate blog article titles. One title per response."
},
{
"role": "user",
"content": "Theme: workplace productivity in 2026"
}
],
n=5,
temperature=0.8,
max_tokens=50,
stop=["\n"]
)
titres = [choice.message.content.strip() for choice in response.choices]
for i, titre in enumerate(titres):
print(f"{i+1}. {titre}")
Key points to remember
- The
nparameter generates multiple responses in a single call (input tokens billed once) - Combine
n > 1with sufficient temperature to get real variations - Stop sequences halt generation at a specific delimiter
max_tokensis a safety net, not a primary length control- Combine these parameters for fine-grained generation control