Anthropic Passthrough

Anthropic Passthrough

Anthropic의 패스스루 엔드포인트를 소개할게요. Anthropic의 프로바이더 고유 엔드포인트를 네이티브 형식 그대로(변환 없이) 호출할 수 있는 기능이에요. /messages, /v1/messages/batches 같은 Anthropic 전용 엔드포인트를 그대로 사용할 수 있어요.

출처: 문서

본문

연결 대상 호스트는 https://api.anthropic.com이고, 프록시를 통한 주소는 이렇게 구성돼요.

LITELLM_PROXY_BASE_URL/anthropic

사용 예시 (Example Usage)

아래 요청에 end_user(최종 사용자) 필드를 포함하면 litellm.enable_end_user_cost_tracking_prometheus_only 설정과 함께 최종 사용자별 비용 추적을 Prometheus로 할 수 있어요. /v1/messages 엔드포인트를 호출하는 예시예요.

curl --request POST \
  --url http://0.0.0.0:4000/anthropic/v1/messages \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --header "Authorization: bearer ***" \
  --data '{
        "model": "claude-sonnet-5",
        "max_tokens": 1024,
        "messages": [
            {"role": "user", "content": "Hello, world"}
        ]
    }'

Anthropic Python SDK를 프록시로 연결하는 예시예요.

from anthropic import Anthropic
# Initialize client with proxy base URL
client = Anthropic(
    base_url="http://0.0.0.0:4000/anthropic", # <proxy-base-url>/anthropic
    api_key="sk-anything" # proxy virtual key
)
# Make a completion request
response = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, world"}
    ]
)
print(response)

빠른 시작 (Quick Start)

Anthropic의 /messages 엔드포인트를 호출하는 예시예요. 먼저 API 키를 환경 변수로 설정해요.

export ANTHROPIC_API_KEY=""

그다음 LiteLLM 프록시를 실행해요.

litellm
# RUNNING on http://0.0.0.0:4000

이제 /messages 엔드포인트를 Anthropic 네이티브 방식(x-api-key 헤더)으로 호출해요.

curl http://0.0.0.0:4000/anthropic/v1/messages \
     --header "x-api-key: *** \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
    '{
        "model": "claude-sonnet-5",
        "max_tokens": 1024,
        "messages": [
            {"role": "user", "content": "Hello, world"}
        ]
    }'

예시 (Examples)

핵심 아이디어는 단순해요. Anthropic SDK/API의 호스트를 http://0.0.0.0:4000/anthropic로 바꾸면 돼요. 원래 https://api.anthropic.com을 호출하던 것을 http://0.0.0.0:4000/anthropic로 바꾸고, 인증 헤더를 bearer $ANTHROPIC_API_KEY 대신 LiteLLM 키(bearer anything 또는 가상 키 bearer LITELLM_VIRTUAL_KEY)로 바꾸면 됩니다.

예시 1: messages 엔드포인트

LiteLLM 프록시 호출

curl --request POST \
  --url http://0.0.0.0:4000/anthropic/v1/messages \
  --header "x-api-key: *** \
    --header "anthropic-version: 2023-06-01" \
    --header "content-type: application/json" \
  --data '{
    "model": "claude-sonnet-5",
    "max_tokens": 1024,
    "messages": [
        {"role": "user", "content": "Hello, world"}
    ]
  }'

Anthropic 직접 API 호출 (변환 전)

curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: $ANTHR...KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
    '{
        "model": "claude-sonnet-5",
        "max_tokens": 1024,
        "messages": [
            {"role": "user", "content": "Hello, world"}
        ]
    }'

예시 2: 토큰 계산 API (Token Counting)

LiteLLM 프록시 호출

curl --request POST \
    --url http://0.0.0.0:4000/anthropic/v1/messages/count_tokens \
    --header "x-api-key: *** \
    --header "anthropic-version: 2023-06-01" \
    --header "anthropic-beta: token-counting-2024-11-01" \
    --header "content-type: application/json" \
    --data \
    '{
        "model": "claude-sonnet-5",
        "messages": [
            {"role": "user", "content": "Hello, world"}
        ]
    }'

Anthropic 직접 API 호출 (변환 전)

curl https://api.anthropic.com/v1/messages/count_tokens \
     --header "x-api-key: $ANTHR...KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "anthropic-beta: token-counting-2024-11-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-sonnet-5",
    "messages": [
        {"role": "user", "content": "Hello, world"}
    ]
}'

예시 3: 배치 메시지 (Batch Messages)

