Follow Discord
Sweep 24 Nov 2026 · 22:58Z Build v2.1.332 549 read Stable v2.1.326 Latest v2.1.332 Next v2.1.332 Feeds RSS JSON llms.txt llms-full.txt Unofficial
One change · api

API usage primer for Claude changedclaude_api_primer

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+73added
Lines−17removed
From line 104 where the diff opens
First seen 14 Aug 2026 this site's first read of the page
Recorded edits28to this page, all time

#### Alternatives to a prefill

The whole hunk

from line 104, old and new numbered
/
lines
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 
Feedback