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

# Create Messages (Claude)

> Claude native message interface for Anthropic clients like Claude Code

## Introduction

The Messages API provides native Anthropic Claude interface compatibility through MixRoute , allowing direct use of Anthropic SDKs and tools like Claude Code. This interface follows Anthropic's API specification and provides full Claude model functionality, including Extended Thinking, tool calling, and other advanced features.

If you're using OpenAI-compatible clients (like OpenAI SDK), we recommend using the `/v1/chat/completions` endpoint instead.

## Authentication

Bearer Token, e.g., `Bearer sk-xxxxxxxxxx`

## Request Parameters

<ParamField body="model" type="string" required>
  Claude model identifier, e.g., `claude-opus-4-8`
</ParamField>

<ParamField body="messages" type="array" required>
  List of conversation messages, each containing `role` (user/assistant) and `content`
</ParamField>

<ParamField body="max_tokens" type="integer" required>
  Maximum tokens to generate, must be greater than 0
</ParamField>

<ParamField body="system" type="string | array">
  System prompt, supports string format or array format (for Prompt Caching)
</ParamField>

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

<ParamField body="temperature" type="number">
  Sampling temperature, range 0-1
</ParamField>

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

<ParamField body="top_k" type="integer">
  Top-k sampling parameter
</ParamField>

<ParamField body="stop_sequences" type="array">
  Custom stop sequences
</ParamField>

<ParamField body="thinking" type="object">
  Extended thinking configuration, contains `type` and `budget_tokens`
</ParamField>

<ParamField body="tools" type="array">
  List of tool definitions
</ParamField>

## Basic Examples

<Tabs>
  <Tab title="Non-Streaming Request">
    ```bash theme={null}
    curl -X POST "https://api.mixroute.ai/v1/messages" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer sk-xxxxxxxxxx" \
      -H "anthropic-version: 2023-06-01" \
      -d '{
        "model": "claude-opus-4-8",
        "max_tokens": 1024,
        "messages": [
          {"role": "user", "content": "Briefly explain artificial intelligence"}
        ]
      }'
    ```
  </Tab>

  <Tab title="Streaming Request (SSE)">
    ```bash theme={null}
    curl -X POST "https://api.mixroute.ai/v1/messages" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer sk-xxxxxxxxxx" \
      -H "anthropic-version: 2023-06-01" \
      -d '{
        "model": "claude-opus-4-8",
        "max_tokens": 1024,
        "stream": true,
        "messages": [
          {"role": "user", "content": "Briefly explain artificial intelligence"}
        ]
      }'
    ```
  </Tab>

  <Tab title="Python Example">
    ```python theme={null}
    from anthropic import Anthropic

    client = Anthropic(
        api_key="sk-xxxxxxxxxx",
        base_url="https://api.mixroute.ai"
    )

    # Non-streaming
    message = client.messages.create(
        model="claude-opus-4-8",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "Briefly explain artificial intelligence"}
        ]
    )
    print(message.content[0].text)

    # Streaming
    with client.messages.stream(
        model="claude-opus-4-8",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "Briefly explain artificial intelligence"}
        ]
    ) as stream:
        for text_block in stream.text_stream:
            print(text_block, end="")
    ```
  </Tab>
</Tabs>

## Advanced Features

### System Prompt

<Tabs>
  <Tab title="String Format">
    ```bash theme={null}
    curl -X POST "https://api.mixroute.ai/v1/messages" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer sk-xxxxxxxxxx" \
      -H "anthropic-version: 2023-06-01" \
      -d '{
        "model": "claude-opus-4-8",
        "max_tokens": 1024,
        "system": "You are a professional programming assistant skilled at explaining complex technical concepts.",
        "messages": [
          {"role": "user", "content": "What is recursion?"}
        ]
      }'
    ```
  </Tab>

  <Tab title="Array Format">
    ```bash theme={null}
    curl -X POST "https://api.mixroute.ai/v1/messages" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer sk-xxxxxxxxxx" \
      -H "anthropic-version: 2023-06-01" \
      -d '{
        "model": "claude-opus-4-8",
        "max_tokens": 1024,
        "system": [
          {
            "type": "text",
            "text": "You are a professional programming assistant skilled at explaining complex technical concepts."
          }
        ],
        "messages": [
          {"role": "user", "content": "What is recursion?"}
        ]
      }'
    ```
  </Tab>
