> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apimart.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Claude Messages API

>  - Fully compatible with the native Anthropic Claude Messages protocol (`POST /v1/messages`)
- Supports multi-turn conversations, streaming SSE, tool use, and extended thinking
- Supports multimodal content including text and images
- Responses are upstream passthrough with no `{code, data}` wrapper 

<Warning>
  **Do not mix the two APIs**: `/v1/*` is the inference API (this document — upstream passthrough, no wrapper); `/api/*` is the management API (balance/logs, etc., response shape `{success, message, data}`). If you see docs claiming `/v1/messages` returns `{code, data}`, this document takes precedence.
</Warning>

<RequestExample>
  ```bash cURL theme={null}
  curl https://api.apimart.ai/v1/messages \
    -H "x-api-key: $API_KEY" \
    -H "anthropic-version: 2025-10-01" \
    -H "content-type: application/json" \
    -d '{
      "model": "claude-sonnet-4-6",
      "max_tokens": 1024,
      "messages": [
        {"role": "user", "content": "Hello, world"}
      ]
    }'
  ```

  ```python Python theme={null}
  import anthropic

  client = anthropic.Anthropic(
      api_key="YOUR_API_KEY",
      base_url="https://api.apimart.ai"
  )

  message = client.messages.create(
      model="claude-sonnet-4-6",
      max_tokens=1024,
      messages=[
          {"role": "user", "content": "Hello, world"}
      ]
  )

  print(message.content)
  ```

  ```javascript JavaScript theme={null}
  import Anthropic from '@anthropic-ai/sdk';

  const client = new Anthropic({
    apiKey: process.env.API_KEY,
    baseURL: 'https://api.apimart.ai'
  });

  const message = await client.messages.create({
    model: 'claude-sonnet-4-6',
    max_tokens: 1024,
    messages: [
      { role: 'user', content: 'Hello, world' }
    ]
  });

  console.log(message.content);
  ```

  ```go Go theme={null}
  package main

  import (
      "bytes"
      "encoding/json"
      "fmt"
      "io/ioutil"
      "net/http"
      "os"
  )

  func main() {
      url := "https://api.apimart.ai/v1/messages"

      payload := map[string]interface{}{
          "model": "claude-sonnet-4-6",
          "max_tokens": 1024,
          "messages": []map[string]string{
              {
                  "role":    "user",
                  "content": "Hello, world",
              },
          },
      }

      jsonData, _ := json.Marshal(payload)

      req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
      req.Header.Set("x-api-key", os.Getenv("API_KEY"))
      req.Header.Set("anthropic-version", "2025-10-01")
      req.Header.Set("Content-Type", "application/json")

      client := &http.Client{}
      resp, err := client.Do(req)
      if err != nil {
          panic(err)
      }
      defer resp.Body.Close()

      body, _ := ioutil.ReadAll(resp.Body)
      fmt.Println(string(body))
  }
  ```

  ```java Java theme={null}
  import java.net.http.HttpClient;
  import java.net.http.HttpRequest;
  import java.net.http.HttpResponse;
  import java.net.URI;

  public class Main {
      public static void main(String[] args) throws Exception {
          String url = "https://api.apimart.ai/v1/messages";
          String apiKey = System.getenv("API_KEY");

          String payload = """
          {
            "model": "claude-sonnet-4-6",
            "max_tokens": 1024,
            "messages": [
              {
                "role": "user",
                "content": "Hello, world"
              }
            ]
          }
          """;

          HttpClient client = HttpClient.newHttpClient();
          HttpRequest request = HttpRequest.newBuilder()
              .uri(URI.create(url))
              .header("x-api-key", apiKey)
              .header("anthropic-version", "2025-10-01")
              .header("Content-Type", "application/json")
              .POST(HttpRequest.BodyPublishers.ofString(payload))
              .build();

          HttpResponse<String> response = client.send(request,
              HttpResponse.BodyHandlers.ofString());

          System.out.println(response.body());
      }
  }
  ```

  ```php PHP theme={null}
  <?php

  $url = "https://api.apimart.ai/v1/messages";
  $apiKey = getenv('API_KEY');

  $payload = [
      "model" => "claude-sonnet-4-6",
      "max_tokens" => 1024,
      "messages" => [
          [
              "role" => "user",
              "content" => "Hello, world"
          ]
      ]
  ];

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
  curl_setopt($ch, CURLOPT_HTTPHEADER, [
      "x-api-key: " . $apiKey,
      "anthropic-version: 2025-10-01",
      "Content-Type: application/json"
  ]);

  $response = curl_exec($ch);
  curl_close($ch);

  echo $response;
  ?>
  ```

  ```ruby Ruby theme={null}
  require 'net/http'
  require 'json'
  require 'uri'

  url = URI("https://api.apimart.ai/v1/messages")
  api_key = ENV['API_KEY']

  payload = {
    model: "claude-sonnet-4-6",
    max_tokens: 1024,
    messages: [
      {
        role: "user",
        content: "Hello, world"
      }
    ]
  }

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(url)
  request["x-api-key"] = api_key
  request["anthropic-version"] = "2025-10-01"
  request["Content-Type"] = "application/json"
  request.body = payload.to_json

  response = http.request(request)
  puts response.body
  ```

  ```swift Swift theme={null}
  import Foundation

  let url = URL(string: "https://api.apimart.ai/v1/messages")!
  let apiKey = ProcessInfo.processInfo.environment["API_KEY"] ?? ""

  let payload: [String: Any] = [
      "model": "claude-sonnet-4-6",
      "max_tokens": 1024,
      "messages": [
          [
              "role": "user",
              "content": "Hello, world"
          ]
      ]
  ]

  var request = URLRequest(url: url)
  request.httpMethod = "POST"
  request.setValue(apiKey, forHTTPHeaderField: "x-api-key")
  request.setValue("2025-10-01", forHTTPHeaderField: "anthropic-version")
  request.setValue("application/json", forHTTPHeaderField: "Content-Type")
  request.httpBody = try? JSONSerialization.data(withJSONObject: payload)

  let task = URLSession.shared.dataTask(with: request) { data, response, error in
      if let error = error {
          print("Error: \(error)")
          return
      }
      
      if let data = data, let responseString = String(data: data, encoding: .utf8) {
          print(responseString)
      }
  }

  task.resume()
  ```

  ```csharp C# theme={null}
  using System;
  using System.Net.Http;
  using System.Text;
  using System.Threading.Tasks;

  class Program
  {
      static async Task Main(string[] args)
      {
          var url = "https://api.apimart.ai/v1/messages";
          var apiKey = Environment.GetEnvironmentVariable("API_KEY");

          var payload = @"{
              ""model"": ""claude-sonnet-4-6"",
              ""max_tokens"": 1024,
              ""messages"": [
                  {
                      ""role"": ""user"",
                      ""content"": ""Hello, world""
                  }
              ]
          }";

          using var client = new HttpClient();
          client.DefaultRequestHeaders.Add("x-api-key", apiKey);
          client.DefaultRequestHeaders.Add("anthropic-version", "2025-10-01");

          var content = new StringContent(payload, Encoding.UTF8, "application/json");
          var response = await client.PostAsync(url, content);
          var result = await response.Content.ReadAsStringAsync();

          Console.WriteLine(result);
      }
  }
  ```

  ```c C theme={null}
  #include <stdio.h>
  #include <curl/curl.h>
  #include <stdlib.h>

  int main(void) {
      CURL *curl;
      CURLcode res;
      const char *api_key = getenv("API_KEY");

      curl_global_init(CURL_GLOBAL_DEFAULT);
      curl = curl_easy_init();

      if(curl) {
          const char *url = "https://api.apimart.ai/v1/messages";
          const char *payload = "{"
              "\"model\":\"claude-sonnet-4-6\","
              "\"max_tokens\":1024,"
              "\"messages\":[{\"role\":\"user\",\"content\":\"Hello, world\"}]"
          "}";

          char auth_header[256];
          snprintf(auth_header, sizeof(auth_header), "x-api-key: %s", api_key);

          struct curl_slist *headers = NULL;
          headers = curl_slist_append(headers, auth_header);
          headers = curl_slist_append(headers, "anthropic-version: 2025-10-01");
          headers = curl_slist_append(headers, "Content-Type: application/json");

          curl_easy_setopt(curl, CURLOPT_URL, url);
          curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);
          curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

          res = curl_easy_perform(curl);

          if(res != CURLE_OK) {
              fprintf(stderr, "curl_easy_perform() failed: %s\n",
                      curl_easy_strerror(res));
          }

          curl_slist_free_all(headers);
          curl_easy_cleanup(curl);
      }

      curl_global_cleanup();
      return 0;
  }
  ```

  ```objectivec Objective-C theme={null}
  #import <Foundation/Foundation.h>

  int main(int argc, const char * argv[]) {
      @autoreleasepool {
          NSURL *url = [NSURL URLWithString:@"https://api.apimart.ai/v1/messages"];
          NSString *apiKey = [NSProcessInfo processInfo].environment[@"API_KEY"];
          
          NSDictionary *payload = @{
              @"model": @"claude-sonnet-4-6",
              @"max_tokens": @1024,
              @"messages": @[
                  @{
                      @"role": @"user",
                      @"content": @"Hello, world"
                  }
              ]
          };
          
          NSError *error;
          NSData *jsonData = [NSJSONSerialization dataWithJSONObject:payload
                                                            options:0
                                                              error:&error];
          
          NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
          [request setHTTPMethod:@"POST"];
          [request setValue:apiKey forHTTPHeaderField:@"x-api-key"];
          [request setValue:@"2025-10-01" forHTTPHeaderField:@"anthropic-version"];
          [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
          [request setHTTPBody:jsonData];
          
          NSURLSessionDataTask *task = [[NSURLSession sharedSession] 
              dataTaskWithRequest:request
              completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                  if (error) {
                      NSLog(@"Error: %@", error);
                      return;
                  }
                  NSString *result = [[NSString alloc] initWithData:data 
                                                          encoding:NSUTF8StringEncoding];
                  NSLog(@"%@", result);
              }];
          
          [task resume];
          [[NSRunLoop mainRunLoop] run];
      }
      return 0;
  }
  ```

  ```ocaml OCaml theme={null}
  (* Requires cohttp and yojson libraries *)
  open Lwt
  open Cohttp
  open Cohttp_lwt_unix

  let url = "https://api.apimart.ai/v1/messages"
  let api_key = Sys.getenv "API_KEY"

  let payload = {|{
    "model": "claude-sonnet-4-6",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": "Hello, world"
      }
    ]
  }|}

  let () =
    let headers = Header.init ()
      |> fun h -> Header.add h "x-api-key" api_key
      |> fun h -> Header.add h "anthropic-version" "2025-10-01"
      |> fun h -> Header.add h "Content-Type" "application/json"
    in
    let body = Cohttp_lwt.Body.of_string payload in
    
    let response = Client.post ~headers ~body (Uri.of_string url) >>= fun (resp, body) ->
      body |> Cohttp_lwt.Body.to_string >|= fun body_str ->
      print_endline body_str
    in
    Lwt_main.run response
  ```

  ```dart Dart theme={null}
  import 'dart:convert';
  import 'dart:io';
  import 'package:http/http.dart' as http;

  void main() async {
    final url = Uri.parse('https://api.apimart.ai/v1/messages');
    final apiKey = Platform.environment['API_KEY'];
    
    final payload = {
      'model': 'claude-sonnet-4-6',
      'max_tokens': 1024,
      'messages': [
        {
          'role': 'user',
          'content': 'Hello, world'
        }
      ]
    };
    
    final response = await http.post(
      url,
      headers: {
        'x-api-key': apiKey!,
        'anthropic-version': '2025-10-01',
        'Content-Type': 'application/json',
      },
      body: jsonEncode(payload),
    );
    
    print(response.body);
  }
  ```

  ```r R theme={null}
  library(httr)
  library(jsonlite)

  url <- "https://api.apimart.ai/v1/messages"
  api_key <- Sys.getenv("API_KEY")

  payload <- list(
    model = "claude-sonnet-4-6",
    max_tokens = 1024,
    messages = list(
      list(
        role = "user",
        content = "Hello, world"
      )
    )
  )

  response <- POST(
    url,
    add_headers(
      `x-api-key` = api_key,
      `anthropic-version` = "2025-10-01",
      `Content-Type` = "application/json"
    ),
    body = toJSON(payload, auto_unbox = TRUE),
    encode = "raw"
  )

  cat(content(response, "text"))
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "model": "claude-sonnet-4-6",
    "id": "msg_011CdfeHuC728oxqaLrRNbcB",
    "type": "message",
    "role": "assistant",
    "content": [
      {
        "type": "text",
        "text": "Hello! I'm Claude. Nice to meet you."
      }
    ],
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "stop_details": null,
    "usage": {
      "input_tokens": 12,
      "cache_creation_input_tokens": 0,
      "cache_read_input_tokens": 0,
      "cache_creation": {
        "ephemeral_5m_input_tokens": 0,
        "ephemeral_1h_input_tokens": 0
      },
      "output_tokens": 18,
      "service_tier": "standard",
      "inference_geo": "global"
    }
  }
  ```

  ```json 400 theme={null}
  {
    "error": {
      "code": "model_not_found",
      "message": "model not found (request id: 20260803181903172761862QzohKPXF)",
      "param": "",
      "type": "apimart_error"
    }
  }
  ```

  ```json 401 theme={null}
  {
    "error": {
      "code": "",
      "message": "Invalid API key (request id: 20260803181903172761862QzohKPXF)",
      "param": "",
      "type": "apimart_error"
    }
  }
  ```

  ```json 402 theme={null}
  {
    "error": {
      "code": "",
      "message": "Insufficient balance (request id: 20260803181903172761862QzohKPXF)",
      "param": "",
      "type": "apimart_error"
    }
  }
  ```

  ```json 429 theme={null}
  {
    "error": {
      "code": "",
      "message": "Too many requests (request id: 20260803181903172761862QzohKPXF)",
      "param": "",
      "type": "apimart_error"
    }
  }
  ```

  ```json 500 theme={null}
  {
    "error": {
      "code": "",
      "message": "Internal server error (request id: 20260803181903172761862QzohKPXF)",
      "param": "",
      "type": "apimart_error"
    }
  }
  ```
