working-with-messages changedbuild-with-claude/working-with-messages
Nearest release: v2.1.331, published 2 hours before this site recorded the change. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
Recorded here
Lines+67added
Lines−32removed
From line
362
where the diff opens
First seen
14 Aug 2026
this site's first read of the page
Recorded edits32to this page, all time
### Alternatives to a prefill
The whole hunk
from line 362, old and new numbered
/
from line 362
362362
363363## Prefilling Claude’s response
364364
365You can prefill part of Claude’s response in the last position of the input messages list. Use this technique to shape Claude’s response. The following example uses `"max_tokens": 1` to get a single multiple-choice answer from Claude.
365A prefill is text that you write in an `assistant` message and put last in `messages`. An `assistant` message anywhere else in `messages` isn’t a prefill. Claude continues the prefilled text, and the response holds only the continuation.
366366
367<Warning>
368 Prefilling is not supported on Claude 4.6 and later models and [Claude Mythos Preview](https://anthropic.com/glasswing). Requests using prefill with these models return a 400 error. Use [structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs) on models that support it, or system prompt instructions, instead. See [Migration guides](https://platform.claude.com/docs/en/about-claude/models/migration-guide) for migration patterns.
369</Warning>
367Use a prefill to set how a response starts, for example, to start it in a fixed format or to skip a preamble.
370368
369Whether the API accepts a prefill depends on the model, not on the API version:
370
371* **Claude 4.5 and earlier models:** The API accepts a prefill unless the model is retired on the platform that you use. All requests to a retired model fail. To check a model’s status, see [Model deprecations](https://platform.claude.com/docs/en/about-claude/model-deprecations).
372* **All other models:** The API returns a 400 error (see the [Prefill not supported](https://platform.claude.com/docs/en/api/errors#prefill-not-supported) section of the Claude API errors page). Use one of the [alternatives to a prefill](https://platform.claude.com/docs/en/build-with-claude/working-with-messages#alternatives-to-a-prefill) instead.
373
374The following example runs on Claude Opus 4.5 and sets the format of a multiple-choice answer. The prefill, `The answer is (`, ends right before the answer’s letter so that Claude writes the letter first. Because the answer is one letter, the example also sets `max_tokens` to `1`. For a longer answer, set a higher `max_tokens`.
375
371376<CodeGroup>
372377 ```bash cURL
373378 #!/bin/sh
from line 381
376381 -H "anthropic-version: 2026-10-21" \
377382 -H "content-type: application/json" \
378383 -d '{
379 "model": "claude-sonnet-4-5",
384 "model": "claude-opus-4-5-20251101",
380385 "max_tokens": 1,
381386 "messages": [
382 {"role": "user", "content": "What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"},
387 {"role": "user", "content": "What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"},
383388 {"role": "assistant", "content": "The answer is ("}
384389 ]
385390 }'
from line 391
386391 ```
387392
388393 ```bash CLI
389 ant messages create <<'YAML'
390 model: claude-sonnet-4-5
391 max_tokens: 1
392 messages:
393 - role: user
394 content: "What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"
395 - role: assistant
396 content: "The answer is ("
397 YAML
394 ant messages create \
395 --model claude-opus-4-5-20251101 \
396 --max-tokens 1 \
397 --message '{role: user, content: "What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"}' \
398 --message '{role: assistant, content: "The answer is ("}'
398399 ```
399400
400401 ```python Python
401402 message = anthropic.Anthropic().messages.create(
402 model="claude-sonnet-4-5",
403 model="claude-opus-4-5-20251101",
403404 max_tokens=1,
404405 messages=[
405406 {
406407 "role": "user",
407 "content": "What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae",
408 "content": "What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae",
408409 },
409410 {"role": "assistant", "content": "The answer is ("},
410411 ],
from line 417
416417 const anthropic = new Anthropic();
417418
418419 const message = await anthropic.messages.create({
419 model: "claude-sonnet-4-5",
420 model: "claude-opus-4-5-20251101",
420421 max_tokens: 1,
421422 messages: [
422423 {
423424 role: "user",
424 content: "What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"
425 content: "What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"
425426 },
426427 { role: "assistant", content: "The answer is (" }
427428 ]
from line 435
434435
435436 var parameters = new MessageCreateParams
436437 {
437 Model = Model.ClaudeSonnet4_5,
438 Model = Model.ClaudeOpus4_5_20251101,
438439 MaxTokens = 1,
439440 Messages = [
440 new UserMessageParam { Content = "What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae" },
441 new UserMessageParam { Content = "What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae" },
441442 new AssistantMessageParam { Content = "The answer is (" }
442443 ]
443444 };
from line 451
450451 client := anthropic.NewClient()
451452
452453 response, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
453 Model: anthropic.ModelClaudeSonnet4_5,
454 Model: anthropic.ModelClaudeOpus4_5_20251101,
454455 MaxTokens: 1,
455456 Messages: []anthropic.MessageParam{
456 anthropic.NewUserMessage(anthropic.NewTextBlock("What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae")),
457 anthropic.NewUserMessage(anthropic.NewTextBlock("What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae")),
457458 anthropic.NewAssistantMessage(anthropic.NewTextBlock("The answer is (")),
458459 },
459460 })
from line 468
467468 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
468469
469470 MessageCreateParams params = MessageCreateParams.builder()
470 .model(Model.CLAUDE_SONNET_4_5)
471 .model(Model.CLAUDE_OPUS_4_5_20251101)
471472 .maxTokens(1L)
472 .addUserMessage("What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae")
473 .addUserMessage("What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae")
473474 .addAssistantMessage("The answer is (")
474475 .build();
475476
from line 484
483484 $message = $client->messages->create(
484485 maxTokens: 1,
485486 messages: [
486 ['role' => 'user', 'content' => 'What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae'],
487 ['role' => 'user', 'content' => 'What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae'],
487488 ['role' => 'assistant', 'content' => 'The answer is ('],
488489 ],
489 model: 'claude-sonnet-4-5',
490 model: Model::CLAUDE_OPUS_4_5_20251101,
490491 );
491 echo $message->content[0]->text;
492 echo json_encode($message, JSON_PRETTY_PRINT), PHP_EOL;
492493 ```
493494
494495 ```ruby Ruby
from line 496
495496 client = Anthropic::Client.new
496497
497498 message = client.messages.create(
498 model: Anthropic::Model::CLAUDE_SONNET_4_5,
499 model: Anthropic::Model::CLAUDE_OPUS_4_5_20251101,
499500 max_tokens: 1,
500501 messages: [
501502 {
502503 role: "user",
503 content: "What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"
504 content: "What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"
504505 },
505506 { role: "assistant", content: "The answer is (" }
506507 ]
from line 521
520521 "text": "C"
521522 }
522523 ],
523 "model": "claude-sonnet-4-5",
524 "model": "claude-opus-4-5-20251101",
524525 "stop_reason": "max_tokens",
525526 "stop_sequence": null,
526527 "usage": {
from line 530
529530 }
530531}
531532```
533
534Note two fields in this response:
535
536* **`text`:** The value is `C`. It holds only the continuation, not the prefill.
537* **`stop_reason`:** The value is `max_tokens` because the example ends the response after one token.
538
539To get the full text, `The answer is (C`, join the prefill and the `text` value in your code.
540
541On Claude 4.5 and earlier models, these limits apply to a prefill:
542
543* **Whitespace:** If the prefill ends with whitespace, the API returns a 400 error.
544* **Structured outputs:** If the request also sets `output_config.format`, the API returns a 400 error (see the [Feature compatibility](https://platform.claude.com/docs/en/build-with-claude/structured-outputs#feature-compatibility) section of the Structured outputs page).
545* **[Extended thinking](https://platform.claude.com/docs/en/build-with-claude/extended-thinking):** If the request also sets `thinking.type` to `enabled`, the prefill causes no error. The API treats thinking as off, so the response has no `thinking` block.
546
547The `message` in each 400 error says what caused it.
548
549### Alternatives to a prefill
550
551In place of a prefill, end `messages` with a `user` message. Then, choose an approach by what you want from the response:
552
553* **JSON that follows a schema:** Use [structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs), which constrain the response to follow the schema.
554* **Another output format:** Describe the format in the system prompt, which is the request’s [`system` field](https://platform.claude.com/docs/en/api/messages/create#create.system).
555* **No preamble:** Ask in the system prompt for a direct answer.
556
557Unlike a prefill, a system prompt instruction doesn’t guarantee how the response starts, so check the response’s text in your code.
558
559For what to use in place of other common prefills, see the [Migrating away from prefilled responses](https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices#migrating-away-from-prefilled-responses) section of the Prompting best practices page.
560
561To move the multiple-choice example to a model that doesn’t accept a prefill, follow these steps:
562
5631. Change the model and remove the `assistant` message.
5642. Ask in the system prompt for only the answer’s letter.
5653. Set `max_tokens` high enough for the whole response. On [models with thinking on by default](https://platform.claude.com/docs/en/build-with-claude/thinking#configuring-thinking), thinking tokens also count toward `max_tokens`.
5664. Read the letter from the response’s `text` block. This block can follow a `thinking` block. Don’t join the letter to the prefill.
532567
533568## Vision
534569
No line in this hunk matches that.