토큰 이해 및 집계
Gemini 및 기타 생성형 AI 모델은 토큰이라는 세분성으로 입력과 출력을 처리합니다.
Gemini 모델의 경우 토큰은 약 4자에 해당합니다. 100개의 토큰은 약 60~80개의 영어 단어에 해당합니다.
토큰 정보
토큰은 단일 문자(예: z) 또는 전체 단어(예: cat)일 수 있습니다. 긴 단어는 여러 토큰으로 나뉩니다. 모델에서 사용하는 모든 토큰 집합을 어휘라고 하며, 텍스트를 토큰으로 분할하는 프로세스를 토큰화라고 합니다.
결제가 사용 설정된 경우 Gemini API 호출 비용은 입력 및 출력 토큰 수에 따라 결정되므로 토큰 수를 세는 방법을 알아두면 유용합니다.
토큰 집계
텍스트, 이미지 파일, 기타 비텍스트 모달리티를 비롯한 Gemini API의 모든 입력과 출력은 토큰화됩니다.
다음과 같은 방법으로 토큰을 계산할 수 있습니다.
요청의 입력으로
count_tokens호출 입력만의 총 토큰 수를 반환합니다. 입력을 전송하기 전에 이 호출을 실행하여 요청의 크기를 확인하세요.상호작용 응답에서
usage를 사용합니다. 입력 (total_input_tokens), 출력 (total_output_tokens), 생각 (total_thought_tokens), 캐시된 콘텐츠(total_cached_tokens), 도구 사용 (total_tool_use_tokens), 합계 (total_tokens)의 토큰 수를 반환합니다.
텍스트 토큰 수 계산
Python
# This will only work for SDK newer than 2.0.0
from google import genai
client = genai.Client()
prompt = "The quick brown fox jumps over the lazy dog."
# Count tokens before sending
total_tokens = client.models.count_tokens(
model="gemini-3-flash-preview",
contents=prompt
)
print("total_tokens:", total_tokens.total_tokens)
# Get usage from interaction
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input=prompt
)
print(interaction.usage)
자바스크립트
// This will only work for SDK newer than 2.0.0
import { GoogleGenAI } from '@google/genai';
const client = new GoogleGenAI({});
const prompt = "The quick brown fox jumps over the lazy dog.";
// Count tokens before sending
const countResponse = await client.models.countTokens({
model: "gemini-3-flash-preview",
contents: prompt,
});
console.log(countResponse.totalTokens);
// Get usage from interaction
const interaction = await client.interactions.create({
model: "gemini-3-flash-preview",
input: prompt,
});
console.log(interaction.usage);
REST
# Specifies the API revision to avoid breaking changes when they become default
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:countTokens" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-H "Api-Revision: 2026-05-20" \
-d '{"contents": [{"parts": [{"text": "The quick brown fox."}]}]}'
멀티턴 토큰 수 계산
previous_interaction_id를 사용하여 대화 기록에서 토큰을 집계합니다.
Python
# This will only work for SDK newer than 2.0.0
# First interaction
interaction1 = client.interactions.create(
model="gemini-3-flash-preview",
input="Hi, my name is Bob"
)
# Second interaction continues the conversation
interaction2 = client.interactions.create(
model="gemini-3-flash-preview",
input="What's my name?",
previous_interaction_id=interaction1.id
)
# Usage includes tokens from both turns
print(f"Input tokens: {interaction2.usage.total_input_tokens}")
print(f"Output tokens: {interaction2.usage.total_output_tokens}")
print(f"Total tokens: {interaction2.usage.total_tokens}")
자바스크립트
// This will only work for SDK newer than 2.0.0
// First interaction
const interaction1 = await client.interactions.create({
model: "gemini-3-flash-preview",
input: "Hi, my name is Bob"
});
// Second interaction continues the conversation
const interaction2 = await client.interactions.create({
model: "gemini-3-flash-preview",
input: "What's my name?",
previous_interaction_id: interaction1.id
});
console.log(`Input tokens: ${interaction2.usage.total_input_tokens}`);
console.log(`Output tokens: ${interaction2.usage.total_output_tokens}`);
멀티모달 토큰 수 계산
이미지, 동영상, 오디오를 비롯한 Gemini API의 모든 입력은 토큰화됩니다. 토큰화에 관한 핵심 사항:
- 이미지: 두 치수가 모두 384픽셀 이하인 이미지는 258개의 토큰으로 계산됩니다. 더 큰 이미지는 768x768픽셀 타일로 타일링되며 각 타일은 258개의 토큰으로 계산됩니다.
- 동영상: 초당 토큰 263개
- 오디오: 초당 토큰 32개
이미지 토큰
Python
# This will only work for SDK newer than 2.0.0
uploaded_file = client.files.upload(file="path/to/image.jpg")
# Count tokens for image + text
total_tokens = client.models.count_tokens(
model="gemini-3-flash-preview",
contents=["Tell me about this image", uploaded_file]
)
print(f"Total tokens: {total_tokens}")
# Generate with image
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input=[
{"type": "text", "text": "Tell me about this image"},
{"type": "image", "uri": uploaded_file.uri, "mime_type": uploaded_file.mime_type}
]
)
print(interaction.usage)
자바스크립트
// This will only work for SDK newer than 2.0.0
const uploadedFile = await client.files.upload({
file: "path/to/image.jpg",
config: { mimeType: "image/jpeg" }
});
// Count tokens
const countResponse = await client.models.countTokens({
model: "gemini-3-flash-preview",
contents: [
{ text: "Tell me about this image" },
{ fileData: { fileUri: uploadedFile.uri, mimeType: uploadedFile.mimeType } }
]
});
console.log(countResponse.totalTokens);
인라인 데이터 예:
Python
# This will only work for SDK newer than 2.0.0
import base64
with open('image.jpg', 'rb') as f:
image_bytes = f.read()
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input=[
{"type": "text", "text": "Describe this image"},
{
"type": "image",
"data": base64.b64encode(image_bytes).decode('utf-8'),
"mime_type": "image/jpeg"
}
]
)
print(interaction.usage)
동영상 토큰
Python
# This will only work for SDK newer than 2.0.0
import time
video_file = client.files.upload(file="path/to/video.mp4")
while not video_file.state or video_file.state.name != "ACTIVE":
print("Processing video...")
time.sleep(5)
video_file = client.files.get(name=video_file.name)
# A 60-second video is approximately 263 * 60 = 15,780 tokens
total_tokens = client.models.count_tokens(
model="gemini-3-flash-preview",
contents=["Summarize this video", video_file]
)
print(f"Total tokens: {total_tokens}")
# Generate with video
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input=[
{"type": "text", "text": "Summarize this video"},
{"type": "video", "uri": video_file.uri, "mime_type": video_file.mime_type}
]
)
print(interaction.usage)
오디오 토큰
Python
# This will only work for SDK newer than 2.0.0
audio_file = client.files.upload(file="path/to/audio.mp3")
# A 60-second audio clip is approximately 32 * 60 = 1,920 tokens
total_tokens = client.models.count_tokens(
model="gemini-3-flash-preview",
contents=["Transcribe this audio", audio_file]
)
print(f"Total tokens: {total_tokens}")
# Generate with audio
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input=[
{"type": "text", "text": "Transcribe this audio"},
{"type": "audio", "uri": audio_file.uri, "mime_type": audio_file.mime_type}
]
)
print(interaction.usage)
시스템 요청 사항 토큰 수 계산
시스템 명령어는 입력 토큰의 일부로 계산됩니다.
Python
# This will only work for SDK newer than 2.0.0
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Hello!",
system_instruction="You are a helpful assistant who speaks like a pirate."
)
# system_instruction tokens included in total_input_tokens
print(f"Input tokens: {interaction.usage.total_input_tokens}")
도구 토큰 수 계산
도구 (함수, 코드 실행, Google 검색)도 다음과 같이 계산됩니다.
Python
# This will only work for SDK newer than 2.0.0
tools = [
{
"type": "function",
"name": "get_weather",
"description": "Get current weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
}
}
}
]
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="What's the weather in Tokyo?",
tools=tools
)
print(f"Input tokens: {interaction.usage.total_input_tokens}")
print(f"Tool use tokens: {interaction.usage.total_tool_use_tokens}")
컨텍스트 윈도우
각 Gemini 모델에는 처리할 수 있는 최대 토큰 수가 있습니다. 컨텍스트 윈도우는 입력 및 출력 토큰의 결합된 한도를 정의합니다.
프로그래매틱 방식으로 컨텍스트 윈도우 크기 가져오기
Python
# This will only work for SDK newer than 2.0.0
model_info = client.models.get(model="gemini-3-flash-preview")
print(f"Input token limit: {model_info.input_token_limit}")
print(f"Output token limit: {model_info.output_token_limit}")
자바스크립트
// This will only work for SDK newer than 2.0.0
const modelInfo = await client.models.get({ model: "gemini-3-flash-preview" });
console.log(`Input token limit: ${modelInfo.inputTokenLimit}`);
console.log(`Output token limit: ${modelInfo.outputTokenLimit}`);
모델 페이지에서 컨텍스트 윈도우 크기를 확인하세요.