|
| 1 | +# Claude API Reference |
| 2 | + |
| 3 | +This document provides a condensed overview of the Anthropic Claude API, covering messages, token counting, and model management. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Messages API |
| 8 | + |
| 9 | +The Messages API is the primary way to interact with Claude for multi-turn conversations and single queries. |
| 10 | + |
| 11 | +### Create a Message |
| 12 | + |
| 13 | +Creates a model response for the given conversation. |
| 14 | + |
| 15 | +**Endpoint:** `POST /v1/messages` |
| 16 | + |
| 17 | +#### Request Body |
| 18 | + |
| 19 | +The request body is a JSON object. |
| 20 | + |
| 21 | +| Parameter | Type | Required | Description | |
| 22 | +| :--------------- | :-------------- | :------- | :-------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 23 | +| `model` | string | Yes | The model that will complete your prompt. Example: `claude-3-7-sonnet-20250219`. | |
| 24 | +| `messages` | array | Yes | A list of input messages comprising the conversation so far. See [The Message Object](https://www.google.com/search?q=%23the-message-object) below. | |
| 25 | +| `max_tokens` | integer | Yes | The maximum number of tokens to generate. Different models have different maximums. | |
| 26 | +| `system` | string or array | No | A system prompt to provide context and instructions to Claude, such as specifying a role or goal. | |
| 27 | +| `metadata` | object | No | An object for metadata, such as a `user_id`, to help detect abuse. Do not include any personally identifying information. | |
| 28 | +| `stop_sequences` | array | No | Custom text sequences that will cause the model to stop generating. | |
| 29 | +| `stream` | boolean | No | If set, the response will be incrementally streamed using server-sent events. Defaults to `false`. | |
| 30 | +| `temperature` | number | No | The amount of randomness injected into the response, ranging from `0.0` to `1.0`. Defaults to `1.0`. | |
| 31 | +| `top_p` | number | No | Use nucleus sampling. The model considers tokens with `top_p` probability mass. Should alter `temperature` or `top_p`, but not both. | |
| 32 | +| `top_k` | integer | No | Only sample from the top K options for each subsequent token. Recommended for advanced use cases. | |
| 33 | +| `tools` | array | No | A list of tools the model may use. See [The Tool Object](https://www.google.com/search?q=%23the-tool-object) below. | |
| 34 | +| `tool_choice` | object | No | Controls how the model should use the provided tools. Can be `auto`, `any`, `tool`, or `none`. | |
| 35 | + |
| 36 | +#### The Message Object |
| 37 | + |
| 38 | +The `messages` array consists of message objects, where each object has a `role` and `content`. Models are trained on alternating `user` and `assistant` turns. |
| 39 | + |
| 40 | +| Parameter | Type | Required | Description | |
| 41 | +| :-------- | :-------------- | :------- | :---------------------------------------------------------------------------------------------------------------------------------- | |
| 42 | +| `role` | string | Yes | The role of the message author. Must be either `user` or `assistant`. | |
| 43 | +| `content` | string or array | Yes | The content of the message. This can be a simple string or an array of content blocks for multimodal input (e.g., text and images). | |
| 44 | + |
| 45 | +**Content Blocks:** For multimodal input, the `content` array can contain different types of blocks. |
| 46 | + |
| 47 | +- **`text`**: A block with a `type` of "text" and a `text` field containing the string. |
| 48 | +- **`image`**: A block with a `type` of "image" and a `source` object. The source must specify its `type` (e.g., "base64"), `media_type` (e.g., "image/jpeg"), and `data`. |
| 49 | +- **`tool_result`**: A block used to return the output of a tool back to the model. It includes the `tool_use_id`, `content`, and an optional `is_error` flag. |
| 50 | + |
| 51 | +#### The Tool Object |
| 52 | + |
| 53 | +The `tools` array allows you to define client-side tools the model can call. |
| 54 | + |
| 55 | +| Parameter | Type | Required | Description | |
| 56 | +| :------------- | :----- | :------- | :-------------------------------------------------------------------------------------------------------- | |
| 57 | +| `name` | string | Yes | The name of the tool, matching `^[a-zA-Z0-9_-]{1,64}$`. | |
| 58 | +| `description` | string | No | A detailed description of what the tool does, which helps the model decide when to use it. | |
| 59 | +| `input_schema` | object | Yes | A [JSON Schema](https://json-schema.org/draft/2020-12) object describing the parameters the tool accepts. | |
| 60 | + |
| 61 | +#### Response (200 OK) |
| 62 | + |
| 63 | +A successful non-streaming request returns a `Message` object. |
| 64 | + |
| 65 | +| Parameter | Type | Description | |
| 66 | +| :-------------- | :----- | :------------------------------------------------------------------------------------------------------------------------ | |
| 67 | +| `id` | string | A unique identifier for the message object. | |
| 68 | +| `type` | string | The object type, which is always `message`. | |
| 69 | +| `role` | string | The role of the author, which is always `assistant`. | |
| 70 | +| `content` | array | An array of content blocks generated by the model (e.g., `text` or `tool_use`). | |
| 71 | +| `model` | string | The model that handled the request. | |
| 72 | +| `stop_reason` | string | The reason the model stopped generating tokens. Can be `end_turn`, `max_tokens`, `stop_sequence`, or `tool_use`. | |
| 73 | +| `stop_sequence` | string | If the model was stopped by a stop sequence, this field will contain which sequence was generated. Can be null. | |
| 74 | +| `usage` | object | An object containing token usage statistics. See [The Usage Object](https://www.google.com/search?q=%23the-usage-object). | |
| 75 | + |
| 76 | +#### The Usage Object |
| 77 | + |
| 78 | +The `usage` object details billing and rate-limit token counts. |
| 79 | + |
| 80 | +| Parameter | Type | Description | |
| 81 | +| :-------------- | :------ | :------------------------------------- | |
| 82 | +| `input_tokens` | integer | The number of input tokens used. | |
| 83 | +| `output_tokens` | integer | The number of output tokens generated. | |
| 84 | + |
| 85 | +### Count Message Tokens |
| 86 | + |
| 87 | +Calculates the number of tokens for a given set of messages without executing the model. |
| 88 | + |
| 89 | +**Endpoint:** `POST /v1/messages/count_tokens` |
| 90 | + |
| 91 | +#### Request Body |
| 92 | + |
| 93 | +The request accepts a subset of the "Create a Message" parameters. |
| 94 | + |
| 95 | +| Parameter | Type | Required | Description | |
| 96 | +| :--------- | :-------------- | :------- | :----------------------------------- | |
| 97 | +| `model` | string | Yes | The model that would be used. | |
| 98 | +| `messages` | array | Yes | A list of input messages. | |
| 99 | +| `system` | string or array | No | A system prompt. | |
| 100 | +| `tools` | array | No | A list of tools the model could use. | |
| 101 | + |
| 102 | +#### Response (200 OK) |
| 103 | + |
| 104 | +A successful request returns a JSON object. |
| 105 | + |
| 106 | +| Parameter | Type | Description | |
| 107 | +| :------------- | :------ | :------------------------------------------------------------------------------ | |
| 108 | +| `input_tokens` | integer | The total number of tokens counted from the messages, system prompt, and tools. | |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +## Models API |
| 113 | + |
| 114 | +The Models API allows you to list and retrieve information about available models. |
| 115 | + |
| 116 | +### List Models |
| 117 | + |
| 118 | +Lists the currently available models, with the most recent models appearing first. |
| 119 | + |
| 120 | +**Endpoint:** `GET /v1/models` |
| 121 | + |
| 122 | +#### Response (200 OK) |
| 123 | + |
| 124 | +A successful request returns a list of model objects. |
| 125 | + |
| 126 | +| Parameter | Type | Description | |
| 127 | +| :--------- | :------ | :------------------------------------------------------------------------------ | |
| 128 | +| `data` | array | A list of [Model Objects](https://www.google.com/search?q=%23the-model-object). | |
| 129 | +| `has_more` | boolean | Indicates if more results are available for pagination. | |
| 130 | + |
| 131 | +### Get a Model |
| 132 | + |
| 133 | +Retrieves a specific model instance by its ID or alias. |
| 134 | + |
| 135 | +**Endpoint:** `GET /v1/models/{model_id}` |
| 136 | + |
| 137 | +#### Response (200 OK) |
| 138 | + |
| 139 | +A successful request returns a single [Model Object](https://www.google.com/search?q=%23the-model-object). |
| 140 | + |
| 141 | +#### The Model Object |
| 142 | + |
| 143 | +| Parameter | Type | Description | |
| 144 | +| :------------- | :----- | :------------------------------------------------------------------ | |
| 145 | +| `id` | string | The unique model identifier. Example: `claude-3-7-sonnet-20250219`. | |
| 146 | +| `type` | string | The object type, which is always `model`. | |
| 147 | +| `display_name` | string | A human-readable name for the model. Example: `Claude 3.7 Sonnet`. | |
| 148 | +| `created_at` | string | An RFC 3339 datetime string of when the model was released. | |
0 commit comments