Menu
CoddyTech

비용 보고서

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

challenge icon

챌린지

중급

마지막 단계: 모든 호출에는 비용이 들므로, 보고서는 실행에서 사용된 내역으로 끝납니다. 지금까지 작성한 코드를 모두 유지하세요.

  1. 클라이언트 뒤에 PRICE_IN = 4.00 및 PRICE_OUT = 20.00을 추가하세요: 2026년 11월 기준 백만 입력 및 출력 토큰당 달러 단위의 Claude Opus 5.5 가격입니다(이를 참고하기 전에 요금 안내 페이지를 확인하세요).
  2. 루프 앞에 tokens_in과 tokens_out을 0으로 설정하세요. 각 호출 후 응답의 usage.input_tokens와 usage.output_tokens를 여기에 더하세요.
  3. 마지막에 Tokens: <in> in, <out> out을 출력한 다음, Cost: $와 소수점 6자리까지의 비용을 출력하세요. 비용은 입력 토큰을 1,000,000으로 나눈 값에 PRICE_IN을 곱한 것에, 출력 토큰을 1,000,000으로 나눈 값에 PRICE_OUT을 곱한 값을 더한 것입니다.

예를 들어, 이전 단계의 두 billing 티켓에 대해 보고서는 다음과 같이 끝납니다:

billing: 2
Urgent: none
Tokens: 324 in, 72 out
Cost: $0.002736

직접 해보기

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

client = anthropic.Anthropic()

SCHEMA = {
    "type": "object",
    "properties": {
        "category": {"type": "string", "enum": ["billing", "shipping", "account", "technical", "other"]},
        "urgent": {"type": "boolean"},
        "summary": {"type": "string"},
    },
    "required": ["category", "urgent", "summary"],
    "additionalProperties": False,
}

count = int(input())
tickets = [input() for _ in range(count)]

print(f"Tickets: {count}")
results = []
for number, ticket in enumerate(tickets, start=1):
    response = client.messages.create(
        model="claude-opus-5-5",
        max_tokens=200,
        output_config={"format": {"type": "json_schema", "schema": SCHEMA}},
        messages=[{"role": "user", "content": ticket}],
    )
    for block in response.content:
        if block.type == "text":
            data = json.loads(block.text)
            results.append(data)
            urgent = "yes" if data["urgent"] else "no"
            print(f"#{number} {data['category']} | urgent: {urgent} | {data['summary']}")

for category in SCHEMA["properties"]["category"]["enum"]:
    total = sum(1 for data in results if data["category"] == category)
    if total > 0:
        print(f"{category}: {total}")
urgent_numbers = [f"#{number}" for number, data in enumerate(results, start=1) if data["urgent"]]
if urgent_numbers:
    print("Urgent: " + ", ".join(urgent_numbers))
else:
    print("Urgent: none")

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

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