TokenLab

네이티브 SDK

Anthropic SDK

Claude-native Messages API 호환성을 위해 Anthropic SDK와 함께 TokenLab를 사용하세요

개요

TokenLab는 네이티브 Anthropic Messages API 경로를 지원하므로, Claude 모델에 대해 공식 Anthropic SDK를 직접 사용할 수 있습니다.

Anthropic SDK의 경우 base URL로 https://api.tokenlab.sh를 사용하고, /v1를 직접 추가하지 마세요.

문서화된 SDK 경로 가운데서도 Claude-native 기능에 대해 특히 잘 지원되는 TokenLab 경로 중 하나입니다.

설치

pip install anthropic

Client 구성

from anthropic import Anthropic

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

기본 사용법

message = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Reply with: Connected to TokenLab."}
    ]
)

print(message.content[0].text)

스트리밍

with client.messages.stream(
    model="claude-sonnet-5",
    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)

비전

import base64

message = client.messages.create(
    model="claude-sonnet-5",
    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-5",
    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
                }
            }
        ]
    }]
)

Tool 사용

message = client.messages.create(
    model="claude-sonnet-5",
    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)

확장된 사고

message = client.messages.create(
    model="claude-opus-5",
    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)

권장 Claude 모델

모델가장 적합한 용도
claude-opus-4-6심층 추론, 장문 분석
claude-sonnet-4-6코딩, 일반적인 assistant 작업
claude-haiku-4-5빠르고 가벼운 응답

문제 해결

이 페이지의 내용