# Chat Completion (OpenAI Protocol)

## 1. Overview

Claude is a large language model developed by Anthropic, with powerful conversational and writing capabilities. It can understand context, generate coherent text, write code, and excels at logical reasoning and analysis. It prioritizes safety and ethical guidelines and clearly identifies itself as an AI assistant. It supports multilingual communication and can handle complex tasks and long conversations.

{% hint style="success" %}
This API is compatible with the OpenAI interface format.
{% endhint %}

**Model List:**

* `claude-sonnet-4-20250514`
* `claude-sonnet-4-5-20250929`
* `claude-haiku-4-5-20251001`
* `claude-opus-4-5-20251101`
* `claude-opus-4-6`
* `claude-sonnet-4-6`
* `claude-opus-4-7`

## 2. Request Description

* **Request Method**: `POST`
* **Request URL**:

  > `https://gateway.theturbo.ai/v1/chat/completions`

{% hint style="info" %}
To ensure concurrent resource availability, the backend uses multi-account load balancing. To improve cache hit rates in multi-turn conversation mode, include the HTTP request header `X-Conversation-Id` with a random string in your request. The platform will preferentially route requests to the same backend account. [Reference Documentation](/api-reference/compute/aig/gateway-features/cache-optimization.md)
{% endhint %}

***

## 3. Request Parameters

### 3.1 Header Parameters

| Parameter Name  | Type   | Required | Description                                                          | Example                |
| --------------- | ------ | -------- | -------------------------------------------------------------------- | ---------------------- |
| `Content-Type`  | string | Yes      | Sets the request header type, must be `application/json`             | `application/json`     |
| `Accept`        | string | Yes      | Sets the response type, recommended to use `application/json`        | `application/json`     |
| `Authorization` | string | Yes      | API\_KEY required for authentication, format: `Bearer $YOUR_API_KEY` | `Bearer $YOUR_API_KEY` |

***

### 3.2 Body Parameters (application/json)

