Skip to main content
Lesson 15 of 16 · Chat Completions: Mastering the API

N completions

Lesson 15 of 16 Updated November 2, 2026 Developer
On this page
  1. Generate multiple responses in a single call
  2. Select the best response
  3. Compare two prompts on a sample
  4. Improve classification reliability via voting
  5. When to use n > 1
  6. Key takeaways

Generate multiple responses in a single call

The n parameter instructs the model to produce multiple alternative responses to the same prompt within a single API request. While this might seem like a minor feature, it enables three serious use cases: comparing two prompt formulations on a sample set, automatically selecting the best response from a batch, and exploring the solution space rather than accepting the first output.

By default, n=1, and the API returns a single response in choices. If you set n=5, the array contains five entries, each with its own index. Note the temperature set to 1.0 in the example below: without sufficient randomization, all five responses would be identical, rendering the call useless.

from mistralai.client import Mistral
import os

client = Mistral(api_key=os.getenv("MISTRAL_API_KEY"))

response = client.chat.complete(
    model="ministral-3b-latest",
    messages=[
        {"role": "user", "content": "What is the best programming language for beginners? One word only."}
    ],
    temperature=1.0,  # High temperature to maximize diversity
    n=5
)

for choice in response.choices:
    print(f"Response {choice.index}: {choice.message.content}")

Two factors determine the practical value of this parameter. The first is billing: input tokens are charged only once since the prompt is sent a single time, but output tokens are charged for every completion. With n=5 and max_tokens=100, your output cap jumps to 500 tokens. This is precisely what makes n attractive for long prompts—amortizing a large context across five responses costs significantly less than making five separate calls.

print(f"Prompt tokens  : {response.usage.prompt_tokens}")     # Charged 1x
print(f"Response tokens: {response.usage.completion_tokens}")  # Charged nx
print(f"Total          : {response.usage.total_tokens}")

The second factor is compatibility: not all Mistral models support n. As of April 2026, recent versions of mistral-large do not support n > 1. Use ministral-3b-latest or other compatible models for this feature—which is why all examples in this lesson rely on that model.

Select the best response

The first use case involves generating multiple candidates and retaining the best one based on a criterion you define. The mechanism is intentionally generic: best_of_n accepts any scoring function, allowing you to measure what matters in your context—presence of a mandatory keyword, JSON validity, or adherence to a target length. The length_scorer function illustrates this last criterion, where the score decreases proportionally to the deviation from the target length.

def best_of_n(client, messages, n=5, scorer=None, **kwargs):
    """Generates N responses and selects the best one."""
    response = client.chat.complete(
        model="ministral-3b-latest",
        messages=messages,
        n=n,
        temperature=0.9,
        **kwargs
    )

    candidates = [
        {
            "index": c.index,
            "content": c.message.content,
            "score": scorer(c.message.content) if scorer else 0
        }
        for c in response.choices
    ]

    if scorer:
        candidates.sort(key=lambda x: x["score"], reverse=True)

    return candidates

# Scorer: appropriate length (not too short, not too long)
def length_scorer(text: str, target=200) -> float:
    """Score based on proximity to a target length."""
    diff = abs(len(text) - target)
    return max(0, 1 - diff / target)

candidates = best_of_n(
    client,
    [{"role": "user", "content": "Write a summary of generative AI in 2026."}],
    n=5,
    scorer=length_scorer,
    max_tokens=300
)

print("Best response:")
print(candidates[0]["content"])
print(f"Score: {candidates[0]['score']:.2f}")

Compare two prompts on a sample

The second use case is methodological rather than product-focused. When deciding between two prompt formulations, comparing a single response to another proves nothing: model variance alone could explain the difference. By generating N responses for each version and measuring average length and the rate of distinct responses, you obtain preliminary evidence. A diversity score of 1.0 on a prompt intended to produce a stable format is a red flag, not good news.

def ab_test_prompts(client, prompt_a: str, prompt_b: str, n: int = 5):
    """Compares two prompts by generating N responses for each."""
    results = {}

    for label, prompt in [("A", prompt_a), ("B", prompt_b)]:
        response = client.chat.complete(
            model="ministral-3b-latest",
            messages=[{"role": "user", "content": prompt}],
            n=n,
            temperature=0.8,
            max_tokens=200
        )

        texts = [c.message.content for c in response.choices]
        avg_length = sum(len(t) for t in texts) / len(texts)
        unique_ratio = len(set(texts)) / len(texts)

        results[label] = {
            "avg_length": round(avg_length),
            "diversity": round(unique_ratio, 2),
            "sample": texts[0][:150]
        }

    return results

# Test
results = ab_test_prompts(
    client,
    "Summarize the benefits of Python.",
    "List the 3 main benefits of Python in one sentence each."
)

for label, data in results.items():
    print(f"\nPrompt {label}:")
    print(f"  Average length : {data['avg_length']} chars")
    print(f"  Diversity      : {data['diversity']}")
    print(f"  Sample         : {data['sample']}...")

Improve classification reliability via voting

The third use case offers the highest ROI in production. For a classification task, a single response tells you nothing about the model’s confidence: the answer might be obvious, or it might be a borderline guess between two categories. Generate seven responses, count the votes, and you obtain both a label and an actionable confidence rate—a 4/7 classification can be routed for human validation, while a 7/7 passes directly through. Using an odd number avoids ties.

from collections import Counter

def majority_vote(client, messages, n=7, **kwargs):
    """Majority vote classification over N completions."""
    response = client.chat.complete(
        model="ministral-3b-latest",
        messages=messages,
        n=n,
        temperature=0.7,
        max_tokens=20,
        **kwargs
    )

    votes = [c.message.content.strip().upper() for c in response.choices]
    counts = Counter(votes)
    winner, count = counts.most_common(1)[0]
    confidence = count / n

    return {
        "result": winner,
        "confidence": round(confidence, 2),
        "votes": dict(counts),
        "n": n
    }

# Sentiment classification
result = majority_vote(
    client,
    messages=[
        {"role": "system", "content": "Classify the sentiment: POSITIVE, NEGATIVE, or NEUTRAL. Reply with one word only."},
        {"role": "user", "content": "The product is okay but the delivery was very slow."}
    ],
    n=7
)

print(f"Result    : {result['result']}")
print(f"Confidence: {result['confidence']:.0%}")
print(f"Votes     : {result['votes']}")

When to use n > 1

This parameter isn’t justified everywhere: on standard traffic, every additional completion is an output cost without return. The table below summarizes defensible values based on the scenario.

ScenarioRecommended nWhy
Standard production1Minimal cost
Quality selection3-5Best-of-N for critical text
Robust classification5-9 (odd)Majority vote for reliability
Prompt A/B testing5-10Sufficient sample size for comparison
Brainstorming5-10Maximum idea diversity

Key takeaways

  • The n parameter generates multiple responses in a single API call
  • Input tokens are charged once; output tokens are multiplied by N
  • Not all models support n > 1—verify compatibility
  • Majority voting improves classification reliability
  • Best-of-N selects the best response based on a measurable criterion
  • Use high temperature with n > 1 to maximize response diversity