Ringkasan
Endpoint ini menyediakan kompatibilitas native dengan Anthropic Messages API. Gunakan ini untuk model Claude dengan fitur seperti extended thinking.
Endpoint ini tetap mengikuti kontrak native Anthropic. messages harus berupa array pesan user / assistant, system berada pada field top-level system, dan max_tokens wajib dikirim. Jika payload menggunakan role OpenAI seperti system, developer, atau tool di dalam messages, kirim ke /v1/chat/completions.
Base URL untuk Anthropic SDK: https://api.tokenlab.sh (tanpa sufiks /v1)
TokenLab API key Anda. Alternatif untuk token Bearer.
Versi Anthropic API. Gunakan 2023-06-01.
Body Permintaan
ID model Claude (misalnya, claude-sonnet-4-6 atau claude-opus-4-6).
Array objek message dengan role dan content.
Jumlah token maksimum yang akan dihasilkan.
Prompt sistem (terpisah dari array messages).
Temperature sampling (0-1).
Aktifkan respons streaming.
Konfigurasi extended thinking (Claude Opus 4.5).
type (string): "enabled" untuk mengaktifkan
budget_tokens (integer): Anggaran token untuk thinking
Tools yang tersedia untuk model.
Bagaimana model harus menggunakan tools. Opsi: auto, any, tool (tool tertentu).
Parameter nucleus sampling. Gunakan temperature atau top_p, bukan keduanya.
Hanya ambil sampel dari K opsi teratas untuk setiap token.
Urutan stop kustom yang akan menyebabkan model berhenti menghasilkan.
Metadata yang dilampirkan ke permintaan untuk tujuan pelacakan.
Respons
Pengidentifikasi message yang unik.
Array blok konten (text, thinking, tool_use).
Alasan generation berhenti (end_turn, max_tokens, tool_use).
Penggunaan token dengan input_tokens dan output_tokens.
cURL
Python
JavaScript
Go
PHP
curl -X POST "https://api.tokenlab.sh/v1/messages" \
-H "x-api-key: sk-your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"system": "You are a helpful assistant.",
"messages": [
{"role": "user", "content": "Hello, Claude!"}
]
}'
from anthropic import Anthropic
client = Anthropic(
api_key = "sk-your-api-key" ,
base_url = "https://api.tokenlab.sh"
)
message = client.messages.create(
model = "claude-sonnet-4-6" ,
max_tokens = 1024 ,
system = "You are a helpful assistant." ,
messages = [
{ "role" : "user" , "content" : "Hello, Claude!" }
]
)
print (message.content[ 0 ].text)
import Anthropic from '@anthropic-ai/sdk' ;
const client = new Anthropic ({
apiKey: 'sk-your-api-key' ,
baseURL: 'https://api.tokenlab.sh'
});
const message = await client . messages . create ({
model: 'claude-sonnet-4-6' ,
max_tokens: 1024 ,
system: 'You are a helpful assistant.' ,
messages: [
{ role: 'user' , content: 'Hello, Claude!' }
]
});
console . log ( message . content [ 0 ]. text );
package main
import (
" bytes "
" encoding/json "
" fmt "
" io "
" net/http "
)
func main () {
payload := map [ string ] interface {}{
"model" : "claude-sonnet-4-6" ,
"max_tokens" : 1024 ,
"system" : "You are a helpful assistant." ,
"messages" : [] map [ string ] string {
{ "role" : "user" , "content" : "Hello, Claude!" },
},
}
jsonData , _ := json . Marshal ( payload )
req , _ := http . NewRequest ( "POST" , "https://api.tokenlab.sh/v1/messages" , bytes . NewBuffer ( jsonData ))
req . Header . Set ( "x-api-key" , "sk-your-api-key" )
req . Header . Set ( "anthropic-version" , "2023-06-01" )
req . Header . Set ( "Content-Type" , "application/json" )
client := & http . Client {}
resp , _ := client . Do ( req )
defer resp . Body . Close ()
body , _ := io . ReadAll ( resp . Body )
fmt . Println ( string ( body ))
}
<? php
$payload = [
'model' => 'claude-sonnet-4-6' ,
'max_tokens' => 1024 ,
'system' => 'You are a helpful assistant.' ,
'messages' => [
[ 'role' => 'user' , 'content' => 'Hello, Claude!' ]
]
];
$ch = curl_init ( 'https://api.tokenlab.sh/v1/messages' );
curl_setopt_array ( $ch , [
CURLOPT_RETURNTRANSFER => true ,
CURLOPT_POST => true ,
CURLOPT_HTTPHEADER => [
'x-api-key: sk-your-api-key' ,
'anthropic-version: 2023-06-01' ,
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => json_encode ( $payload )
]);
$response = curl_exec ( $ch );
curl_close ( $ch );
$data = json_decode ( $response , true );
echo $data [ 'content' ][ 0 ][ 'text' ];
{
"id" : "msg_abc123" ,
"type" : "message" ,
"role" : "assistant" ,
"content" : [
{
"type" : "text" ,
"text" : "Hello! How can I help you today?"
}
],
"model" : "claude-sonnet-4-6" ,
"stop_reason" : "end_turn" ,
"usage" : {
"input_tokens" : 15 ,
"output_tokens" : 10
}
}
Untuk model Claude dengan dukungan visi, letakkan gambar di dalam messages[].content sebagai blok gambar terstruktur.
{
"model" : "claude-sonnet-4-6" ,
"max_tokens" : 1024 ,
"messages" : [
{
"role" : "user" ,
"content" : [
{
"type" : "text" ,
"text" : "Please describe this image."
},
{
"type" : "image" ,
"source" : {
"type" : "url" ,
"url" : "https://example.com/demo.jpg"
}
}
]
}
]
}
{
"model" : "claude-sonnet-4-6" ,
"max_tokens" : 1024 ,
"messages" : [
{
"role" : "user" ,
"content" : [
{
"type" : "text" ,
"text" : "Please describe this image."
},
{
"type" : "image" ,
"source" : {
"type" : "base64" ,
"media_type" : "image/jpeg" ,
"data" : "/9j/4AAQSkZJRgABAQ..."
}
}
]
}
]
}
Contoh Extended Thinking
message = client.messages.create(
model = "claude-opus-4-6" ,
max_tokens = 16000 ,
thinking = {
"type" : "enabled" ,
"budget_tokens" : 10000
},
messages = [{ "role" : "user" , "content" : "Solve this math problem..." }]
)
for block in message.content:
if block.type == "thinking" :
print ( f "Thinking: { block.thinking } " )
elif block.type == "text" :
print ( f "Response: { block.text } " )
Anthropic Batch Pesan
TokenLab kini juga menyediakan alur native Anthropic Message Batches di samping /v1/messages.
Rute yang tersedia:
POST /v1/messages/batches
GET /v1/messages/batches
GET /v1/messages/batches/:message_batch_id
GET /v1/messages/batches/:message_batch_id/results
POST /v1/messages/batches/:message_batch_id/cancel
DELETE /v1/messages/batches/:message_batch_id
Catatan operasional:
Gunakan API key TokenLab yang sama bersama header native Anthropic.
Jika item batch mereferensikan file_id, sertakan juga anthropic-beta: files-api-2025-04-14.
Batch job mempertahankan bentuk request/response native Anthropic, sementara TokenLab melacak status billing untuk rekonsiliasi.