Menu
CoddyTech

예산 지킴이

Coddy AI Engineering 여정의 코드에서 모델 호출하기 섹션에 포함된 레슨. 22개 중 21번째.

challenge icon

챌린지

중급

입력 토큰 예산을 초과하지 않고 퀴즈 질문에 답하세요. 첫 번째 입력 줄은 예산이며, 그 다음에는 done 줄이 나올 때까지 줄당 하나씩 질문이 이어집니다.

  1. 각 질문마다 보내기 전에 입력 토큰 수를 세세요: model="claude-opus-5-5", system=SYSTEM 및 질문만을 메시지로 지정하여 client.messages.count_tokens를 사용합니다.
  2. 지금까지 사용한 토큰에 해당 토큰 수를 더한 값이 예산을 초과하면 Stopped: <question> needs <count> tokens, <tokens left> left를 출력하고 질문 읽기를 중단하세요. 예산에 정확히 들어맞는 질문은 그대로 전송됩니다.
  3. 그렇지 않다면 동일한 system, model 및 messages와 max_tokens=200으로 질문을 보내고, usage.input_tokens를 사용한 토큰에 더한 후 Q<number>: 및 답변 텍스트를 출력하세요. 질문 번호는 1부터 시작합니다.
  4. 마지막에 Spent: <spent> of <budget> input tokens를 출력하세요.

예를 들어, 다음 입력에 대해:

100
What is the capital of France?
What is 12 * 12?
What is the tallest mountain?
What is the largest planet?
done

프로그램은 다음을 출력합니다:

Q1: The capital of France is Paris.
Q2: 12 * 12 = 144
Q3: Mount Everest is the tallest mountain above sea level, at 8,849 metres.
Stopped: What is the largest planet? needs 26 tokens, 21 left
Spent: 79 of 100 input tokens

직접 해보기

import coddy_ai as anthropic  # 프로덕션 환경에서는: import anthropic

client = anthropic.Anthropic()

SYSTEM = "You are a quiz helper. Answer in one sentence."

budget = int(input())

# 아래에 코드를 작성하세요

코드에서 모델 호출하기의 모든 레슨

직접 연습해 보세요: 온라인 Python 컴파일러