Menu
CoddyTech

Bütçe Koruyucu

Coddy'nin AI Engineering Journey'sinin Kod İle Model Çağırma bölümünün bir parçası. Ders 21 / 22.

challenge icon

Görev

Orta

Girdi token bütçesini aşmadan bilgi yarışması sorularını yanıtlayın. İlk girdi satırı bütçedir; ardından done satırına kadar her satırda bir soru gelir.

  1. Her soru için, soruyu göndermeden önce girdi token'larını sayın: model="claude-opus-5-5", system=SYSTEM ve tek mesaj olarak soru ile client.messages.count_tokens kullanın.
  2. Şimdiye kadar harcanan token'lar ile bu sayımın toplamı bütçeyi aşarsa, Stopped: <question> needs <count> tokens, <tokens left> left yazdırın ve soru okumayı durdurun. Bütçeye tam uyan bir soru yine de gönderilir.
  3. Aksi takdirde soruyu aynı system, model, mesajlar ve max_tokens=200 ile gönderin, usage.input_tokens değerini harcanan token'lara ekleyin ve Q<number>: ile yanıt metnini yazdırın. Sorular 1'den başlayarak numaralandırılır.
  4. Sonunda, Spent: <spent> of <budget> input tokens yazdırın.

Örneğin, bu girdi için:

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

program şunu yazdırır:

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

Kendin dene

import coddy_ai as anthropic  # üretim ortamında: import anthropic

client = anthropic.Anthropic()

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

budget = int(input())

# Kodunuzu aşağıya yazın

Kod İle Model Çağırma bölümündeki tüm dersler

Kendi başına pratik yap: Online Python derleyicisi