← Blog

Claude API Output Length Control: Tips That Work

2026-11-18 · 5 min read · SubToAPI Team

Controlling how long Claude's responses are is one of the most common pain points developers hit once they move past basic prompting. You either get responses cut off mid-sentence, or you get long rambling answers when you wanted three bullet points. Both problems come down to the same handful of levers: the max_tokens parameter, stop sequences, prompt instructions, and how you detect and handle truncation.

The short answer: set max_tokens to a value that matches your expected output, use explicit length instructions in your prompt or system prompt, add stop_sequences for structured formats, and always check the stop_reason field in the response to know whether Claude finished naturally or got cut off. The rest of this article walks through each of these in detail, with examples you can apply directly.

Understanding why output length goes wrong

Claude doesn't have a built-in sense of "write me a short answer" unless you tell it. Two separate mechanisms control length, and conflating them causes most of the confusion:

  1. max_tokens — a hard ceiling on the API side. If Claude hits this limit mid-thought, the response is truncated and stop_reason comes back as "max_tokens".
  2. Prompted length guidance — instructions in your system prompt or user message that tell the model how much to write. This is a soft constraint; Claude tries to comply but isn't guaranteed to hit an exact word count.

If you only set max_tokens without prompting for brevity, you'll get truncated long answers. If you only prompt for brevity without setting max_tokens appropriately, you rely entirely on the model's judgment, which varies by task complexity.

Set max_tokens deliberately, not as an afterthought

A lot of integrations leave max_tokens at some default copied from a tutorial — often 1024 or 4096 — regardless of what the task needs. Instead, size it to the task:

curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 150,
    "messages": [
      {"role": "user", "content": "Summarize this paragraph in two sentences: ..."}
    ]
  }'

For a two-sentence summary, 150 tokens is plenty — roughly 100-120 words max. For a full report generation, you'd set it much higher. The key habit: every time you write a new prompt template, estimate the expected output length in tokens (roughly 0.75 tokens per English word) and set max_tokens with some margin, not a blanket default.

If you're building against SubToAPI's wrapper, the same parameter works identically — see /docs/messages for the full request schema, since the output length controls pass through unchanged from the underlying model.

Use stop_sequences for structured output

When you need Claude to stop at a specific marker — end of a JSON object, end of a list, before it starts adding commentary — stop_sequences is more reliable than hoping the model self-regulates:

{
  "model": "claude-sonnet-4-5",
  "max_tokens": 500,
  "stop_sequences": ["</answer>"],
  "messages": [
    {"role": "user", "content": "Answer the question and wrap your final answer in <answer></answer> tags. Question: ..."}
  ]
}

This is especially useful when you want Claude to generate a fixed number of items and nothing else — ask it to end each list with a sentinel string, then stop on that string. It prevents the common failure mode where Claude adds a closing paragraph ("I hope this helps!") after the structured content you actually wanted.

Prompt for length explicitly and specifically

Vague instructions like "be concise" produce inconsistent results. Specific instructions work much better:

Numeric constraints anchor the model far better than adjectives like "brief" or "short," which Claude interprets inconsistently depending on context length and task complexity. If you need hard enforcement rather than best-effort compliance, pair the instruction with a matching max_tokens value so truncation — if it happens — occurs at a reasonable boundary rather than mid-word.

Detect truncation instead of assuming completion

Every response includes a stop_reason field. Check it on every call that matters:

const response = await fetch("https://api.subtoapi.app/v1/messages", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
    "content-type": "application/json"
  },
  body: JSON.stringify({
    model: "claude-sonnet-4-5",
    max_tokens: 300,
    messages: [{ role: "user", content: "Explain database indexing." }]
  })
});

const data = await response.json();
if (data.stop_reason === "max_tokens") {
  console.warn("Response was truncated — consider raising max_tokens or shortening the prompt's expected output.");
}

Silently shipping truncated responses to users is a bad experience — a cut-off sentence looks broken. Handling it programmatically (retry with higher max_tokens, or re-prompt for a shorter answer) is far better than ignoring the field.

Handle length control in streaming responses

If you're streaming output for a better perceived latency, you still need to watch for truncation — it just arrives as part of the final message_delta event rather than the full response object. Buffer the stream, but check the terminal event's stop_reason the same way you would for a non-streaming call. SubToAPI's streaming endpoint mirrors Anthropic's event format exactly, so existing stream-handling code carries over; see /docs/streaming for event details.

Combine techniques for predictable output

In production, the most reliable setups combine all three levers:

This layered approach avoids both failure modes — unpredictable long responses and jarring mid-sentence cutoffs — without needing to guess at a single magic number.

If you're running Claude behind an API layer for your team, getting these parameters right once in a shared request template (rather than re-deriving them per integration) saves a lot of debugging later. SubToAPI exposes the same max_tokens and stop_sequences parameters directly, so existing prompt templates built around Anthropic's API work without modification — check /docs/quickstart or start a trial from /signup to test it against your own prompts.

questions

Does increasing max_tokens make Claude write longer responses? No. max_tokens only sets the ceiling — it doesn't make Claude write more. If the model would naturally stop at 80 tokens, raising the limit to 1000 has no effect on actual length. Length is primarily controlled by prompt instructions.

Why does Claude ignore my "keep it short" instruction? Vague adjectives are interpreted inconsistently. Use concrete constraints instead — word counts, sentence counts, or bullet point limits — and pair them with an appropriately low max_tokens as a backstop.

How do I know if a response was cut off? Check the stop_reason field in the API response. A value of "max_tokens" means the output was truncated; "end_turn" means Claude finished naturally. Always check this before displaying or storing output in production.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →