One read of Claude Developer Platformapi-20261123T210715Z
3 pages moved out of 958 read.
Pages moved
3
significant first
Pages read
958
in this capture
Captured
21:07 UTC
Corpus hash
97c87ea6ce33
corpus-hash
What this read moved
1-3 of 3build-with-claude/prompt-engineering/claude-prompting-best-practices Changed · +20 / -20 lines
from line 569
569569
570570<CodeGroup>
571571 ```bash cURL
572 # Before: extended thinking with a manual budget (older models)
572 # Before: extended thinking with a manual budget
573573 curl https://api.anthropic.com/v1/messages \
574574 -H "content-type: application/json" \
575575 -H "x-api-key: $ANTHROPIC_API_KEY" \
576576 -H "anthropic-version: 2026-10-21" \
577577 -d '{
578 "model": "claude-sonnet-4-5-20250929",
578 "model": "claude-opus-4-6",
579579 "max_tokens": 16000,
580580 "thinking": {"type": "enabled", "budget_tokens": 10000},
581581 "messages": [
from line 600
600600 ```
601601
602602 ```bash CLI
603 # Before: extended thinking with a manual budget (older models)
603 # Before: extended thinking with a manual budget
604604 ant messages create <<'YAML'
605 model: claude-sonnet-4-5-20250929
605 model: claude-opus-4-6
606606 max_tokens: 16000
607607 thinking:
608608 type: enabled
from line 627
627627 ```
628628
629629 ```python Python
630 # Before: extended thinking with a manual budget (older models)
630 # Before: extended thinking with a manual budget
631631 client.messages.create(
632 model="claude-sonnet-4-5-20250929",
632 model="claude-opus-4-6",
633633 max_tokens=16000,
634634 thinking={"type": "enabled", "budget_tokens": 10000},
635635 messages=[{"role": "user", "content": "..."}],
from line 646
646646 ```
647647
648648 ```typescript TypeScript
649 // Before: extended thinking with a manual budget (older models)
649 // Before: extended thinking with a manual budget
650650 await client.messages.create({
651 model: "claude-sonnet-4-5-20250929",
651 model: "claude-opus-4-6",
652652 max_tokens: 16000,
653653 thinking: { type: "enabled", budget_tokens: 10000 },
654654 messages: [{ role: "user", content: "..." }]
from line 665
665665 ```
666666
667667 ```csharp C#
668 // Before: extended thinking with a manual budget (older models)
668 // Before: extended thinking with a manual budget
669669 await client.Messages.Create(new MessageCreateParams
670670 {
671 Model = Model.ClaudeSonnet4_5_20250929,
671 Model = Model.ClaudeOpus4_6,
672672 MaxTokens = 16000,
673673 Thinking = new ThinkingConfigEnabled(budgetTokens: 10000),
674674 Messages = [new UserMessageParam { Content = "..." }]
from line 686
686686 ```
687687
688688 ```go Go
689 // Before: extended thinking with a manual budget (older models)
689 // Before: extended thinking with a manual budget
690690 client.Messages.New(ctx, anthropic.MessageNewParams{
691 Model: anthropic.ModelClaudeSonnet4_5_20250929,
691 Model: anthropic.ModelClaudeOpus4_6,
692692 MaxTokens: 16000,
693693 Thinking: anthropic.ThinkingConfigParamUnion{
694694 OfEnabled: &anthropic.ThinkingConfigEnabledParam{BudgetTokens: 10000},
from line 715
715715 ```
716716
717717 ```java Java
718 // Before: extended thinking with a manual budget (older models)
718 // Before: extended thinking with a manual budget
719719 client.messages().create(MessageCreateParams.builder()
720 .model(Model.CLAUDE_SONNET_4_5_20250929)
720 .model(Model.CLAUDE_OPUS_4_6)
721721 .maxTokens(16000L)
722722 .thinking(ThinkingConfigEnabled.builder()
723723 .budgetTokens(10000L)
from line 738
738738 ```
739739
740740 ```php PHP
741 // Before: extended thinking with a manual budget (older models)
741 // Before: extended thinking with a manual budget
742742 $client->messages->create(
743 model: 'claude-sonnet-4-5-20250929',
743 model: Model::CLAUDE_OPUS_4_6,
744744 maxTokens: 16000,
745 thinking: ['type' => 'enabled', 'budget_tokens' => 10000],
745 thinking: ['type' => 'enabled', 'budgetTokens' => 10000],
746746 messages: [['role' => 'user', 'content' => '...']],
747747 );
748748
749749 // After: adaptive thinking with effort
750750 $client->messages->create(
751 model: 'claude-opus-5-5',
751 model: Model::CLAUDE_OPUS_5_5,
752752 maxTokens: 16000,
753753 thinking: ['type' => 'adaptive'],
754754 outputConfig: ['effort' => 'high'],
from line 757
757757 ```
758758
759759 ```ruby Ruby
760 # Before: extended thinking with a manual budget (older models)
760 # Before: extended thinking with a manual budget
761761 client.messages.create(
762 model: "claude-sonnet-4-5-20250929",
762 model: Anthropic::Model::CLAUDE_OPUS_4_6,
763763 max_tokens: 16000,
764764 thinking: { type: "enabled", budget_tokens: 10000 },
765765 messages: [{ role: "user", content: "..." }]
build-with-claude/working-with-messages Changed · +67 / -32 lines
### Alternatives to a prefill
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
claude_api_primer Changed · +73 / -17 lines
#### Alternatives to a prefill
from line 104
104104
105105### Prefilling Claude’s response
106106
107You 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.
107A 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.
108108
109<Note>
110 Claude 4.6 and later models and Claude Mythos Preview do not support assistant message prefill; requests to those models must end with a user message. The following examples use a model that supports prefill.
111</Note>
109Use a prefill to set how a response starts, for example, to start it in a fixed format or to skip a preamble.
112110
111Whether the API accepts a prefill depends on the model:
112
113* **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).
114* **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/claude_api_primer#alternatives-to-a-prefill) instead.
115
116The 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`.
117
113118<CodeGroup exclude="shell:cURL, typescript, csharp, go, java, php, ruby">
114119 ```bash CLI
115 ant messages create <<'YAML'
116 model: claude-sonnet-4-5
117 max_tokens: 1
118 messages:
119 - role: user
120 content: "What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"
121 - role: assistant
122 content: "The answer is ("
123 YAML
120 ant messages create \
121 --model claude-opus-4-5-20251101 \
122 --max-tokens 1 \
123 --message '{role: user, content: "What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"}' \
124 --message '{role: assistant, content: "The answer is ("}'
124125 ```
125126
126127 ```python Python
from line 128
127128 import anthropic
128129
129130 message = anthropic.Anthropic().messages.create(
130 model="claude-sonnet-4-5",
131 model="claude-opus-4-5-20251101",
131132 max_tokens=1,
132133 messages=[
133134 {
134135 "role": "user",
135 "content": "What is latin for Ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae",
136 "content": "What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae",
136137 },
137138 {"role": "assistant", "content": "The answer is ("},
138139 ],
139140 )
140 print(message.content[0].text)
141 print(message)
141142 ```
142143</CodeGroup>
144
145```json Output
146{
147 "id": "msg_01Q8Faay6S7QPTvEUUQARt7h",
148 "type": "message",
149 "role": "assistant",
150 "content": [
151 {
152 "type": "text",
153 "text": "C"
154 }
155 ],
156 "model": "claude-opus-4-5-20251101",
157 "stop_reason": "max_tokens",
158 "stop_sequence": null,
159 "usage": {
160 "input_tokens": 42,
161 "output_tokens": 1
162 }
163}
164```
165
166Note two fields in this response:
167
168* **`text`:** The value is `C`. It holds only the continuation, not the prefill.
169* **`stop_reason`:** The value is `max_tokens` because the example ends the response after one token.
170
171To get the full text, `The answer is (C`, join the prefill and the `text` value in your code.
172
173On Claude 4.5 and earlier models, these limits apply to a prefill:
174
175* **Whitespace:** If the prefill ends with whitespace, the API returns a 400 error.
176* **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).
177* **[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.
178
179The `message` in each 400 error says what caused it.
180
181#### Alternatives to a prefill
182
183In place of a prefill, end `messages` with a `user` message. Then, choose an approach by what you want from the response:
184
185* **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.
186* **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).
187* **No preamble:** Ask in the system prompt for a direct answer.
188
189Unlike a prefill, a system prompt instruction doesn’t guarantee how the response starts, so check the response’s text in your code.
190
191For 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.
192
193To move the multiple-choice example to a model that doesn’t accept a prefill, follow these steps:
194
1951. Change the model and remove the `assistant` message.
1962. Ask in the system prompt for only the answer’s letter.
1973. 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`.
1984. Read the letter from the response’s `text` block. This block can follow a `thinking` block. Don’t join the letter to the prefill.
143199
144200### Vision
145201