</Tabs>

### Extended Thinking

<Tabs>
  <Tab title="Basic Usage">
    ```bash theme={null}
    curl -X POST "https://api.mixroute.ai/v1/messages" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer sk-xxxxxxxxxx" \
      -H "anthropic-version: 2023-06-01" \
      -d '{
        "model": "claude-opus-4-8",
        "max_tokens": 16000,
        "thinking": {
          "type": "enabled",
          "budget_tokens": 10000
        },
        "messages": [
          {"role": "user", "content": "Provide a medium difficulty geometry problem and solve it step by step"}
        ]
      }'
    ```
  </Tab>

  <Tab title="Python Example">
    ```python theme={null}
    from anthropic import Anthropic

    client = Anthropic(
        api_key="sk-xxxxxxxxxx",
        base_url="https://api.mixroute.ai"
    )

    with client.messages.stream(
        model="claude-opus-4-8",
        max_tokens=16000,
        thinking={
            "type": "enabled",
            "budget_tokens": 10000
        },
        messages=[
            {"role": "user", "content": "Provide a medium difficulty geometry problem and solve it step by step"}
        ]
    ) as stream:
        for event in stream:
            if event.type == "content_block_delta":
                if hasattr(event.delta, "thinking"):
                    print(f"[Thinking] {event.delta.thinking}", end="")
                elif hasattr(event.delta, "text"):
                    print(event.delta.text, end="")
    ```
  </Tab>
</Tabs>

### Tools

<Tabs>
  <Tab title="Function Tools">
    ```bash theme={null}
    curl -X POST "https://api.mixroute.ai/v1/messages" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer sk-xxxxxxxxxx" \
      -H "anthropic-version: 2023-06-01" \
      -d '{
        "model": "claude-opus-4-8",
        "max_tokens": 1024,
        "tools": [
          {
            "name": "get_weather",
            "description": "Get weather information for a city",
            "input_schema": {
              "type": "object",
              "properties": {
                "city": {
                  "type": "string",
                  "description": "City name"
                }
              },
              "required": ["city"]
            }
          }
        ],
        "messages": [
          {"role": "user", "content": "What is the weather like in Tokyo?"}
        ]
      }'
    ```
  </Tab>

  <Tab title="Claude Official Web Search Tool">
    ```bash theme={null}
    curl -X POST "https://api.mixroute.ai/v1/messages" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer sk-xxxxxxxxxx" \
      -H "anthropic-version: 2023-06-01" \
      -d '{
        "model": "claude-opus-4-8",
        "max_tokens": 4096,
        "tools": [
          {
            "type": "web_search_20250305",
            "name": "web_search",
            "max_uses": 5
          }
        ],
        "messages": [
          {"role": "user", "content": "What are the latest news about artificial intelligence?"}
        ]
      }'
    ```
  </Tab>
</Tabs>

### Multimodal Input (Images)

```bash theme={null}
curl -X POST "https://api.mixroute.ai/v1/messages" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-xxxxxxxxxx" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-opus-4-8",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "image",
            "source": {
              "type": "base64",
              "media_type": "image/jpeg",
              "data": "base64_encoded_image_data"
            }
          },
          {
            "type": "text",
            "text": "Please describe this image"
          }
        ]
      }
    ]
  }'
```

### Prompt Caching

By caching frequently used context content, you can significantly reduce costs and improve response speed. Cached content requires a minimum of 1024 tokens.

