Langsung ke konten utama

Gambaran Umum

AI Sonar mendukung path native Anthropic Messages API, sehingga Anda dapat menggunakan Anthropic SDK resmi secara langsung untuk model Claude.
Untuk Anthropic SDK, gunakan https://api.aisonar.dev sebagai base URL, tanpa menambahkan /v1 sendiri.
Di antara rute SDK yang didokumentasikan, ini adalah salah satu jalur AI Sonar yang paling didukung untuk fitur native Claude.
Jenis: SDK nativeJalur utama: Anthropic-nativeTingkat dukungan: Jalur native yang kuat

Instalasi

pip install anthropic

Konfigurasikan Client

from anthropic import Anthropic

client = Anthropic(
    api_key="sk-your-api-key",
    base_url="https://api.aisonar.dev",
)

Penggunaan Dasar

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain AI Sonar in one sentence."}
    ]
)

print(message.content[0].text)

Streaming

with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a short poem about coding."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Vision

import base64

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What's in this image?"},
            {
                "type": "image",
                "source": {
                    "type": "url",
                    "url": "https://example.com/image.jpg"
                }
            }
        ]
    }]
)

with open("image.png", "rb") as f:
    image_data = base64.b64encode(f.read()).decode()

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "Describe this image"},
            {
                "type": "image",
                "source": {
                    "type": "base64",
                    "media_type": "image/png",
                    "data": image_data
                }
            }
        ]
    }]
)

Penggunaan Tool

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=[{
        "name": "get_weather",
        "description": "Get the weather for a location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            },
            "required": ["location"]
        }
    }],
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)

for block in message.content:
    if block.type == "tool_use":
        print(block.name)
        print(block.input)

Pemikiran Mendalam

message = client.messages.create(
    model="claude-opus-4-6",
    max_tokens=16000,
    thinking={
        "type": "enabled",
        "budget_tokens": 10000
    },
    messages=[{"role": "user", "content": "Solve this complex problem step by step."}]
)

for block in message.content:
    if block.type == "thinking":
        print(block.thinking)
    elif block.type == "text":
        print(block.text)

Model Claude yang Direkomendasikan

ModelPaling Cocok Untuk
claude-opus-4-6Penalaran mendalam, analisis panjang
claude-sonnet-4-6Coding, tugas assistant umum
claude-haiku-4-5Respons cepat dan ringan

Pemecahan Masalah

  • Gunakan https://api.aisonar.dev
  • Jangan tambahkan /v1 secara manual saat mengonfigurasi Anthropic SDK
  • Periksa bahwa AI Sonar API key Anda diawali dengan sk-
  • Konfirmasikan bahwa key tersebut aktif di dashboard AI Sonar
  • Biarkan Anthropic SDK mengelola auth header alih-alih menambahkan custom headers secara manual
  • Verifikasi nama model Claude secara tepat
  • Periksa ketersediaan saat ini di katalog model AI Sonar