</ResponseExample>

## Authorizations

Authentication supports two methods — **use either one**:

<ParamField header="x-api-key" type="string">
  Anthropic-style API key header

  Visit the [API Key Management Page](https://apimart.ai/keys) to get your API Key

  ```
  x-api-key: YOUR_API_KEY
  ```
</ParamField>

<ParamField header="Authorization" type="string">
  Bearer token authentication (alternative to `x-api-key`)

  ```
  Authorization: Bearer YOUR_API_KEY
  ```
</ParamField>

<ParamField header="anthropic-version" type="string">
  API version (**optional** — requests work without it)

  Recommended for easier future migration to Anthropic's official endpoint:

  Example: `2025-10-01`
</ParamField>

## Body

<ParamField body="model" type="string" required default="claude-sonnet-4-6">
  Model name

  * `claude-opus-4-8` - Claude Opus 4.8 flagship model
  * `claude-opus-4-7` - Claude Opus 4.7 flagship model
  * `claude-opus-4-6` - Claude Opus 4.6 flagship model
  * `claude-sonnet-4-6` - Claude Sonnet 4.6 balanced version
  * `claude-opus-4-5-20251101` - Claude Opus 4.5 model
</ParamField>

<ParamField body="messages" type="array" required>
  List of messages

  Array of messages for the model to generate the next response. Each message contains `role` and `content` fields.

  **💡 Quick fill (Try it area):**

  1. Click "+ Add an item" to add a message
  2. `role` input: `user` (user message) or `assistant` (AI response, for multi-turn)
  3. `content` input: your message text

  <Expandable title="Field details">
    <ParamField body="role" type="string" required default="user">
      Role type

      Options: `user` (user message), `assistant` (AI response, for multi-turn conversations and prefilling)

      Note: Claude API uses a separate `system` parameter for system prompts, not in messages
    </ParamField>

    <ParamField body="content" type="string" required>
      Message content

      Text content of the message
    </ParamField>
  </Expandable>

  **Single user message:**

  ```json theme={null}
  [{"role": "user", "content": "Hello, Claude"}]
  ```

  **Multi-turn conversation:**

  ```json theme={null}
  [
    {"role": "user", "content": "Hello there."},
    {"role": "assistant", "content": "Hi, I'm Claude. How can I help you?"},
    {"role": "user", "content": "Can you explain LLMs in plain English?"}
  ]
  ```

  **Prefilled assistant response:**

  ```json theme={null}
  [
    {"role": "user", "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"},
    {"role": "assistant", "content": "The best answer is ("}
  ]
  ```
</ParamField>

<ParamField body="max_tokens" type="integer" required>
  Maximum tokens to generate (**required**, same as Anthropic official)

  Maximum number of tokens to generate before stopping. The model may stop before reaching this limit.

  Different models have different maximum values. See model docs. Minimum: 1
</ParamField>

<ParamField body="thinking" type="object">
  Extended thinking configuration

  When enabled, the response `content` may include `thinking` blocks. **Prefer** the standard model name plus this parameter over platform-side `-thinking` model aliases, so you can migrate to the official endpoint without code changes.

  If multi-turn conversations need to pass thinking blocks back, you must return the `signature` **unchanged**, or the upstream will reject the request.
</ParamField>

<ParamField body="system" type="string | array">
  System prompt

  System prompts set Claude's role, personality, goals, and instructions.

  **String format:**

  ```json theme={null}
  {
    "system": "You are a professional Python programming tutor"
  }
  ```

  **Structured format:**

  ```json theme={null}
  {
    "system": [
      {
        "type": "text",
        "text": "You are a professional Python programming tutor"
      }
    ]
  }
  ```
</ParamField>

<ParamField body="temperature" type="number">
  Temperature parameter, range 0-1

  Controls randomness of output:

  * Low values (e.g., 0.2): More deterministic, conservative
  * High values (e.g., 0.8): More random, creative

  Default: 1.0
</ParamField>

<ParamField body="top_p" type="number">
  Nucleus sampling parameter, range 0-1

  Uses nucleus sampling. Recommend using either `temperature` or `top_p`, not both.

  Default: 1.0
</ParamField>

<ParamField body="top_k" type="integer">
  Top-K sampling

  Sample from top K options only, removes "long tail" low probability responses.

  Recommended for advanced use cases only.
</ParamField>

<ParamField body="stream" type="boolean">
  Enable streaming

  When `true`, uses Server-Sent Events (SSE) to stream responses.

  Default: false
</ParamField>

<ParamField body="stop_sequences" type="array">
  Stop sequences

  Custom text sequences that cause the model to stop generating.

  Maximum 4 sequences.

  Example: `["\n\nHuman:", "\n\nAssistant:"]`
</ParamField>

<ParamField body="metadata" type="object">
  Metadata

  Metadata object for the request.

  Includes:

  * `user_id`: User identifier
</ParamField>

<ParamField body="tools" type="array">
  Tool definitions

  List of tools the model can use to complete tasks.

  **Function tool example:**

  ```json theme={null}
  {
    "tools": [
      {
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "input_schema": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"],
              "description": "Temperature unit"
            }
          },
          "required": ["location"]
        }
      }
    ]
  }
  ```

  Supported tool types:

  * Custom function tools
  * Computer use tool (computer\_20241022)
  * Text editor tool (text\_editor\_20241022)
  * Bash tool (bash\_20241022)
</ParamField>

<ParamField body="tool_choice" type="object">
  Tool choice strategy

  Controls how the model uses tools:

  * `{"type": "auto"}`: Auto-decide (default)
  * `{"type": "any"}`: Must use a tool
  * `{"type": "tool", "name": "tool_name"}`: Use specific tool
</ParamField>

## Response

<ResponseField name="id" type="string">
  Unique message identifier

  Example: `"msg_013Zva2CMHLNnXjNJJKqJ2EF"`
</ResponseField>

<ResponseField name="type" type="string">
  Object type

  Always `"message"`
</ResponseField>

<ResponseField name="role" type="string">
  Role

  Always `"assistant"`
</ResponseField>

<ResponseField name="content" type="array">
  Content blocks array

  `content` is an array of blocks distinguished by `type`. **A single response may contain multiple blocks** (for example, with thinking enabled: a `thinking` block plus a `text` block).

  **text block:**

  ```json theme={null}
  { "type": "text", "text": "OK" }
  ```

  **tool\_use block:**

  ```json theme={null}
  {
    "type": "tool_use",
    "id": "toolu_01QgsazxKXSfQVj9Q1XxjYXo",
    "name": "get_weather",
    "input": { "city": "Beijing" },
    "caller": { "type": "direct" }
  }
  ```

  <Note>
    `caller` is a newer upstream field not yet documented officially; ignore it when parsing.
  </Note>

  **thinking block** (present when the request body includes the `thinking` parameter):

  ```json theme={null}
  {
    "type": "thinking",
    "thinking": "Reasoning process text...",
    "signature": "<signature string, typically 500+ characters>"
  }
  ```

  <Warning>
    When returning thinking blocks in multi-turn conversations, you must pass `signature` back **unchanged**, or the upstream will reject the request.
  </Warning>

  <Warning>
    **Do not assume `content[0]` is text.** With thinking enabled, `content[0]` may be a thinking block. Iterate and filter:

    ```python theme={null}
    text = "".join(b.text for b in resp.content if b.type == "text")
    ```
  </Warning>
</ResponseField>

<ResponseField name="model" type="string">
  Model that handled the request

  Example: `"claude-sonnet-4-6"`
</ResponseField>

<ResponseField name="stop_reason" type="string">
  Stop reason

  Possible values:

  * `end_turn`: Natural completion
  * `max_tokens`: Reached maximum tokens
  * `stop_sequence`: Hit stop sequence
  * `tool_use`: Invoked a tool
</ResponseField>

<ResponseField name="stop_sequence" type="string | null">
  Stop sequence triggered

  The stop sequence that was generated, if any; otherwise `null`
</ResponseField>

<ResponseField name="stop_details" type="object | null">
  Newer Anthropic field; `null` for typical requests
</ResponseField>

<ResponseField name="usage" type="object">
  Token usage statistics (full structure for non-streaming)

  <Expandable title="Properties">
    <ResponseField name="input_tokens" type="integer">
      Number of input tokens
    </ResponseField>

    <ResponseField name="output_tokens" type="integer">
      Number of output tokens (**already includes** thinking tokens; do not double-count for billing)
    </ResponseField>

    <ResponseField name="cache_creation_input_tokens" type="integer">
      Cache write tokens
    </ResponseField>

    <ResponseField name="cache_read_input_tokens" type="integer">
      Cache hit tokens
    </ResponseField>

    <ResponseField name="cache_creation" type="object">
      `{ ephemeral_5m_input_tokens, ephemeral_1h_input_tokens }`
    </ResponseField>

    <ResponseField name="service_tier" type="string">
      e.g. `"standard"`
    </ResponseField>

    <ResponseField name="inference_geo" type="string">
      e.g. `"global"`
    </ResponseField>

    <ResponseField name="output_tokens_details" type="object">
      May appear when thinking is enabled: `{ thinking_tokens: int }`
    </ResponseField>
  </Expandable>
</ResponseField>

## Usage Examples

### Basic Conversation

```python theme={null}
import anthropic

client = anthropic.Anthropic(
    api_key="YOUR_API_KEY",
    base_url="https://api.apimart.ai"
)

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain quantum computing basics"}
    ]
)

print(message.content[0].text)
```

### Multi-turn Conversation

```python theme={null}
messages = [
    {"role": "user", "content": "What is machine learning?"},
    {"role": "assistant", "content": "Machine learning is a branch of AI..."},
    {"role": "user", "content": "Can you give a practical example?"}
]

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=messages
)
```

### Using System Prompts

```python theme={null}
message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system="You are a senior Python developer expert in code review and optimization.",
    messages=[
        {"role": "user", "content": "How to optimize this code?\n\n[code]"}
    ]
)
```

### Streaming Response

```python theme={null}
with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Write a short essay about AI"}
    ]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
```

### Tool Use

```python theme={null}
tools = [
    {
        "name": "get_stock_price",
        "description": "Get real-time stock price",
        "input_schema": {
            "type": "object",
            "properties": {
                "ticker": {
                    "type": "string",
                    "description": "Stock ticker symbol, e.g., AAPL"
                }
            },
            "required": ["ticker"]
        }
    }
]

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=tools,
    messages=[
        {"role": "user", "content": "What's Tesla's stock price?"}
    ]
)

# Handle tool calls
if message.stop_reason == "tool_use":
    tool_use = next(block for block in message.content if block.type == "tool_use")
    print(f"Calling tool: {tool_use.name}")
    print(f"Arguments: {tool_use.input}")
```

### Vision Understanding

```python theme={null}
message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "url",
                        "url": "https://example.com/image.jpg"
                    }
                },
                {
                    "type": "text",
                    "text": "Describe this image"
                }
            ]
        }
    ]
)
```

### Base64 Image

```python theme={null}
import base64

with open("image.jpg", "rb") as image_file:
    image_data = base64.b64encode(image_file.read()).decode("utf-8")

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": "image/jpeg",
                        "data": image_data
                    }
                },
                {
                    "type": "text",
                    "text": "Analyze this image"
                }
            ]
        }
    ]
)
```

## Best Practices

### 1. Prompt Engineering

**Clear role definition:**

```python theme={null}
system = """You are an experienced data scientist specializing in:
- Statistical analysis and data visualization
- Machine learning model development
- Python and R programming
Provide professional, accurate advice."""
```

**Structured output:**

```python theme={null}
message = "Please return the analysis results in JSON format with summary, key_findings, and recommendations fields."
```

### 2. Error Handling

```python theme={null}
from anthropic import APIError, RateLimitError

try:
    message = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello"}]
    )
except RateLimitError:
    print("Rate limit exceeded, please retry later")
except APIError as e:
    print(f"API error: {e}")
```

### 3. Token Optimization

```python theme={null}
# Use shorter prompts
messages = [
    {"role": "user", "content": "Summarize key points:\n\n[long text]"}
]

# Limit output length
message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=500,  # Limit output
    messages=messages
)
```

### 4. Prefilling Responses

```python theme={null}
# Guide model to specific format
messages = [
    {"role": "user", "content": "List 5 Python best practices"},
    {"role": "assistant", "content": "Here are 5 Python best practices:\n\n1."}
]

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=messages
)
```

## Streaming Response Handling

### Python Streaming

```python theme={null}
import anthropic

client = anthropic.Anthropic(
    api_key="YOUR_API_KEY",
    base_url="https://api.apimart.ai"
)

with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Write a Python decorator example"}
    ]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
```

### JavaScript Streaming

```javascript theme={null}
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.API_KEY,
  baseURL: 'https://api.apimart.ai'
});

const stream = await client.messages.stream({
  model: 'claude-sonnet-4-6',
  max_tokens: 1024,
  messages: [
    { role: 'user', content: 'Write a React component example' }
  ]
});

for await (const chunk of stream) {
  if (chunk.type === 'content_block_delta' && 
      chunk.delta.type === 'text_delta') {
    process.stdout.write(chunk.delta.text);
  }
}
```

## Platform Differences & Integration Notes

### Unwrapped response

Successful `POST /v1/messages` responses **return the Anthropic message object directly**, with **no** `{code, data}` wrapper. This is required for 1:1 compatibility with official SDKs, Claude Code, Cline, and similar tools.

### Error format (only real incompatibility with official)

```json theme={null}
{
  "error": {
    "code": "model_not_found",
    "message": "... (request id: ...)",
    "param": "",
    "type": "apimart_error"
  }
}
```

Compared with Anthropic official: missing top-level `"type": "error"`; `error.type` is always `apimart_error`, not semantic types like `invalid_request_error`.

**Integration guidance**: do not branch retries on `error.type`; use **HTTP status + `error.code`** instead:

| Status | Meaning                | Suggested action                   |
| ------ | ---------------------- | ---------------------------------- |
| 400    | Bad request parameters | Do not retry; fix the request body |
| 401    | Invalid key            | Do not retry                       |
| 402    | Insufficient balance   | Do not retry; prompt top-up        |
| 429    | Rate limited           | Retry with backoff                 |
| 5xx    | Upstream/gateway error | Retry with exponential backoff     |

When reporting issues, include the request id at the end of `error.message` and the response header `x-oneapi-request-id`.

### Streaming SSE

Send `"stream": true`. Event sequence matches official:

`message_start` → `content_block_start` → `ping` → `content_block_delta` (multiple) → `content_block_stop` → `message_delta` → `message_stop`

⚠️ **Stream vs non-stream `usage` differs**: `message_delta.usage` typically has only 4 token fields and **does not** include `cache_creation`, `service_tier`, or `inference_geo`. Parse them separately or treat all as optional.

### Unimplemented endpoint

`POST /v1/messages/count_tokens` is **not implemented and returns 404**. Official SDK `client.messages.count_tokens()` will fail. Estimate tokens locally, or read `usage.input_tokens` from responses.

### Ignore unknown fields

This endpoint passes through upstream fields. Anthropic may add fields at any time (e.g. `stop_details`, `inference_geo`, `caller`, `output_tokens_details`). Do not use strict schemas:

* Go: do not use `DisallowUnknownFields()`
* Pydantic: do not use `extra="forbid"`
* TypeScript / Zod: use `.passthrough()` instead of `.strict()`

### Model name recommendation

Same-name models with a `-thinking` suffix are platform extension aliases. **Prefer** the standard model name without the suffix plus the request-body `thinking` parameter for easier migration to the official endpoint.

Other request-body fields match official: `model`, `messages`, `max_tokens` (required), `system`, `temperature`, `top_p`, `top_k`, `stop_sequences`, `stream`, `tools`, `tool_choice`, `thinking`, `metadata`. Semantics follow the [Anthropic Messages API](https://docs.anthropic.com/en/api/messages).

## Important Notes

1. **API Key Security**:
   * Store API keys in environment variables
   * Never hardcode keys in source code
   * Rotate keys regularly

2. **Rate Limiting**:
   * Be aware of API rate limits
   * Implement retry mechanisms (by HTTP status code)
   * Use exponential backoff

3. **Token Management**:
   * Monitor token usage (read `usage`)
   * Optimize prompt length
   * Use appropriate `max_tokens` values
   * With thinking enabled, `output_tokens` already includes thinking tokens — do not double-count for billing

4. **Model Selection**:
   * Opus: Complex tasks, deep thinking required
   * Sonnet: Balanced performance and cost
   * Haiku: Fast response, simple tasks

5. **Content parsing**:
   * Iterate `content` for `type == "text"`; do not hardcode `content[0].text`
   * If the model returns JSON wrapped in Markdown code fences, that is model output — not an API wrapper (see FAQ below)

6. **Content Filtering**:
   * Validate user input
   * Filter sensitive information
   * Implement content moderation

## FAQ

### The response `content` text is a ` ```json ... ``` ` code fence — how do I strip it?

This is not an API structure issue. The `text` field holds the **raw model-generated content**: if the model decides you want JSON, it may wrap it in a Markdown code fence. The API does not and should not rewrite model output.

To get clean structured data, use one of these three approaches (recommended from highest to lowest reliability):

1. **Use tools to force structured output** — most reliable; the `input` field is already a parsed object:

```json theme={null}
{
  "tools": [{
    "name": "emit_result",
    "input_schema": {
      "type": "object",
      "properties": { "answer": { "type": "string" } }
    }
  }],
  "tool_choice": { "type": "tool", "name": "emit_result" }
}
```

2. **Prefill the assistant message** so the model continues from `{`:

```json theme={null}
{
  "messages": [
    { "role": "user", "content": "..." },
    { "role": "assistant", "content": "{" }
  ]
}
```

3. In the system prompt, explicitly require “output JSON only, with no Markdown code fences.”

Do not rely on regex to strip code fences — parsing will break when the model occasionally omits the fence.
