Claude Platform Docs
Messages컨텍스트 관리

토큰 카운팅

Claude에 메시지를 보내기 전에 메시지의 토큰 수를 계산합니다. 토큰 수를 사용하여 속도 제한과 비용을 관리하고, 모델 라우팅 결정을 내리고, 프롬프트를 목표 길이에 맞추세요.

“Token counting”(토큰 카운팅)을 사용하면 Claude에 메시지를 보내기 전에 메시지의 토큰 수를 확인할 수 있습니다. 이를 통해 프롬프트와 사용량에 대해 정보에 기반한 결정을 내릴 수 있습니다. 토큰 카운팅을 사용하면 다음을 수행할 수 있습니다:

  • “rate limit”(속도 제한)과 비용을 사전에 관리
  • 현명한 모델 라우팅 결정
  • 프롬프트를 특정 길이로 최적화

메시지 토큰을 계산하는 방법

토큰 카운팅API 참조 링크가 포함된 미리보기 사용 가능 — 카드에 포커스가 들어올 때까지 Tab을 누르고, 돌아가려면 Escape를 누르세요. 엔드포인트는 메시지 생성과 동일한 구조화된 입력 목록을 받으며, 시스템 프롬프트, 도구, 이미지, PDF를 지원합니다. 응답에는 총 입력 토큰API 참조 링크가 포함된 미리보기 사용 가능 — 카드에 포커스가 들어올 때까지 Tab을 누르고, 돌아가려면 Escape를 누르세요. 수가 포함됩니다.

Count tokens in a Message

POST/v1/messages/count_tokens

Count the number of tokens in a Message.

이 엔드포인트는 Messages API가 허용하는 일부 입력에 대해 invalid_request_error를 반환합니다:

이미지와 PDF의 토큰 수를 계산하려면 base64로 전송하세요. 서버 도구 또는 MCP 서버를 사용하는 요청의 경우, Messages API 응답의 usageAPI 참조 링크가 포함된 미리보기 사용 가능 — 카드에 포커스가 들어올 때까지 Tab을 누르고, 돌아가려면 Escape를 누르세요. 객체에 사용된 토큰이 보고됩니다.

지원 모델

모든 활성 모델이 토큰 카운팅을 지원합니다.

기본 메시지의 토큰 계산

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-5-5",
    system="You are a scientist",
    messages=[{"role": "user", "content": "Hello, Claude"}],
)

print(response.json())
Output
{ "input_tokens": 14 }

도구가 포함된 메시지의 토큰 계산

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-5-5",
    tools=[
        {
            "name": "get_weather",
            "description": "Get the current weather in a given location",
            "input_schema": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    }
                },
                "required": ["location"],
            },
        }
    ],
    messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}],
)

print(response.json())
Output
{ "input_tokens": 403 }

이미지가 포함된 메시지의 토큰 계산

import base64
import httpx2

image_url = "https://platform.claude.com/docs/images/vision-example.jpg"
image_media_type = "image/jpeg"
image_data = base64.standard_b64encode(httpx2.get(image_url).content).decode("utf-8")

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-5-5",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": image_media_type,
                        "data": image_data,
                    },
                },
                {"type": "text", "text": "Describe this image"},
            ],
        }
    ],
)
print(response.json())
Output
{ "input_tokens": 1028 }

"oversized_image": "error"를 설정한 임베디드 이미지 블록은 Messages API가 거부하는 것과 정확히 동일하게 카운트 시점에 거부됩니다.

사고가 포함된 메시지의 토큰 계산

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-opus-5-5",
    thinking={"type": "adaptive"},
    messages=[
        {
            "role": "user",
            "content": "Are there an infinite number of prime numbers such that n mod 4 == 3?",
        },
        {
            "role": "assistant",
            "content": [
                {
                    "type": "thinking",
                    "thinking": "This is a nice number theory question. Let's think about it step by step...",
                    "signature": "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV...",
                },
                {
                    "type": "text",
                    "text": "Yes, there are infinitely many prime numbers p such that p mod 4 = 3...",
                },
            ],
        },
        {"role": "user", "content": "Can you write a formal proof?"},
    ],
)

print(response.json())
Output
{ "input_tokens": 88 }

PDF가 포함된 메시지의 토큰 계산

import base64
import anthropic

client = anthropic.Anthropic()

with open("/path/to/document.pdf", "rb") as pdf_file:
    pdf_base64 = base64.standard_b64encode(pdf_file.read()).decode("utf-8")

response = client.messages.count_tokens(
    model="claude-opus-5-5",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "base64",
                        "media_type": "application/pdf",
                        "data": pdf_base64,
                    },
                },
                {"type": "text", "text": "Please summarize this document."},
            ],
        }
    ],
)

print(response.json())
Output
{ "input_tokens": 2188 }

Claude Fable 및 Claude Mythos 모델의 토큰 수

Claude Fable 5.1, Claude Mythos 5.1, Claude Fable 5, Claude Mythos 5는 Claude Opus 4.7과 함께 도입된 토크나이저를 공유합니다. 프롬프트는 네 모델 모두에서 동일하게 계산되며, Claude Opus 4.7 이전 모델보다 약 30퍼센트 더 높게 계산됩니다(정확한 증가량은 콘텐츠에 따라 다릅니다). 토큰 카운팅 엔드포인트는 전달한 model의 토크나이저를 기준으로 계산합니다. 워크로드에 대한 차이를 측정하려면 동일한 요청을 현재 모델로 한 번, 이동하려는 모델로 한 번, 총 두 번 계산한 다음 두 input_tokens 값을 비교하세요.


가격 및 속도 제한

토큰 카운팅은 무료로 사용할 수 있지만, 사용 등급에 따른 분당 요청 수 속도 제한이 적용됩니다. 더 높은 한도가 필요한 경우 Claude Console의 속도 제한 페이지에서 티어 상향 요청을 사용하세요.

사용 등급분당 요청 수(RPM)
Start5,000
Build10,000
Scale20,000

FAQ


다음 단계

토큰 카운팅 엔드포인트에 대한 전체 API 레퍼런스를 읽어보세요.

토큰 수를 사용하여 프롬프트를 모델의 컨텍스트 윈도우 내로 유지하세요.

요청을 보내기 전에 토큰 수를 확인하여 사용 등급 내로 유지하세요.

프롬프트 접두사를 캐싱하여 반복되는 프롬프트의 비용과 지연 시간을 줄이세요.

제공 여부

토큰 카운팅은(는) 모든 모델에서 사용 가능 상태이며 제로 데이터 보존 및 HIPAA 대비에 적격합니다.

API 지원

상태사용 가능
모델
상태사용 불가

데이터 처리

데이터 처리 개요

적용 엔드포인트

  • /v1/messages/count_tokens
제로 데이터 보존적격

적용 모델 제외

요청을 보내기 전에 토큰 수를 계산합니다.

HIPAA적격

이 페이지가 도움이 되었나요?