LiteLLM 프록시 호출

curl --request POST \
    --url http://0.0.0.0:4000/anthropic/v1/messages/batches \
    --header "x-api-key: *** \
    --header "anthropic-version: 2023-06-01" \
    --header "anthropic-beta: message-batches-2024-09-24" \
    --header "content-type: application/json" \
    --data \
'{
    "requests": [
        {
            "custom_id": "my-first-request",
            "params": {
                "model": "claude-sonnet-5",
                "max_tokens": 1024,
                "messages": [
                    {"role": "user", "content": "Hello, world"}
                ]
            }
        },
        {
            "custom_id": "my-second-request",
            "params": {
                "model": "claude-sonnet-5",
                "max_tokens": 1024,
                "messages": [
                    {"role": "user", "content": "Hi again, friend"}
                ]
            }
        }
    ]
}'

Anthropic 직접 API 호출 (변환 전)

curl https://api.anthropic.com/v1/messages/batches \
     --header "x-api-key: $ANTHR...KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "anthropic-beta: message-batches-2024-09-24" \
     --header "content-type: application/json" \
     --data \
'{
    "requests": [
        {
            "custom_id": "my-first-request",
            "params": {
                "model": "claude-sonnet-5",
                "max_tokens": 1024,
                "messages": [
                    {"role": "user", "content": "Hello, world"}
                ]
            }
        },
        {
            "custom_id": "my-second-request",
            "params": {
                "model": "claude-sonnet-5",
                "max_tokens": 1024,
                "messages": [
                    {"role": "user", "content": "Hi again, friend"}
                ]
            }
        }
    ]
}'

프록시에서 Anthropic 모델을 등록하려면 proxy_config.yaml에 다음과 같이 설정해요.

model_list:
  - model_name: claude-sonnet-5  # or any alias
    litellm_params:
      model: anthropic/claude-sonnet-5
      api_key: os.environ/ANTHROPIC_API_KEY

고급 (Advanced)

가상 키(Virtual Keys)와 함께 사용하기

가상 키는 LiteLLM 프록시에 데이터베이스가 설정된 경우에 사용할 수 있어요. 가상 키 설정 문서를 참고해 주세요.

환경 변수를 설정해요.

export DATABASE_URL=""
export LITELLM_MASTER_KEY=""
export COHERE_API_KEY=""

프록시를 실행해요.

litellm
# RUNNING on http://0.0.0.0:4000

가상 키를 생성해요.

curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H "Authorization: Bearer ***" \
-H 'Content-Type: application/json' \
-d '{}'

응답에서 키를 받아요.

{
    ...
    "key": "sk-<virtual-key>"
}

이제 messages 엔드포인트를 가상 키로 호출해요.

curl --request POST \
  --url http://0.0.0.0:4000/anthropic/v1/messages \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --header "Authorization: bearer ***" \
  --data '{
    "model": "claude-sonnet-5",
    "max_tokens": 1024,
    "messages": [
        {"role": "user", "content": "Hello, world"}
    ]
  }'

litellm_metadata로 태그·최종 사용자 비용 추적 보내기

litellm_metadata 필드에 tagsuser를 넣어 최종 사용자/고객 비용을 추적할 수 있어요.

curl --request POST \
  --url http://0.0.0.0:4000/anthropic/v1/messages \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --header "Authorization: bearer ***" \
  --data '{
    "model": "claude-sonnet-5",
    "max_tokens": 1024,
    "messages": [
        {"role": "user", "content": "Hello, world"}
    ],
    "litellm_metadata": {
        "tags": ["test-tag-1", "test-tag-2"], 
        "user": "test-user" # track end-user/customer cost
    }
  }'

Python SDK에서도 동일하게 사용할 수 있어요. extra_bodylitellm_metadata를 넣거나, Anthropic 네이티브 파라미터인 metadatauser_id를 넣어 최종 사용자 비용을 추적할 수 있습니다.

from anthropic import Anthropic
client = Anthropic(
    base_url="http://0.0.0.0:4000/anthropic",
    api_key="sk-anything")
response = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, world"}
    ],
    extra_body={
        "litellm_metadata": {
            "tags": ["test-tag-1", "test-tag-2"], 
            "user": "test-user" # track end-user/customer cost
        }
    },
    ## OR
    ##
    metadata={ # anthropic native param - https://docs.anthropic.com/en/api/messages
        "user_id": "test-user" # track end-user/customer cost
    })
print(response)

더 알아보기 (Learn more)