| Parameter Name          | Type    | Required | Description                                                                                                                                                                                                                                                                                                                                                         | Example                                 |
| ----------------------- | ------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------- |
| **model**               | string  | Yes      | The model ID to use. See the available versions listed in [Overview](#id-1.-overview), e.g. `claude-haiku-4-5-20251001`.                                                                                                                                                                                                                                            | `claude-haiku-4-5-20251001`             |
| **messages**            | array   | Yes      | List of chat messages, compatible with OpenAI format. Each object in the array contains `role` and `content`.                                                                                                                                                                                                                                                       | `[{"role": "user","content": "Hello"}]` |
| role                    | string  | No       | Message role, possible values: `system`, `user`, `assistant`.                                                                                                                                                                                                                                                                                                       | `user`                                  |
| content                 | string  | No       | The specific content of the message.                                                                                                                                                                                                                                                                                                                                | `Hello, tell me a joke.`                |
| temperature             | number  | No       | Sampling temperature, value range `0～2`. Higher values produce more random output; lower values produce more focused and deterministic output.                                                                                                                                                                                                                      | `0.7`                                   |
| top\_p                  | number  | No       | Another way to control the sampling distribution, value range `0～1`. Typically used as an alternative to `temperature`.                                                                                                                                                                                                                                             | `0.9`                                   |
| stream                  | boolean | No       | Whether to enable streaming output. When set to `true`, returns streaming data similar to ChatGPT.                                                                                                                                                                                                                                                                  | `false`                                 |
| max\_tokens             | number  | No       | Maximum number of tokens that can be generated in a single response, limited by the model's context length.                                                                                                                                                                                                                                                         | `8192`                                  |
| reasoning\_effort       | string  | No       | Controls how much "thinking effort" the model invests in reasoning tasks. Supports `low`, `medium`, `high`, `none`. Default is `none`. The platform converts this into the `thinking` and `output_config.effort` parameters when forwarding upstream. If the request already includes `thinking` and `output_config`, the user-supplied parameters take precedence. | `none`                                  |
| thinking                | struct  | No       | Configuration to enable Claude's extended thinking.                                                                                                                                                                                                                                                                                                                 | `{"type":"adaptive"}`                   |
| thinking.type           | string  | Yes      | Thinking type. Supports `enabled`, `disabled`, `adaptive`. Versions 4.7 and above support only `adaptive` and `disabled`; versions 4.5 and below support only `enabled` and `disabled`.                                                                                                                                                                             | `adaptive`                              |
| thinking.budget\_tokens | number  | No       | Required when `type` is set to `enabled`. Determines how many tokens Claude may use during its internal reasoning process. A larger budget allows Claude to perform deeper analysis on complex problems, improving answer quality. The value must be greater than or equal to `1024` and less than `max_tokens`.                                                    | `1024`                                  |
| thinking.display        | string  | No       | When set to `summarized`, the thinking content is returned normally. When set to `omitted`, the thinking content is hidden but a signature is returned to support multi-turn conversation continuity. Versions 4.7 and above typically default to `omitted`.                                                                                                        | `omitted`                               |
| output\_config.effort   | string  | No       | Controls how much "computational effort" the model invests in reasoning tasks. Supports `low`, `medium`, `high`, `xhigh`, `max`.                                                                                                                                                                                                                                    | `medium`                                |

***

## 4. Request Examples

### 4.1 Chat Conversation

{% tabs %}
{% tab title="HTTP" %}

```http
POST /v1/chat/completions
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
	"model": "claude-haiku-4-5-20251001",
	"messages": [
		{
			"role": "user",
			"content": "Hello, please give me an introduction to quantum mechanics"
		}
	]
}
```

{% endtab %}

{% tab title="Shell" %}

```sh
curl https://gateway.theturbo.ai/v1/chat/completions \
	-H "Content-Type: application/json" \
	-H "Accept: application/json" \
	-H "Authorization: Bearer $YOUR_API_KEY" \
	-d "{
	\"model\": \"claude-haiku-4-5-20251001\",
	\"messages\": [{
		\"role\": \"user\",
		\"content\": \"Hello, please give me an introduction to quantum mechanics\"
	}]
}"
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"context"
	"fmt"

	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {
	apiKey := "sk-123456789012345678901234567890123456789012345678"

	client := openai.NewClient(
		option.WithAPIKey(apiKey),
		option.WithBaseURL("https://gateway.theturbo.ai/v1"),
	)

	resp, err := client.Chat.Completions.New(
		context.Background(),
		openai.ChatCompletionNewParams{
			Model: "claude-haiku-4-5-20251001",
			Messages: []openai.ChatCompletionMessageParamUnion{
				openai.UserMessage("Hello, please give me an introduction to quantum mechanics"),
			},
		},
	)

	if err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println(resp.Choices[0].Message.Content)
}

```

{% endtab %}

{% tab title="Python" %}

```python
#!/usr/bin/env python3

from openai import OpenAI

def main():
    api_key = "sk-123456789012345678901234567890123456789012345678"

    client = OpenAI(
        api_key=api_key,
        base_url="https://gateway.theturbo.ai/v1"
    )

    response = client.chat.completions.create(
        model="claude-haiku-4-5-20251001",
        messages=[
            {"role": "user", "content": "Hello, please give me an introduction to quantum mechanics"}
        ]
    )

    print(response.choices[0].message.content)

if __name__ == "__main__":
    main()

```

{% endtab %}
{% endtabs %}

### 4.2 Image Understanding

Multimodal image understanding feature — submit images to the model by uploading an image file or providing an image URL.

<details>

<summary>Supported Image Types</summary>

`image/png` `image/jpeg` `image/webp` `image/gif`

</details>

{% tabs %}
{% tab title="HTTP" %}

```http
POST /v1/chat/completions
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
	"model": "claude-haiku-4-5-20251001",
	"messages": [
		{
			"role": "user",
			"content": [
				{
					"type": "text",
					"text": "What is in this image?"
				},
				{
					"type": "image_url",
					"image_url": {
						"url": "data:image/jpeg;base64,${base64_image}"
					}
				}
			]
		}
	]
}
```

{% endtab %}

{% tab title="Shell" %}

```sh
base64_image=$(base64 -i "Path/to/agi/image.jpeg");
curl https://gateway.theturbo.ai/v1/chat/completions \
	-H "Content-Type: application/json" \
	-H "Accept: application/json" \
	-H "Authorization: Bearer $YOUR_API_KEY" \
	-d "{
	\"model\": \"claude-haiku-4-5-20251001\",
	\"messages\": [{
		\"role\": \"user\",
		\"content\": [{
				\"type\": \"text\",
				\"text\": \"What is in this image?\"
			},
			{
				\"type\": \"image_url\",
				\"image_url\": {
					\"url\": \"data:image/jpeg;base64,${base64_image}\"
				}
			}
		]
	}]
}"
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"context"
	"encoding/base64"
	"fmt"
	"os"

	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {
	apiKey := "sk-123456789012345678901234567890123456789012345678"

	client := openai.NewClient(
		option.WithAPIKey(apiKey),
		option.WithBaseURL("https://gateway.theturbo.ai/v1"),
	)

	imagePath := "Path/to/agi/image.jpeg"
	imageBytes, err := os.ReadFile(imagePath)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	base64Image := base64.StdEncoding.EncodeToString(imageBytes)
	imageURL := "data:image/jpeg;base64," + base64Image

	resp, err := client.Chat.Completions.New(
		context.Background(),
		openai.ChatCompletionNewParams{
			Model: "claude-haiku-4-5-20251001",
			Messages: []openai.ChatCompletionMessageParamUnion{
				openai.UserMessage([]openai.ChatCompletionContentPartUnionParam{
					openai.TextContentPart("What is in this image?"),
					openai.ImageContentPart(openai.ChatCompletionContentPartImageImageURLParam{
						URL: imageURL,
					}),
				}),
			},
		},
	)

	if err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println(resp.Choices[0].Message.Content)
}

```

{% endtab %}

{% tab title="Python" %}

```python
#!/usr/bin/env python3

import base64
from openai import OpenAI

def main():
    api_key = "sk-123456789012345678901234567890123456789012345678"

    client = OpenAI(
        api_key=api_key,
        base_url="https://gateway.theturbo.ai/v1"
    )

    image_path = "Path/to/agi/image.jpeg"
    with open(image_path, "rb") as f:
        image_bytes = f.read()

    base64_image = base64.b64encode(image_bytes).decode("utf-8")
    image_url = f"data:image/jpeg;base64,{base64_image}"

    response = client.chat.completions.create(
        model="claude-haiku-4-5-20251001",
        messages=[{
            "role": "user",
            "content": [
                {"type": "text", "text": "What is in this image?"},
                {"type": "image_url", "image_url": {"url": image_url}}
            ]
        }]
    )

    print(response.choices[0].message.content)

if __name__ == "__main__":
    main()

```

{% endtab %}
{% endtabs %}

### 4.3 Function Calling

{% tabs %}
{% tab title="HTTP" %}

```http
POST /v1/chat/completions
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
	"model": "claude-haiku-4-5-20251001",
	"messages": [{
		"role": "user",
		"content": "What's the weather like in Boston today?"
	}],
	"tools": [{
		"type": "function",
		"function": {
			"name": "get_current_weather",
			"description": "Get the current weather in a given location",
			"parameters": {
				"type": "object",
				"properties": {
					"location": {
						"type": "string",
						"description": "The city and state, e.g. San Francisco, CA"
					},
					"unit": {
						"type": "string",
						"enum": ["celsius", "fahrenheit"]
					}
				},
				"required": ["location"]
			}
		}
	}],
	"tool_choice": "auto"
}
```

{% endtab %}

{% tab title="Shell" %}

```sh
curl https://gateway.theturbo.ai/v1/chat/completions \
	-H "Content-Type: application/json" \
	-H "Accept: application/json" \
	-H "Authorization: Bearer $YOUR_API_KEY" \
	-d "{
	\"model\": \"claude-haiku-4-5-20251001\",
	\"messages\": [{
		\"role\": \"user\",
		\"content\": \"What's the weather like in Boston today?\"
	}],
	\"tools\": [{
		\"type\": \"function\",
		\"function\": {
			\"name\": \"get_current_weather\",
			\"description\": \"Get the current weather in a given location\",
			\"parameters\": {
				\"type\": \"object\",
				\"properties\": {
					\"location\": {
						\"type\": \"string\",
						\"description\": \"The city and state, e.g. San Francisco, CA\"
					},
					\"unit\": {
						\"type\": \"string\",
						\"enum\": [\"celsius\", \"fahrenheit\"]
					}
				},
				\"required\": [\"location\"]
			}
		}
	}],
	\"tool_choice\": \"auto\"
}"
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
	"github.com/openai/openai-go/packages/param"
	"github.com/openai/openai-go/shared"
)

func main() {
	apiKey := "sk-123456789012345678901234567890123456789012345678"

	client := openai.NewClient(
		option.WithAPIKey(apiKey),
		option.WithBaseURL("https://gateway.theturbo.ai/v1"),
	)

	tools := []openai.ChatCompletionToolParam{
		{
			Type: "function",
			Function: shared.FunctionDefinitionParam{
				Name:        "get_current_weather",
				Description: param.NewOpt("Get the current weather in a given location"),
				Parameters: shared.FunctionParameters{
					"type": "object",
					"properties": map[string]interface{}{
						"location": map[string]interface{}{
							"type":        "string",
							"description": "The city and state, e.g. San Francisco, CA",
						},
						"unit": map[string]interface{}{
							"type": "string",
							"enum": []string{"celsius", "fahrenheit"},
						},
					},
					"required": []string{"location"},
				},
			},
		},
	}

	resp, err := client.Chat.Completions.New(
		context.Background(),
		openai.ChatCompletionNewParams{
			Model: "claude-haiku-4-5-20251001",
			Messages: []openai.ChatCompletionMessageParamUnion{
				openai.UserMessage("What's the weather like in Boston today?"),
			},
			Tools: tools,
			ToolChoice: openai.ChatCompletionToolChoiceOptionUnionParam{
				OfAuto: param.NewOpt("auto"),
			},
		},
	)

	if err != nil {
		fmt.Println("error:", err)
		return
	}

	msg := resp.Choices[0].Message

	if msg.ToolCalls != nil && len(msg.ToolCalls) > 0 {
		for _, call := range msg.ToolCalls {
			fmt.Println("🔧 Function called:", call.Function.Name)
			fmt.Println("📥 Arguments JSON:", call.Function.Arguments)

			// Parse the arguments
			var args map[string]any
			_ = json.Unmarshal([]byte(call.Function.Arguments), &args)
			fmt.Println("📦 Parsed args:", args)
		}
	} else {
		fmt.Println("💬 Assistant reply:", msg.Content)
	}
}

```

{% endtab %}

{% tab title="Python" %}

```python
#!/usr/bin/env python3

import json
from openai import OpenAI

def main():
    api_key = "sk-123456789012345678901234567890123456789012345678"

    client = OpenAI(
        api_key=api_key,
        base_url="https://gateway.theturbo.ai/v1"
    )

    tools = [{
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"]
                    }
                },
                "required": ["location"]
            }
        }
    }]

    response = client.chat.completions.create(
        model="claude-haiku-4-5-20251001",
        messages=[
            {"role": "user", "content": "What's the weather like in Boston today?"}
        ],
        tools=tools,
        tool_choice="auto"
    )

    msg = response.choices[0].message

    if msg.tool_calls:
        for call in msg.tool_calls:
            print(f"🔧 Function called: {call.function.name}")
            print(f"📥 Arguments JSON: {call.function.arguments}")

            args = json.loads(call.function.arguments)
            print(f"📦 Parsed args: {args}")
    else:
        print(f"💬 Assistant reply: {msg.content}")

if __name__ == "__main__":
    main()

```

{% endtab %}
{% endtabs %}

### 4.4 Multi-turn Conversation

{% tabs %}
{% tab title="HTTP" %}

```http
POST /v1/chat/completions
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
    "model": "claude-haiku-4-5-20251001",
    "reasoning_effort": "high",
    "messages": [
        {
            "role": "user",
            "content": "Hello, my name is z"
        },
        {
            "role": "assistant",
            "content": "Hello, z! Nice to meet you. 👋\n\nIs there anything I can help you with?",
            "reasoning_content": "The user introduced that their name is \"z\". This is a simple self-introduction. I should respond in a friendly and polite manner, and be ready to help them.",
            "reasoning_details": [
                {
                    "signature": "Er4CC...CaVGAE="
                }
            ]
        },
        {
            "role": "user",
            "content": "What is my name"
        }
    ]
}
```

{% endtab %}

{% tab title="Shell" %}

```sh
curl https://gateway.theturbo.ai/v1/chat/completions \
	-H "Content-Type: application/json" \
	-H "Accept: application/json" \
	-H "Authorization: Bearer $YOUR_API_KEY" \
	-d "{
    \"model\": \"claude-haiku-4-5-20251001\",
    \"reasoning_effort\": \"high\",
    \"messages\": [
        {
            \"role\": \"user\",
            \"content\": \"Hello, my name is z\"
        },
        {
            \"role\": \"assistant\",
            \"content\": \"Hello, z! Nice to meet you. 👋\\n\\nIs there anything I can help you with?\",
            \"reasoning_content\": \"The user introduced that their name is \\\"z\\\". This is a simple self-introduction. I should respond in a friendly and polite manner, and be ready to help them.\",
            \"reasoning_details\": [
                {
                    \"signature\": \"Er4CC...CaVGAE=\"
                }
            ]
        },
        {
            \"role\": \"user\",
            \"content\": \"What is my name\"
        }
    ]
}"
```

{% endtab %}
{% endtabs %}

## 5. Response Example

```json
{
	"id": "chatcmpl-1234567890",
	"object": "chat.completion",
	"created": 1699999999,
	"model": "claude-haiku-4-5-20251001",
	"choices": [
		{
			"message": {
				"role": "assistant",
				"content": "Quantum mechanics is a branch of physics that studies the microscopic world..."
			},
			"finish_reason": "stop"
		}
	],
	"usage": {
		"prompt_tokens": 10,
		"completion_tokens": 30,
		"total_tokens": 40
	}
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.console.zenlayer.com/api-reference/compute/aig/chat-completion/anthropic-claude/anthropic-claude-chat-completion.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
