Overview

A completion is the output a model generates in response to a prompt. The term comes from early text-completion APIs; chat APIs still call the generated message a completion.

Check why generation stopped before using the text. In the Claude API stop_reason is end_turn (finished), max_tokens (truncated), model_context_window_exceeded (truncated), stop_sequence, tool_use (a tool call awaits execution), pause_turn, or refusal. A max_tokens or model_context_window_exceeded stop means the answer is incomplete: raise the limit, trim the input, or split the task. Treat the text as untrusted output: parse and validate it before acting on it (see structured-output).

Two request parameters bound a completion: max_tokens sets a hard length ceiling and stop_sequences ends it at a marker (stop-sequence). Sampling parameters such as temperature accept only their defaults on current Claude models; see temperature.

Example

msg = client.messages.create(
    model="claude-sonnet-5-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Explain backpressure in one paragraph."}],
)
if msg.stop_reason == "max_tokens":
    ...  # truncated: raise max_tokens or split the task