Chat Completion (Anthropic native API)¶
1. Overview¶
Claude is Anthropic's large language model family, known for strong conversational, writing, coding, and reasoning capabilities. It handles long-context tasks well and is designed with a strong emphasis on safety.
2. Request¶
- Method:
POST -
Endpoint:
https://gateway.serevixai.ai/v1/messages
3. Parameters¶
3.1 Header Parameters¶
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
Content-Type |
string | Yes | Sets the request content type. It must be application/json |
application/json |
Accept |
string | Yes | Sets the response content type. The recommended value is application/json |
application/json |
x-api-key |
string | Yes | API key required for authentication, in the format $YOUR_API_KEY. |
$YOUR_API_KEY |
3.2 Body Parameters (application/json)¶
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
| model | string | Yes | The model ID to use. See Model List for available versions, such as claude-3-5-haiku-20241022. |
claude-haiku-4-5-20251001 |
| messages | array | Yes | A chat message list in the Anthropic native format. Each item contains role and content. |
[{"role": "user","content": [{"type":"text","text":"Hello, tell me a joke."}]}] |
| message.role | string | No | Message role. Supported values: user and assistant. |
user |
| message.content | array | No | The message content. | [{"type":"text","text":"Hello, tell me a joke."}] |
| system | array | No | System prompt content. | [{"type":"text","text":"You are a friendly AI assistant"}] |
| temperature | number | No | Sampling temperature in the range 0-2. Higher values make the output more random, while lower values make it more focused and deterministic. |
0.7 |
| top_p | number | No | Another way to control the sampling distribution, in the range 0-1. It is usually used instead of temperature. |
0.9 |
| stream | boolean | No | Whether to enable streaming output. When set to true, the API returns ChatGPT-style streamed data. |
false |
| max_tokens | number | No | The maximum number of tokens that can be generated in a single reply, subject to the model context window. | 8192 |
4. Request Examples¶
4.1 Chat¶
POST /v1/messages
Content-Type: application/json
Accept: application/json
x-api-key: $YOUR_API_KEY
{
"model": "claude-haiku-4-5-20251001",
"max_tokens": 4096,
"system": [{
"type": "text",
"text": "You are a friendly AI assistant"
}],
"messages": [{
"role": "user",
"content": [{
"type": "text",
"text": "Hello, can you explain quantum mechanics to me?"
}]
}]
}
curl https://gateway.serevixai.ai/v1/messages \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "x-api-key: $YOUR_API_KEY" \
-d "{
\"model\": \"claude-haiku-4-5-20251001\",
\"max_tokens\": 4096,
\"system\": [{
\"type\": \"text\",
\"text\": \"You are a friendly AI assistant\"
}],
\"messages\": [{
\"role\": \"user\",
\"content\": [{
\"type\": \"text\",
\"text\": \"Hello, can you explain quantum mechanics to me?\"
}]
}]
}"
package main
import (
"context"
"fmt"
"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
)
func main() {
apiKey := "sk-123456789012345678901234567890123456789012345678"
client := anthropic.NewClient(
option.WithAPIKey(apiKey),
option.WithBaseURL("https://gateway.serevixai.ai"),
)
resp, err := client.Messages.New(
context.Background(),
anthropic.MessageNewParams{
Model: "claude-haiku-4-5-20251001",
MaxTokens: 4096,
System: []anthropic.TextBlockParam{
{
Type: "text",
Text: "You are a friendly AI assistant",
},
},
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Hello, can you explain quantum mechanics to me?")),
},
},
)
if err != nil {
fmt.Println("error:", err)
return
}
for _, block := range resp.Content {
if block.Type == "text" {
fmt.Println("π¬ Assistant reply:")
fmt.Println(block.Text)
}
}
fmt.Println("\nπ Token usage:")
fmt.Printf(" - Input tokens: %d\n", resp.Usage.InputTokens)
fmt.Printf(" - Output tokens: %d\n", resp.Usage.OutputTokens)
}
#!/usr/bin/env python3
from anthropic import Anthropic
def main():
api_key = "sk-123456789012345678901234567890123456789012345678"
client = Anthropic(
api_key=api_key,
base_url="https://gateway.serevixai.ai"
)
response = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=4096,
system=[{
"type": "text",
"text": "You are a friendly AI assistant"
}],
messages=[{
"role": "user",
"content": "Hello, can you explain quantum mechanics to me?"
}]
)
for block in response.content:
if block.type == "text":
print("π¬ Assistant reply:")
print(block.text)
print("\nπ Token usage:")
print(f" - Input tokens: {response.usage.input_tokens}")
print(f" - Output tokens: {response.usage.output_tokens}")
if __name__ == "__main__":
main()
5. Response Example¶
{
"model": "claude-3-5-haiku-20241022",
"id": "msg_bdrk_01AZpXbu4crT5R6gsYJwk6KD",
"type": "message",
"role": "assistant",
"content": [{
"type": "text",
"text": "Quantum mechanics is the branch of physics that studies the microscopic world..."
}],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 36,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"output_tokens": 366
}
}