claude_api_primer changedclaude_api_primer
Nearest release: v2.1.333, published 4 hours after 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+90added
Lines−38removed
From line
1
where the diff opens
First seen
14 Aug 2026
this site's first read of the page
Recorded edits32to this page, all time
### Invalid tool call ### Example event stream # API usage primer for Claude ### Invalid tool name ### Basic streaming request example
The whole hunk
from line 1, old and new numbered
/
from line 1
11---
22title: API usage primer for Claude
33url: https://platform.claude.com/docs/en/claude_api_primer
4description: This guide is designed to give Claude the basics of using the Claude API. It gives explanation and examples of model IDs/the basic messages API, tool use, streaming, thinking, and nothing else.
4description: The basics of the Claude API on one page, written for Claude and other agents.
55---
66
7# API usage primer for Claude
7This page gives Claude, or any other agent that writes code against the Claude API, the basics in one place. It covers model IDs, Messages API requests with text and images, thinking, tool use and its errors, JSON output, and streaming. The instructions on this page are for whoever sends the request, and “Claude” is the model that answers it.
88
9> This guide is designed to give Claude the basics of using the Claude API. It gives explanation and examples of model IDs/the basic messages API, tool use, streaming, thinking, and nothing else.
9For the rest, see the [Messages API reference](https://platform.claude.com/docs/en/api/messages/create) and the guides that each section links to.
1010
1111## Models
1212
from line 67
6767
6868### Multiple conversational turns
6969
70The Messages API is stateless, which means that you always send the full conversational history to the API. You can use this pattern to build up a conversation over time. Earlier conversational turns don’t necessarily need to actually originate from Claude. You can use synthetic `assistant` messages.
70By default, the Messages API is stateless, so you send the full conversation history with each request. To continue a conversation, add the response’s `content` to `messages` as an `assistant` message. Keep it unchanged. It can hold `thinking` and `tool_use` blocks, and during tool use, those blocks must go back as they came. Then, add the next `user` message.
7171
72With [message threads](https://platform.claude.com/docs/en/build-with-claude/message-threads), in beta, the API can keep the conversation, so that each later request sends only the new messages. A thread expires 5 minutes after its last request and is deleted no more than 1 hour after its last use.
72Earlier `assistant` messages don’t have to come from Claude. You can write them yourself, as the following example does. In a request, `content` can be a string or an array of blocks. A response has an array of blocks. The last message is a special case. See [Prefilling Claude’s response](https://platform.claude.com/docs/en/claude_api_primer#putting-words-in-claudes-mouth).
7373
7474<CodeGroup exclude="shell:cURL, typescript, csharp, go, java, php, ruby">
7575 ```bash CLI
from line 102
102102 ```
103103</CodeGroup>
104104
105With [message threads](https://platform.claude.com/docs/en/build-with-claude/message-threads), in beta, the API can keep the conversation, so that each later request sends only the new messages. A thread expires 5 minutes after its last request and is deleted no more than 1 hour after its last use.
106
105107### Prefilling Claude’s response
106108
107109A 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.
from line 188
186188
187189In place of a prefill, end `messages` with a `user` message. Then, choose an approach by what you want from the response:
188190
189* **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.
191* **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. With an `enum` in the schema, they also constrain a value to fixed options, apart from its capitalization (see the [Invalid outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs#invalid-outputs) section of that page).
190192* **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).
191193* **No preamble:** Ask in the system prompt for a direct answer.
194* **A continuation, fewer refusals, or a consistent role:** 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.
192195
193196Unlike a prefill, a system prompt instruction doesn’t guarantee how the response starts, so check the response’s text in your code.
194197
195For 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.
196
197198To move the multiple-choice example to a model that doesn’t accept a prefill, follow these steps:
198199
1992001. Change the model and remove the `assistant` message.
2002012. Ask in the system prompt for only the answer’s letter.
2013. 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`.
2023. Set `max_tokens` high enough for the whole response. On [models where thinking is always on or on by default](https://platform.claude.com/docs/en/claude_api_primer#thinking), thinking tokens also count toward `max_tokens`.
2022034. Read the letter from the response’s `text` block. This block can follow a `thinking` block. Don’t join the letter to the prefill.
203204
205For these steps as a request in code, see [Using the Messages API](https://platform.claude.com/docs/en/build-with-claude/working-with-messages#alternatives-to-a-prefill).
206
204207### Vision
205208
206209Claude can read both text and images in requests. Both `base64` and `url` source types are supported for images, along with the `image/jpeg`, `image/png`, `image/gif`, and `image/webp` media types.
from line 303
300303
301304## Thinking
302305
303Thinking can sometimes help Claude with very hard tasks. The current mechanism is [adaptive thinking](https://platform.claude.com/docs/en/build-with-claude/thinking) (`thinking: {"type": "adaptive"}`): Claude determines when and how much to think, and you steer thinking depth with the [`effort`](https://platform.claude.com/docs/en/build-with-claude/effort) parameter rather than a token budget. Adaptive thinking is supported on Claude 4.6 and later models and Claude Mythos Preview. On Claude 5 and later models and Claude Mythos Preview, thinking is on by default when the `thinking` parameter is omitted.
306Thinking can help Claude with very hard tasks. With [adaptive thinking](https://platform.claude.com/docs/en/build-with-claude/thinking) (`thinking: {"type": "adaptive"}`), Claude determines when and how much to think.
304307
305Thinking is supported in the following models:
308With adaptive thinking, you steer thinking depth with the [`effort` parameter](https://platform.claude.com/docs/en/build-with-claude/effort), not a token budget. Set it in `output_config`, for example, `output_config: {"effort": "medium"}`. From least to most thinking, the levels are `low`, `medium`, `high`, `xhigh`, and `max`. The [Effort levels](https://platform.claude.com/docs/en/build-with-claude/effort#effort-levels) table says which models support `xhigh` and `max`.
306309
310The following models support thinking. In the list, “always on” means that you can’t turn thinking off. “On by default” means that thinking runs when you omit the `thinking` parameter. To turn it off, send `thinking: {"type": "disabled"}`. On Claude Haiku 5.5 and Claude Opus 5, this works only at `high` effort or below. Claude Sonnet 5.5 differs, as the text after the list explains. With neither label, thinking is off until you set `thinking`. “Legacy manual thinking” is the manual extended thinking that the [How thinking works](https://platform.claude.com/docs/en/claude_api_primer#how-thinking-works) section describes. For what each model does with each `thinking` value, see the [Configuring thinking](https://platform.claude.com/docs/en/build-with-claude/thinking#configuring-thinking) section of the Thinking page.
311
307312* Claude Fable 5.2 (`claude-fable-5-2`, adaptive thinking only, always on)
308313* Claude Mythos 5.2 (`claude-mythos-5-2`, adaptive thinking only, always on)
309314* Claude Opus 5.5 (`claude-opus-5-5`, adaptive thinking only, always on)
310* Claude Sonnet 5.5 (`claude-sonnet-5-5`, adaptive thinking only, on by default; `thinking: {"type": "disabled"}` returns a 400 error, so send `thinking: {"type": "between_tools"}` to turn off up-front thinking)
315* Claude Sonnet 5.5 (`claude-sonnet-5-5`, adaptive thinking or `between_tools`, on by default)
311316* Claude Haiku 5.5 (`claude-haiku-5-5`, adaptive thinking only, on by default)
312317* Claude Fable 5.1 (`claude-fable-5-1`, adaptive thinking only, always on)
313318* Claude Mythos 5.1 (`claude-mythos-5-1`, adaptive thinking only, always on)
from line 328
323328* Claude Sonnet 4.5 (`claude-sonnet-4-5-20250929`, [deprecated on Anthropic-operated platforms](https://platform.claude.com/docs/en/about-claude/model-deprecations#overview), legacy manual thinking only)
324329* Claude Haiku 4.5 (`claude-haiku-4-5-20251001`, legacy manual thinking only)
325330
331Claude Mythos Preview also supports thinking. See the [Configuring thinking](https://platform.claude.com/docs/en/build-with-claude/thinking#configuring-thinking) section of the Thinking page.
332
333Claude Sonnet 5.5 returns a 400 error for `thinking: {"type": "disabled"}`. Its lowest setting is `thinking: {"type": "between_tools"}`, which turns off up-front thinking: the `thinking` blocks that come ahead of the response. That setting takes no other field, and at `xhigh` or `max` effort, it returns a 400 error.
334
326335<Note>
327336 On Claude 4.7 and later models, manual extended thinking (`type: enabled` with a `budget_tokens` value) is not supported and returns a 400 error. Use [adaptive thinking](https://platform.claude.com/docs/en/build-with-claude/thinking) (`type: adaptive`) instead.
328337</Note>
from line 338
329338
330339### How thinking works
331340
332When thinking is on, Claude creates `thinking` content blocks where it outputs its internal reasoning. The API response includes `thinking` content blocks, followed by `text` content blocks.
341When thinking is on, the API response can include `thinking` content blocks before its `text` content blocks. With adaptive thinking, Claude can skip thinking for a request that doesn’t need it, and the response then has no `thinking` block. So select blocks by `type`, not by position.
333342
343The `display` field of the `thinking` parameter sets what each `thinking` block returns. You can set it with `type: "adaptive"` or `type: "enabled"`:
344
345* `"summarized"`: The block’s `thinking` field holds a summary of the thinking. This is the default on Claude 4.6 and earlier models.
346* `"omitted"`: The block’s `thinking` field is empty. This is the default on Claude 4.7 and later models and Claude Mythos Preview. Even when the field is empty, pass the block back.
347
348The following example sets `display: "summarized"`.
349
334350<CodeGroup exclude="shell:cURL, typescript, csharp, go, java, php, ruby">
335351 ```bash CLI
336352 ant messages create <<'YAML'
from line 388
372388 ```
373389</CodeGroup>
374390
375Manual extended thinking (`thinking: {"type": "enabled", "budget_tokens": N}`) is the legacy mechanism. It works only on Claude 4 through 4.6 models that support thinking; Claude 4.7 and later models reject `type: enabled` with a 400 error and use [adaptive thinking](https://platform.claude.com/docs/en/build-with-claude/thinking) instead. With manual extended thinking, `budget_tokens` sets the maximum number of tokens Claude is allowed to use for its internal reasoning process; the limit applies to full thinking tokens, not to the summarized output. Unless you are using [interleaved thinking](https://platform.claude.com/docs/en/claude_api_primer#interleaved-thinking), `budget_tokens` must be less than `max_tokens` so that Claude has space to write its response after thinking is complete.
391Manual extended thinking (`thinking: {"type": "enabled", "budget_tokens": N}`) is the legacy mechanism. `budget_tokens` sets a target for how many tokens Claude uses for thinking, not a strict cap:
376392
393* The budget applies to the full thinking, not to the summary that the response returns.
394* `budget_tokens` must be less than `max_tokens`, which leaves room for the response, unless you use [interleaved thinking](https://platform.claude.com/docs/en/claude_api_primer#interleaved-thinking).
395
377396## Thinking with tool use
378397
379Thinking can be used alongside tool use, allowing Claude to reason through tool selection and results processing.
398You can use thinking alongside tool use, which lets Claude reason through tool selection and results processing.
380399
381400Important limitations:
382401
from line 470
451470
452471### Interleaved thinking
453472
454Interleaved thinking allows Claude to think between tool calls, reasoning about tool results before determining the next step.
473Interleaved thinking lets Claude think between tool calls and reason about each tool result before the next step.
455474
456<Info>
457 On models with [adaptive thinking](https://platform.claude.com/docs/en/build-with-claude/thinking) (`thinking: {type: "adaptive"}`), interleaved thinking is automatically enabled. No beta header is needed. Sonnet 4.6 supports both the `interleaved-thinking-2025-05-14` beta header with manual extended thinking and adaptive thinking.
458</Info>
475With [adaptive thinking](https://platform.claude.com/docs/en/build-with-claude/thinking), interleaved thinking is automatic. On Claude Opus 4.6, only adaptive thinking interleaves.
459476
460On older models that use manual extended thinking (Claude 4, 4.5, and Sonnet 4.6 models), enable interleaved thinking by adding the beta header `interleaved-thinking-2025-05-14` to your API request:
477With manual extended thinking, add the `interleaved-thinking-2025-05-14` [beta header](https://platform.claude.com/docs/en/api/beta-headers) to your request. This works on Claude Sonnet 4.6, Claude Opus 4.5, Claude Sonnet 4.5, and Claude Haiku 4.5. On Claude Sonnet 4.6, the header is deprecated. The following example sends the header:
461478
462479<CodeGroup exclude="shell:cURL, typescript, csharp, go, java, php, ruby">
463480 ```bash CLI
from line 568
551568 ```
552569</CodeGroup>
553570
554With interleaved thinking and ONLY with interleaved thinking (not regular manual extended thinking), the `budget_tokens` can exceed the `max_tokens` parameter, as `budget_tokens` in this case represents the total budget across all thinking blocks within one assistant turn.
571With interleaved thinking, `budget_tokens` can exceed `max_tokens`, because the budget covers all thinking blocks within one assistant turn (a whole tool-use loop).
555572
556573## Tool use
557574
from line 606
589606
590607### Best practices for tool definitions
591608
592**Provide extremely detailed descriptions.** This is by far the most important factor in tool performance. Your descriptions should explain every detail about the tool, including:
609**Provide extremely detailed descriptions.** This is the most important factor in tool performance. Your descriptions should explain every detail about the tool, including:
593610
594611* What the tool does
595612* When it should be used (and when it shouldn’t)
from line 613
596613* What each parameter means and how it affects the tool’s behavior
597614* Any important caveats or limitations
598615
599**Consider using `input_examples` for complex tools.** For tools with nested objects, optional parameters, or format-sensitive inputs, you can provide concrete examples using the `input_examples` field (beta). This shows Claude the expected input patterns. See [Providing tool use examples](https://platform.claude.com/docs/en/agents-and-tools/tool-use/define-tools#providing-tool-use-examples) for details.
600
601616Example of a good tool description:
602617
603618```json
from line 632
617632}
618633```
619634
635**Consider using `input_examples` for complex tools.** For tools with nested objects, optional parameters, or format-sensitive inputs, add an `input_examples` field to the tool definition. It takes an array of example input objects, each valid against the tool’s `input_schema`. See the [Providing tool use examples](https://platform.claude.com/docs/en/agents-and-tools/tool-use/define-tools#providing-tool-use-examples) section of the Define tools page.
636
620637## Controlling Claude’s output
621638
622639### Forcing tool use
from line 655
638655
639656### JSON output
640657
641Tools do not necessarily need to be client functions. You can use tools anytime you want the model to return JSON output that follows a provided schema.
658To get a response that follows a JSON schema, use [structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs). Send the schema in `output_config.format`:
642659
660```json
661{
662 "output_config": {
663 "format": {
664 "type": "json_schema",
665 "schema": {
666 "type": "object",
667 "properties": { "city": { "type": "string" } },
668 "required": ["city"],
669 "additionalProperties": false
670 }
671 }
672 }
673}
674```
675
676The JSON comes back in the response’s `text` block.
677
643678### Parallel tool use
644679
645By default, Claude might use multiple tools to answer a user query. You can disable this behavior by setting `disable_parallel_tool_use=true`.
680By default, Claude might call several tools in one response, which then has several `tool_use` blocks. To turn off parallel tool use, set `disable_parallel_tool_use: true` inside `tool_choice`. With the `auto` type, Claude then calls at most one tool in each response:
646681
682```json
683{
684 "tool_choice": { "type": "auto", "disable_parallel_tool_use": true }
685}
686```
687
647688## Handling tool use and tool result content blocks
648689
649690### Handling results from client tools
from line 712
671712When you receive a tool use response, you should:
672713
6737141. Extract the `name`, `id`, and `input` from the `tool_use` block.
715
6747162. Run the actual tool in your code base corresponding to that tool name.
6753. Continue the conversation by sending a new message with a `tool_result`:
676717
7183. Continue the conversation in a new request. Add two messages to `messages`:
719
720 1. An `assistant` message with the response’s `content`, unchanged.
721 2. A `user` message with a `tool_result` for each `tool_use` block.
722
723The `user` message looks like this:
724
677725```json
678726{
679727 "role": "user",
from line 763
715763}
716764```
717765
718### Invalid tool name
766### Invalid tool call
719767
720If Claude’s attempted use of a tool is invalid (for example, missing required parameters), try the request again with more-detailed `description` values in your tool definitions.
768If your code finds an invalid tool call (for example, one missing a required parameter), return a `tool_result` that states the error, with `"is_error": true`. Claude can then try the call again with corrections.
721769
770An invalid call usually means that Claude didn’t have enough information to use the tool. During development, give your tool definitions more detailed `description` values. To make every call match the tool’s `input_schema`, set `"strict": true` on the tool definition. See [Strict tool use](https://platform.claude.com/docs/en/agents-and-tools/tool-use/strict-tool-use).
771
722772## Streaming messages
723773
724774When creating a Message, you can set `"stream": true` to incrementally stream the response using server-sent events (SSE).
from line 826
776826For `tool_use` content blocks, deltas are *partial JSON strings*:
777827
778828```json
779{"type": "content_block_delta","index": 1,"delta": {"type": "input_json_delta","partial_json": "{\"location\": \"San Fra"}}}
829{"type": "content_block_delta","index": 1,"delta": {"type": "input_json_delta","partial_json": "{\"location\": \"San Fra"}}
780830```
781831
782832#### Thinking delta
783833
784When using thinking with streaming:
834With `display: "summarized"`, thinking text streams in `thinking_delta` events, as in the following example. With `display: "omitted"`, each `thinking` block streams one `thinking_delta` whose `thinking` string is empty.
785835
786836```json
787837{
from line 844
794844}
795845```
796846
797### Basic streaming request example
847### Example event stream
848
849The following events stream a short text response:
798850
799851```sse
800852event: message_start
No line in this hunk matches that.