<Tabs>
  <Tab title="System Cache">
    ```bash theme={null}
    curl -X POST "https://api.mixroute.ai/v1/messages" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer sk-xxxxxxxxxx" \
      -H "anthropic-version: 2023-06-01" \
      -d '{
        "model": "claude-opus-4-8",
        "max_tokens": 1024,
        "system": [
          {
            "type": "text",
            "text": "You are a professional document analysis assistant. Here is the document content to analyze: [long text content, at least 1024 tokens]",
            "cache_control": {"type": "ephemeral"}
          }
        ],
        "messages": [
          {"role": "user", "content": "Please summarize the main points of the document"}
        ]
      }'
    ```
  </Tab>

  <Tab title="Messages Cache">
    ```bash theme={null}
    curl -X POST "https://api.mixroute.ai/v1/messages" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer sk-xxxxxxxxxx" \
      -H "anthropic-version: 2023-06-01" \
      -d '{
        "model": "claude-opus-4-8",
        "max_tokens": 1024,
        "messages": [
          {
            "role": "user",
            "content": [
              {
                "type": "text",
                "text": "Here is the codebase to analyze: [large code content, at least 1024 tokens]",
                "cache_control": {"type": "ephemeral"}
              },
              {
                "type": "text",
                "text": "Please identify potential issues in the code"
              }
            ]
          }
        ]
      }'
    ```
  </Tab>

  <Tab title="Python SDK Example">
    ```python theme={null}
    from anthropic import Anthropic

    client = Anthropic(
        api_key="sk-xxxxxxxxxx",
        base_url="https://api.mixroute.ai"
    )

    # System cache
    message = client.messages.create(
        model="claude-opus-4-8",
        max_tokens=1024,
        system=[
            {
                "type": "text",
                "text": "You are a professional document analysis assistant. Here is the document content to analyze: [long text content]",
                "cache_control": {"type": "ephemeral"}
            }
        ],
        messages=[
            {"role": "user", "content": "Please summarize the main points of the document"}
        ]
    )

    # Check cache usage
    print(f"Cache creation tokens: {message.usage.cache_creation_input_tokens}")
    print(f"Cache read tokens: {message.usage.cache_read_input_tokens}")
    ```
  </Tab>
</Tabs>

## Response Format

<Tabs>
  <Tab title="Non-Streaming Response">
    ```json theme={null}
    {
      "id": "msg_xxx",
      "type": "message",
      "role": "assistant",
      "content": [
        {
          "type": "text",
          "text": "Response content..."
        }
      ],
      "model": "claude-opus-4-8",
      "stop_reason": "end_turn",
      "usage": {
        "input_tokens": 25,
        "output_tokens": 100,
        "cache_creation_input_tokens": 0,
        "cache_read_input_tokens": 0
      }
    }
    ```
  </Tab>

  <Tab title="Streaming Response">
    ```text theme={null}
    event: message_start
    data: {"type":"message_start","message":{"id":"msg_xxx","type":"message","role":"assistant","content":[],"model":"claude-opus-4-8"}}

    event: content_block_start
    data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}

    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Response"}}

    event: content_block_delta
    data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" content"}}

    event: content_block_stop
    data: {"type":"content_block_stop","index":0}

    event: message_delta
    data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":100}}

    event: message_stop
    data: {"type":"message_stop"}
    ```
  </Tab>
