토큰 카운팅을 통해 메시지를 Claude에 보내기 전에 메시지의 토큰 수를 확인할 수 있어, 프롬프트와 사용량에 대해 정보에 기반한 결정을 내릴 수 있습니다. 토큰 카운팅으로 다음과 같은 것이 가능합니다:

  • 속도 제한과 비용을 사전에 관리
  • 스마트한 모델 라우팅 결정
  • 프롬프트를 특정 길이로 최적화

메시지 토큰 카운팅 방법

토큰 카운팅 엔드포인트는 시스템 프롬프트, 도구, 이미지, PDF를 포함하여 메시지 생성을 위한 동일한 구조화된 입력 목록을 받습니다. 응답에는 총 입력 토큰 수가 포함됩니다.

토큰 카운트는 추정치로 간주해야 합니다. 경우에 따라 메시지를 생성할 때 실제 사용된 입력 토큰의 수는 약간 다를 수 있습니다.

지원되는 모델

토큰 카운팅 엔드포인트는 다음 모델을 지원합니다:

  • Claude 3.7 Sonnet
  • Claude 3.5 Sonnet
  • Claude 3.5 Haiku
  • Claude 3 Haiku
  • Claude 3 Opus

기본 메시지의 토큰 카운팅

import anthropic

client = anthropic.Anthropic()

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

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

도구가 포함된 메시지의 토큰 카운팅

import anthropic

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-3-7-sonnet-20250219",
    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())
JSON
{ "input_tokens": 403 }

이미지가 포함된 메시지의 토큰 카운팅

import anthropic
import base64
import httpx

image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
image_media_type = "image/jpeg"
image_data = base64.standard_b64encode(httpx.get(image_url).content).decode("utf-8")

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-3-7-sonnet-20250219",
    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())
JSON
{ "input_tokens": 1551 }

확장된 사고가 포함된 메시지의 토큰 카운팅

확장된 사고를 사용할 때 컨텍스트 윈도우가 어떻게 계산되는지에 대한 자세한 내용은 여기를 참조하세요

  • 이전 어시스턴트 턴의 사고 블록은 무시되며 입력 토큰에 포함되지 않습니다
  • 현재 어시스턴트 턴의 사고는 입력 토큰에 포함됩니다
import anthropic

client = anthropic.Anthropic()

response = client.messages.count_tokens(
    model="claude-3-7-sonnet-20250219",
    thinking={
        "type": "enabled",
        "budget_tokens": 16000
    },
    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())
JSON
{ "input_tokens": 88 }

PDF가 포함된 메시지의 토큰 카운팅

토큰 카운팅은 Messages API와 동일한 제한사항으로 PDF를 지원합니다.

import base64
import anthropic

client = anthropic.Anthropic()

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

response = client.messages.count_tokens(
    model="claude-3-7-sonnet-20250219",
    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())
JSON
{ "input_tokens": 2188 }

가격 책정 및 속도 제한

토큰 카운팅은 무료로 사용할 수 있지만 사용 등급에 따른 분당 요청 제한이 적용됩니다. 더 높은 제한이 필요한 경우 Anthropic Console을 통해 영업팀에 문의하세요.

사용 등급분당 요청 수(RPM)
1100
22,000
34,000
48,000

토큰 카운팅과 메시지 생성은 별도의 독립적인 속도 제한을 가지고 있습니다 — 하나의 사용이 다른 것의 제한에 영향을 미치지 않습니다.


FAQ

Was this page helpful?