Quest 5 - Context Window Budget Calculator
Quest 5: Context Window Budget Calculator
medium 25 minutes🎯 Learning Objectives
- How context windows work and why budgeting matters
- How to calculate available tokens given system/user/history/reserved response
- Why negative available tokens must be clamped to zero
- The engineering habit: BUDGET YOUR CONTEXT
📖 Concept: Context Window Budgeting
ทุก LLM มี context window — จำนวน tokens สูงสุดที่รับได้ใน 1 request เช่น GPT-4 รับ 128K tokens, Claude รับ 200K tokens
แต่ context window ไม่ใช่ของฟรี — คุณต้อง แบ่งสรร ให้:
- System prompt: instructions ที่บอก AI ว่าต้องทำอะไร
- User prompt: คำถามหรือคำสั่งของผู้ใช้
- History: บทสนทนาก่อนหน้า
- Reserved response: พื้นที่สำหรับคำตอบ
ถ้าคุณไม่ budget ให้ดี ข้อความจะถูกตัด (truncated) ทำให้ AI ตอบไม่ครบ
⚙️ How It Works
The Budget Formula
total = 16384 (context window size)used = systemTokens + userTokens + historyTokensavailable = total - used - reservedResponse (min 0)withinBudget = (used + reservedResponse) <= totaloverBy = max(0, (used + reservedResponse) - total)ตัวอย่าง Calculation
// Scenario: Chatbot with historyconst systemTokens = 500; // system promptconst userTokens = 100; // current messageconst historyTokens = 2000; // previous messagesconst reservedResponse = 1000; // expected response size
// total = 16384// used = 500 + 100 + 2000 = 2600// available = 16384 - 2600 - 1000 = 12784// withinBudget = true// overBy = 0Edge Case: Over Budget
// Scenario: Too much historyconst systemTokens = 500;const userTokens = 100;const historyTokens = 15000; // ประวัติเยอะมากconst reservedResponse = 1000;
// total = 16384// used = 500 + 100 + 15000 = 15600// available = 16384 - 15600 - 1000 = -216 → clamp to 0// withinBudget = false// overBy = 15600 + 1000 - 16384 = 216💡 Example: Budget Calculator Implementation
function calculateBudget({ systemTokens, userTokens, historyTokens, reservedResponse }) { const total = 16384; const used = systemTokens + userTokens + historyTokens; const available = Math.max(0, total - used - reservedResponse); const withinBudget = (used + reservedResponse) <= total; const overBy = withinBudget ? 0 : (used + reservedResponse) - total;
return { total, used, available, withinBudget, overBy };}
// Example usageconst budget = calculateBudget({ systemTokens: 500, userTokens: 100, historyTokens: 2000, reservedResponse: 1000});
console.log(budget);// { total: 16384, used: 2600, available: 12784, withinBudget: true, overBy: 0 }⚠️ Common Mistakes
Mistake 1: ไม่ clamp available เป็น 0
ถ้า usage > total, available จะติดลบ → ต้องใช้
Math.max(0, ...)
Mistake 2: คำนวณ overBy ผิด
คิดว่า overBy = total - used (กลับกัน) → overBy = (used + reservedResponse) - total
Mistake 3: ลืม reservedResponse
คำนวณ available โดยไม่หัก reservedResponse → ต้องหัก reservedResponse ด้วย
Mistake 4: ไม่ test edge cases
ทดสอบแค่ happy path → ทดสอบ over budget, exact budget, zero tokens
📝 Knowledge Check
📝 Knowledge Check
Q1:What happens when usage exceeds the context window?
Q2:In the budget formula, what is `used` equal to?
Q3:Why must `overBy` be 0 (not negative) when within budget?
🏋️ Quest: Context Window Budget Calculator
เขียน function ที่คำนวณ context window budget อย่างแม่นยำ
-
Download ไฟล์เริ่มต้นของ quest:
Terminal window npx bluebeltdojo download quest-05-context-budgetcd quest-05-context-budget -
เปิด
problem.jsใน editor ของคุณพร้อม AI tool -
ให้ AI ช่วย implement
calculateBudget()ตาม contract ที่กำหนด -
ตรวจสอบ:
Terminal window node test.js -
ทดสอบ edge cases: over budget, exact budget, zero tokens
-
ส่งคำตอบ:
Terminal window npx bluebeltdojo submit
💡 Tip: ถ้า AI ให้
available = total - usedโดยไม่ clamp เป็น 0 ให้บอกว่าต้องใช้Math.max(0, ...)
คำใบ้
- อ่าน instructions ใน
problem.jsอย่างละเอียด — มี contract ระบุไว้ชัดเจน - ต้อง return object ที่มี: total, used, available, withinBudget, overBy
- available ต้อง clamp ที่ 0 ไม่ให้ติดลบ
- ทดสอบ over budget scenario — overBy ต้อง > 0