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

Basic Parameters

Lesson 3 of 16 Updated November 2, 2026 Developer
On this page
  1. Configure Your API Calls
  2. The model Parameter
  3. The max_tokens Parameter
  4. The temperature Parameter
  5. The top_p Parameter
  6. Complete Example with All Parameters
  7. Key Takeaways

Configure Your API Calls

Beyond the required model and messages fields, the Chat Completions API accepts a handful of parameters that control generation behavior. These settings distinguish a data extractor that returns identical results for the same invoice from a slogan generator that never repeats itself. The calling code remains unchanged from one use case to the next; only the settings shift, which is exactly why you need to understand what each one does.

Here are the parameters you will handle daily, along with their types and default values.

ParameterTypeDefaultDescription
modelstring—Model identifier (required)
messagesarray—Array of messages (required)
max_tokensintegervariableMaximum number of output tokens
temperaturefloat0.7Controls creativity (0.0 to 1.5)
top_pfloat1.0Nucleus sampling (0.0 to 1.0)
streambooleanfalseEnables SSE streaming
safe_promptbooleanfalseEnables safety guardrails

The model Parameter

Your choice of model immediately determines the capabilities, speed, and cost of every call—three trade-offs you cannot recover elsewhere. As of April 2026, the primary models are:

# Flagship model — complex reasoning, multilingual
model = "mistral-large-latest"

# Compact model — fast, economical, ideal for production
model = "mistral-small-latest"

# Miniature model — ultra-fast, edge computing
model = "ministral-3b-latest"

# Code model — specialized for programming
model = "codestral-latest"

The -latest suffix is convenient during development: it always points to the newest version without requiring code changes. In production, however, this convenience becomes a risk, as an update on Mistral’s side can subtly shift your application’s behavior. Prefer versioned identifiers like mistral-large-2512 to guarantee the reproducibility of your tests.

The max_tokens Parameter

This parameter sets the upper limit on the number of tokens the model can generate in its response. It does not force the model to produce exactly that number; it defines a ceiling, nothing more.

response = client.chat.complete(
    model="mistral-large-latest",
    messages=[{"role": "user", "content": "Summarize this text in one sentence."}],
    max_tokens=100  # The response will not exceed 100 tokens
)

If the model hits this limit, the response cuts off abruptly, and finish_reason returns "length" instead of "stop". This is the most common cause of responses that stop mid-sentence, and no one notices until they monitor this field. The right reflex is to calibrate the ceiling to the task: classification fits in 10 to 50 tokens since you expect only a label, a summary needs 200 to 500 tokens, free-form writing takes 1,000 to 4,000, and code generation easily reaches 2,000 or 8,000 tokens because a full file is long.

The temperature Parameter

Temperature controls the degree of randomization in token selection. The lower the value, the more deterministic and predictable the model becomes; the higher it goes, the more it attempts improbable continuations. For factual questions, you want the former approach; for naming exercises, the latter.

# Low temperature — extraction, classification, factual
response = client.chat.complete(
    model="mistral-large-latest",
    messages=[{"role": "user", "content": "What is the capital of France?"}],
    temperature=0.1
)

# High temperature — creativity, brainstorming
response = client.chat.complete(
    model="mistral-large-latest",
    messages=[{"role": "user", "content": "Invent a name for an AI startup."}],
    temperature=1.2
)

The range spans from 0.0 to 1.5. Do not rely on temperature=0 for perfect determinism: slight variations persist due to hardware-level floating-point rounding.

The top_p Parameter

Nucleus sampling, or Top P, acts on the same lever but via a different path: it limits candidate tokens by a cumulative probability threshold. With top_p=0.5, only the most probable tokens whose summed probabilities reach 50% remain in contention; the rest are discarded outright.

response = client.chat.complete(
    model="mistral-large-latest",
    messages=[{"role": "user", "content": "Complete: The cat is on the..."}],
    temperature=0.7,
    top_p=0.9  # Considers tokens covering 90% of the probability
)

Mistral recommends adjusting only one parameter at a time, either temperature or top_p, never both together. The reason is practical: if an output becomes strange after moving both sliders, nothing tells you which one caused it, and you lose both debuggability and reproducibility.

Complete Example with All Parameters

Combined into a single call, these settings create a slogan generator that is intentionally brief and slightly bold, while also tracking token consumption:

from mistralai.client import Mistral
import os

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

response = client.chat.complete(
    model="mistral-large-latest",
    messages=[
        {
            "role": "system",
            "content": "You are a concise and punchy marketing writer."
        },
        {
            "role": "user",
            "content": "Write a slogan for a meditation app."
        }
    ],
    max_tokens=50,
    temperature=0.9,
    top_p=1.0
)

print(response.choices[0].message.content)
print(f"Tokens used: {response.usage.total_tokens}")

Run this script three times in a row, then replace temperature=0.9 with 0.1 and run it three more times. The difference between the two series will teach you more about this parameter than any definition could.

Key Takeaways

  • model and messages are the only required parameters
  • max_tokens is a ceiling, not a target—check finish_reason to detect truncation
  • Use low temperature for factual tasks, high for creativity
  • top_p refines token distribution—do not adjust it simultaneously with temperature
  • Use versioned model identifiers in production for reproducibility