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

# Messages

> Anthropic-compatible messages endpoint. Works with the Anthropic SDK, Claude Code, and any Anthropic-compatible client.

## Request

```bash theme={null}
POST https://modelslab.com/api/v7/llm/v1/messages
```

Pass your API key in the `x-api-key` header or as a Bearer token.

```bash theme={null}
curl -X POST https://modelslab.com/api/v7/llm/v1/messages \
  -H "x-api-key: $MODELSLAB_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "Qwen/Qwen2.5-VL-72B-Instruct-together",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "What is the capital of France?"}
    ]
  }'
```

## Body

```json theme={null}
{
  "model": "Qwen/Qwen2.5-VL-72B-Instruct-together",
  "max_tokens": 1024,
  "messages": [
    {"role": "user", "content": "What is the capital of France?"}
  ],
  "system": "You are a helpful assistant.",
  "temperature": 0.7,
  "top_p": 1,
  "stream": false
}
```

## Response

```json theme={null}
{
  "id": "msg_abc123",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "The capital of France is Paris."
    }
  ],
  "model": "Qwen/Qwen2.5-VL-72B-Instruct-together",
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 15,
    "output_tokens": 8
  }
}
```

## Streaming

Set `"stream": true` to receive Server-Sent Events:

```bash theme={null}
curl -X POST https://modelslab.com/api/v7/llm/v1/messages \
  -H "x-api-key: $MODELSLAB_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "Qwen/Qwen2.5-VL-72B-Instruct-together",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Write a haiku"}],
    "stream": true
  }'
```

## Anthropic SDK

This endpoint is fully compatible with the Anthropic SDK. Just change the `base_url` and `api_key`:

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

    client = Anthropic(
        api_key="YOUR_MODELSLAB_API_KEY",
        base_url="https://modelslab.com/api/v7/llm",
    )

    # Non-streaming
    message = client.messages.create(
        model="Qwen/Qwen2.5-VL-72B-Instruct-together",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "Explain quantum computing"}
        ],
    )
    print(message.content[0].text)

    # Streaming
    with client.messages.stream(
        model="Qwen/Qwen2.5-VL-72B-Instruct-together",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Write a story"}],
    ) as stream:
        for text in stream.text_stream:
            print(text, end="")
    ```
  </Tab>

  <Tab title="Node.js">
    ```javascript theme={null}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      apiKey: 'YOUR_MODELSLAB_API_KEY',
      baseURL: 'https://modelslab.com/api/v7/llm',
    });

    const message = await client.messages.create({
      model: 'Qwen/Qwen2.5-VL-72B-Instruct-together',
      max_tokens: 1024,
      messages: [
        { role: 'user', content: 'Hello!' },
      ],
    });

    console.log(message.content[0].text);
    ```
  </Tab>
</Tabs>

## Using with Claude Code

You can use ModelsLab's LLM API as a backend for [Claude Code](https://claude.ai/claude-code), Anthropic's CLI coding assistant:

```bash theme={null}
ANTHROPIC_BASE_URL="https://modelslab.com/api/v7/llm" \
ANTHROPIC_AUTH_TOKEN="YOUR_MODELSLAB_API_KEY" \
claude --model "Qwen/Qwen2.5-VL-72B-Instruct-together"
```

This lets you use any of ModelsLab's 200+ LLM models as the backend for Claude Code's agentic coding capabilities.


## OpenAPI

````yaml POST /v1/messages
openapi: 3.1.0
info:
  title: ModelsLab LLM API
  description: >-
    Unified LLM API with OpenAI and Anthropic SDK compatibility. Access 200+
    language models through a single API.
  version: 7.0.0
servers: []
security: []
paths:
  /v1/messages:
    post:
      summary: Messages
      description: >-
        Anthropic-compatible messages endpoint. Create messages and receive
        model responses in the Anthropic format.
      parameters:
        - name: anthropic-version
          in: header
          required: false
          schema:
            type: string
            default: '2023-06-01'
          description: Anthropic API version
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MessagesRequest'
      responses:
        '200':
          description: Message response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MessagesResponse'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
      security:
        - apiKeyAuth: []
      servers:
        - url: https://modelslab.com/api/v7/llm
components:
  schemas:
    MessagesRequest:
      type: object
      required:
        - model
        - max_tokens
        - messages
      properties:
        model:
          type: string
          description: Model ID to use
        max_tokens:
          type: integer
          minimum: 1
          description: Maximum number of tokens to generate
        messages:
          type: array
          description: Array of input messages
          items:
            $ref: '#/components/schemas/AnthropicMessage'
        system:
          type: string
          description: System prompt
        temperature:
          type: number
          minimum: 0
          maximum: 1
          default: 1
          description: Sampling temperature
        top_p:
          type: number
          minimum: 0
          maximum: 1
          description: Nucleus sampling parameter
        stream:
          type: boolean
          default: false
          description: Whether to stream the response
    MessagesResponse:
      type: object
      properties:
        id:
          type: string
          description: Unique message ID
        type:
          type: string
          enum:
            - message
        role:
          type: string
          enum:
            - assistant
        content:
          type: array
          items:
            type: object
            properties:
              type:
                type: string
                enum:
                  - text
              text:
                type: string
        model:
          type: string
        stop_reason:
          type: string
          enum:
            - end_turn
            - max_tokens
            - stop_sequence
        usage:
          $ref: '#/components/schemas/AnthropicUsage'
    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
            type:
              type: string
            code:
              type: string
    AnthropicMessage:
      type: object
      required:
        - role
        - content
      properties:
        role:
          type: string
          enum:
            - user
            - assistant
          description: Role of the message sender
        content:
          type: string
          description: Content of the message
    AnthropicUsage:
      type: object
      properties:
        input_tokens:
          type: integer
        output_tokens:
          type: integer
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key
      description: API key authentication via x-api-key header

````