</Tabs>

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST \
    --url https://api.mixroute.ai/v1/messages \
    --header 'Authorization: Bearer sk-xxxxxxxxxx' \
    --header 'Content-Type: application/json' \
    --header 'anthropic-version: 2023-06-01' \
    --data '{
      "model": "claude-opus-4-8",
      "max_tokens": 1024,
      "messages": [
        {"role": "user", "content": "Briefly explain artificial intelligence"}
      ]
    }'
  ```

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

  client = Anthropic(
      api_key="sk-xxxxxxxxxx",
      base_url="https://api.mixroute.ai"
  )

  message = client.messages.create(
      model="claude-opus-4-8",
      max_tokens=1024,
      messages=[
          {"role": "user", "content": "Briefly explain artificial intelligence"}
      ]
  )
  print(message.content[0].text)
  ```

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

  const client = new Anthropic({
    apiKey: 'sk-xxxxxxxxxx',
    baseURL: 'https://api.mixroute.ai'
  });

  const message = await client.messages.create({
    model: 'claude-opus-4-8',
    max_tokens: 1024,
    messages: [
      { role: 'user', content: 'Briefly explain artificial intelligence' }
    ]
  });
  console.log(message.content[0].text);
  ```

  ```php PHP theme={null}
  <?php
  $client = new GuzzleHttp\Client();
  $response = $client->post('https://api.mixroute.ai/v1/messages', [
      'headers' => [
          'Authorization' => 'Bearer sk-xxxxxxxxxx',
          'Content-Type' => 'application/json',
          'anthropic-version' => '2023-06-01',
      ],
      'json' => [
          'model' => 'claude-opus-4-8',
          'max_tokens' => 1024,
          'messages' => [
              ['role' => 'user', 'content' => 'Briefly explain artificial intelligence']
          ]
      ]
  ]);
  echo $response->getBody();
  ```

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

  import (
      "bytes"
      "encoding/json"
      "net/http"
  )

  func main() {
      payload := map[string]interface{}{
          "model": "claude-opus-4-8",
          "max_tokens": 1024,
          "messages": []map[string]string{
              {"role": "user", "content": "Briefly explain artificial intelligence"},
          },
      }
      body, _ := json.Marshal(payload)
      req, _ := http.NewRequest("POST", "https://api.mixroute.ai/v1/messages", bytes.NewBuffer(body))
      req.Header.Set("Authorization", "Bearer sk-xxxxxxxxxx")
      req.Header.Set("Content-Type", "application/json")
      req.Header.Set("anthropic-version", "2023-06-01")
      http.DefaultClient.Do(req)
  }
  ```

  ```java Java theme={null}
  import java.net.http.*;
  import java.net.URI;

  HttpClient client = HttpClient.newHttpClient();
  String json = """
      {
          "model": "claude-opus-4-8",
          "max_tokens": 1024,
          "messages": [{"role": "user", "content": "Briefly explain artificial intelligence"}]
      }
      """;
  HttpRequest request = HttpRequest.newBuilder()
      .uri(URI.create("https://api.mixroute.ai/v1/messages"))
      .header("Authorization", "Bearer sk-xxxxxxxxxx")
      .header("Content-Type", "application/json")
      .header("anthropic-version", "2023-06-01")
      .POST(HttpRequest.BodyPublishers.ofString(json))
      .build();
  HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
  System.out.println(response.body());
  ```

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

  uri = URI('https://api.mixroute.ai/v1/messages')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri)
  request['Authorization'] = 'Bearer sk-xxxxxxxxxx'
  request['Content-Type'] = 'application/json'
  request['anthropic-version'] = '2023-06-01'
  request.body = {
    model: 'claude-opus-4-8',
    max_tokens: 1024,
    messages: [{ role: 'user', content: 'Briefly explain artificial intelligence' }]
  }.to_json

  response = http.request(request)
  puts response.body
  ```
</RequestExample>

<ResponseExample>
  ```json Response theme={null}
  {
    "id": "msg_xxx",
    "type": "message",
    "role": "assistant",
    "content": [
      {
        "type": "text",
        "text": "Artificial intelligence is a new technical science that researches and develops theories, methods, technologies, and application systems for simulating, extending, and expanding human intelligence..."
      }
    ],
    "model": "claude-opus-4-8",
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {
      "input_tokens": 25,
      "output_tokens": 100
    }
  }
  ```
</ResponseExample>
