|
1 | 1 | # Anthropic |
2 | 2 |
|
3 | | -The chat models from this provider have some unique characteristics. In addition to the standard `v1/chat/completions` endpoint, you can also call Anthropic models via the `/messages` endpoint. This capability is described in more detail in the [Capabilities](../../../capabilities/anthropic.md) section. |
| 3 | +## Overview |
| 4 | + |
| 5 | +**Anthropic** is an AI research and product company founded by former OpenAI researchers. The company is best known for its strong emphasis on AI safety, interpretability, and long-term alignment. Anthropic describes its mission as building “reliable, interpretable, and steerable” AI systems that can be safely deployed at scale. |
| 6 | + |
| 7 | +Its product lineup spans lightweight **Haiku** models for fast inference, **Sonnet** models aimed at general-purpose production workloads, and **Opus** models designed for advanced reasoning, coding, and research tasks. Beyond chat assistants, Anthropic is actively expanding into agentic tooling with products such as [Claude Code](../../../integrations/claude-code.md) and enterprise integrations focused on automation, software engineering, and organizational workflows. |
| 8 | + |
| 9 | +*** |
| 10 | + |
| 11 | +The chat models from this provider have some unique characteristics. Models from Anthropic can be accessed not only via the standard `/v1/chat/completions` endpoint but also through dedicated endpoints — `/messages` and `/v1/batches` and `/v1/batches/cancel/{batch_id}`.\ |
| 12 | +The sections below describe their API schemas, usage specifics, and example requests. |
| 13 | + |
| 14 | +Supported capabilities: |
| 15 | + |
| 16 | +* **Text completions:** Build advanced chat bots or text processors. |
| 17 | +* **Function Calling:** Utilize tools for specific tasks and API calling. |
| 18 | +* **Stream mode:** Get the text chat model responses as they are generated, rather than waiting for the entire response to be completed. |
| 19 | +* **Batch Processing:** Send multiple independent requests in a single API call. |
| 20 | +* **Vision Tasks:** Process and analyze images. |
| 21 | + |
| 22 | +## Text Completions |
| 23 | + |
| 24 | +Ask something and get an answer in a chat-like conversation format. |
| 25 | + |
| 26 | +{% openapi-operation spec="messages" path="/v1/messages" method="post" %} |
| 27 | +[OpenAPI messages](https://raw.githubusercontent.com/aimlapi/api-docs/refs/heads/main/docs/capabilities/messages.json) |
| 28 | +{% endopenapi-operation %} |
| 29 | + |
| 30 | +### **Example: Simple Text Response** |
| 31 | + |
| 32 | +{% tabs %} |
| 33 | +{% tab title="Python" %} |
| 34 | +{% code overflow="wrap" %} |
| 35 | +```python |
| 36 | +import requests |
| 37 | +import json # for getting a structured output with indentation |
| 38 | + |
| 39 | +url = "https://api.aimlapi.com/messages" |
| 40 | +headers = { |
| 41 | + # Insert your AIML API Key instead of <YOUR_AIMLAPI_KEY>: |
| 42 | + "Authorization": "Bearer <YOUR_AIMLAPI_KEY>", |
| 43 | + "Content-Type": "application/json" |
| 44 | +} |
| 45 | +payload = { |
| 46 | + "model": "claude-sonnet-4-20250514", |
| 47 | + "max_tokens": 1024, |
| 48 | + "system": "You are a robot. You always optimize for clarity, structure, and accuracy.", |
| 49 | + "messages": [ |
| 50 | + { |
| 51 | + "role": "user", |
| 52 | + "content": "How are you?" |
| 53 | + } |
| 54 | + ] |
| 55 | +} |
| 56 | +response = requests.post(url, json=payload, headers=headers) |
| 57 | +data = response.json() |
| 58 | +print(json.dumps(data, indent=2, ensure_ascii=False)) |
| 59 | +``` |
| 60 | +{% endcode %} |
| 61 | +{% endtab %} |
| 62 | +{% endtabs %} |
| 63 | + |
| 64 | +<details> |
| 65 | + |
| 66 | +<summary>Response</summary> |
| 67 | + |
| 68 | +{% code overflow="wrap" %} |
| 69 | +```json5 |
| 70 | +{ |
| 71 | + "model": "claude-sonnet-4-20250514", |
| 72 | + "id": "msg_01SUmNmSRFZsoa6h96MxJEHH", |
| 73 | + "type": "message", |
| 74 | + "role": "assistant", |
| 75 | + "content": [ |
| 76 | + { |
| 77 | + "type": "text", |
| 78 | + "text": "I'm functioning well, thank you for asking! I'm ready to help you with any questions or tasks you might have. How can I assist you today?" |
| 79 | + } |
| 80 | + ], |
| 81 | + "stop_reason": "end_turn", |
| 82 | + "stop_sequence": null, |
| 83 | + "stop_details": null, |
| 84 | + "usage": { |
| 85 | + "input_tokens": 27, |
| 86 | + "cache_creation_input_tokens": 0, |
| 87 | + "cache_read_input_tokens": 0, |
| 88 | + "cache_creation": { |
| 89 | + "ephemeral_5m_input_tokens": 0, |
| 90 | + "ephemeral_1h_input_tokens": 0 |
| 91 | + }, |
| 92 | + "output_tokens": 35, |
| 93 | + "service_tier": "standard", |
| 94 | + "inference_geo": "not_available" |
| 95 | + }, |
| 96 | + "meta": { |
| 97 | + "usage": { |
| 98 | + "credits_used": 1576, |
| 99 | + "usd_spent": 0.000788 |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | +{% endcode %} |
| 105 | + |
| 106 | +</details> |
| 107 | + |
| 108 | +*** |
| 109 | + |
| 110 | +## **Function Calling** |
| 111 | + |
| 112 | +To process text and use function calling, follow the examples below: |
| 113 | + |
| 114 | +### **Example: Get Weather Information** |
| 115 | + |
| 116 | +{% code overflow="wrap" %} |
| 117 | +```python |
| 118 | +import requests |
| 119 | + |
| 120 | +url = "https://api.aimlapi.com/messages" |
| 121 | +headers = { |
| 122 | + "Authorization": "Bearer YOUR_AIMLAPI_KEY", |
| 123 | + "Content-Type": "application/json" |
| 124 | +} |
| 125 | +payload = { |
| 126 | + "model": "anthropic/claude-sonnet-4.5", |
| 127 | + "max_tokens": 1024, |
| 128 | + "tools": [ |
| 129 | + { |
| 130 | + "name": "get_weather", |
| 131 | + "description": "Get the current weather in a given location", |
| 132 | + "input_schema": { |
| 133 | + "type": "object", |
| 134 | + "properties": { |
| 135 | + "location": { |
| 136 | + "type": "string", |
| 137 | + "description": "The city and state, e.g. San Francisco, CA" |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + ], |
| 143 | + "messages": [ |
| 144 | + { |
| 145 | + "role": "user", |
| 146 | + "content": "What is the weather like in San Francisco?" |
| 147 | + } |
| 148 | + ] |
| 149 | +} |
| 150 | +response = requests.post(url, json=payload, headers=headers) |
| 151 | +print(response.json()) |
| 152 | +``` |
| 153 | +{% endcode %} |
| 154 | + |
| 155 | +*** |
| 156 | + |
| 157 | +## Streaming Mode |
| 158 | + |
| 159 | +To enable streaming of responses, set `stream=True` in your request payload. |
| 160 | + |
| 161 | +```python |
| 162 | +import requests |
| 163 | + |
| 164 | +url = "https://api.aimlapi.com/messages" |
| 165 | +headers = { |
| 166 | + "Authorization": "Bearer YOUR_AIMLAPI_KEY", |
| 167 | + "Content-Type": "application/json" |
| 168 | +} |
| 169 | +payload = { |
| 170 | + "model": "anthropic/claude-sonnet-4.5", |
| 171 | + "max_tokens": 1024, |
| 172 | + "tools": [ |
| 173 | + { |
| 174 | + "name": "get_weather", |
| 175 | + "description": "Get the current weather in a given location", |
| 176 | + "input_schema": { |
| 177 | + "type": "object", |
| 178 | + "properties": { |
| 179 | + "location": { |
| 180 | + "type": "string", |
| 181 | + "description": "The city and state, e.g. San Francisco, CA" |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + ], |
| 187 | + "messages": [ |
| 188 | + { |
| 189 | + "role": "user", |
| 190 | + "content": "What is the weather like in San Francisco?" |
| 191 | + } |
| 192 | + ] |
| 193 | +``` |
| 194 | + |
| 195 | +*** |
| 196 | + |
| 197 | +## **Batch Processing** |
| 198 | + |
| 199 | +Due to the complexity of its description, this capability has been placed on [a separate page](../../../capabilities/batch-processing.md). |
| 200 | + |
| 201 | +*** |
| 202 | + |
| 203 | +## **Vision** |
| 204 | + |
| 205 | +{% hint style="info" %} |
| 206 | +**Note:** API only support [Base64 string](../../../glossary/concepts.md#base64) as image input. |
| 207 | +{% endhint %} |
| 208 | + |
| 209 | +Possible media types: |
| 210 | + |
| 211 | +* `image/jpeg` |
| 212 | +* `image/png` |
| 213 | +* `image/gif` |
| 214 | +* `image/webp` |
| 215 | + |
| 216 | +{% code overflow="wrap" %} |
| 217 | +```python |
| 218 | +import httpx |
| 219 | +import base64 |
| 220 | +from openai import OpenAI |
| 221 | + |
| 222 | +client = OpenAI( |
| 223 | + base_url='https://api.aimlapi.com', |
| 224 | + api_key='<YOUR_AIMLAPI_KEY>' |
| 225 | +) |
| 226 | + |
| 227 | +image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg" |
| 228 | +image_media_type = "image/jpeg" |
| 229 | +image_data = base64.standard_b64encode(httpx.get(image_url).content).decode("utf-8") |
| 230 | + |
| 231 | +response = client.chat.completions.create( |
| 232 | + model="anthropic/claude-sonnet-4.5", |
| 233 | + messages=[ |
| 234 | + { |
| 235 | + "role": "user", |
| 236 | + "content": [ |
| 237 | + { |
| 238 | + "type": "image", |
| 239 | + "source": { |
| 240 | + "type": "base64", |
| 241 | + "media_type": image_media_type, |
| 242 | + "data": imag1_data, |
| 243 | + }, |
| 244 | + }, |
| 245 | + { |
| 246 | + "type": "text", |
| 247 | + "text": "Describe this image." |
| 248 | + } |
| 249 | + ], |
| 250 | + } |
| 251 | +], |
| 252 | +) |
| 253 | +print(response) |
| 254 | +``` |
| 255 | +{% endcode %} |
| 256 | + |
| 257 | +*** |
| 258 | + |
| 259 | +## Response Format |
| 260 | + |
| 261 | +The responses from the AI/ML API for Anthropic models will typically include the generated text or results from the tool called. Here is an example response for a weather query: |
| 262 | + |
| 263 | +{% code overflow="wrap" %} |
| 264 | +```json |
| 265 | +{ |
| 266 | + "model": "claude-sonnet-4-20250514", |
| 267 | + "id": "msg_014iMvypzB9GafRthc8CQHsR", |
| 268 | + "type": "message", |
| 269 | + "role": "assistant", |
| 270 | + "content": [ |
| 271 | + { |
| 272 | + "type": "text", |
| 273 | + "text": "I'm doing well, thank you for asking! I'm here and ready to help with whatever you'd like to discuss or work on. How are you doing today?" |
| 274 | + } |
| 275 | + ], |
| 276 | + "stop_reason": "end_turn", |
| 277 | + "stop_sequence": null, |
| 278 | + "stop_details": null, |
| 279 | + "usage": { |
| 280 | + "input_tokens": 11, |
| 281 | + "cache_creation_input_tokens": 0, |
| 282 | + "cache_read_input_tokens": 0, |
| 283 | + "cache_creation": { |
| 284 | + "ephemeral_5m_input_tokens": 0, |
| 285 | + "ephemeral_1h_input_tokens": 0 |
| 286 | + }, |
| 287 | + "output_tokens": 37, |
| 288 | + "service_tier": "standard", |
| 289 | + "inference_geo": "not_available" |
| 290 | + }, |
| 291 | + "meta": { |
| 292 | + "usage": { |
| 293 | + "credits_used": 1529, |
| 294 | + "usd_spent": 0.0007645 |
| 295 | + } |
| 296 | + } |
| 297 | +} |
| 298 | +``` |
| 299 | +{% endcode %} |
0 commit comments