Skip to main content

จัดการต้นทุนอย่างมีประสิทธิภาพ

ติดตาม token usage, ตั้ง spend limits สำหรับทีม และลดต้นทุน Claude Code ด้วยการจัดการ context, การเลือกโมเดล, การตั้งค่า extended thinking และ preprocessing hooks

Claude Code คิดราคาตาม API token consumption สำหรับ subscription plan pricing ดู claude.com/pricing

ในการ deploy สำหรับ enterprise ค่าใช้จ่ายเฉลี่ยอยู่ที่ประมาณ $13 ต่อนักพัฒนาต่อวันที่ active และ $150-250 ต่อนักพัฒนาต่อเดือน

ติดตามต้นทุน

ใช้คำสั่ง /usage

note

ส่วน Session block ใน /usage แสดง API token usage และมีไว้สำหรับ API users Claude Max และ Pro subscribers มี usage รวมใน subscription ดังนั้น session cost figure ไม่เกี่ยวกับ billing

Session block ที่ด้านบนของ /usage แสดงสถิติ token usage โดยละเอียดสำหรับ session ปัจจุบัน

Total cost:            $0.55
Total duration (API): 6m 19.7s
Total duration (wall): 6h 33m 10.2s
Total code changes: 0 lines added, 0 lines removed

บน Pro, Max, Team หรือ Enterprise plan /usage แสดง breakdown ของสิ่งที่ count ต่อ plan limits รวมถึง attribution สำหรับ skills, subagents, plugins และ MCP servers แต่ละอัน

จัดการต้นทุนสำหรับทีม

เมื่อใช้ Claude API คุณสามารถ set workspace spend limits Admin สามารถ view cost และ usage reporting ใน Console

Rate limit recommendations

Team sizeTPM ต่อ userRPM ต่อ user
1-5 users200k-300k5-7
5-20 users100k-150k2.5-3.5
20-50 users50k-75k1.25-1.75
50-100 users25k-35k0.62-0.87
100-500 users15k-20k0.37-0.47
500+ users10k-15k0.25-0.35

TPM ต่อ user ลดลงเมื่อ team size เพิ่มขึ้นเนื่องจากผู้ใช้น้อยกว่าใช้ Claude Code พร้อมกันในองค์กรขนาดใหญ่

Agent team token costs

Agent teams สร้าง Claude Code instances หลายตัว แต่ละตัวมี context window ของตัวเอง Token usage ปรับตามจำนวน active teammates

เพื่อควบคุมต้นทุน agent team:

  • ใช้ Sonnet สำหรับ teammates
  • ให้ teams เล็ก
  • ให้ spawn prompts กระชับ
  • Clean up teams เมื่อ work เสร็จ

ลด token usage

Token costs ปรับตาม context size Claude Code optimize ต้นทุนอัตโนมัติผ่าน prompt caching และ auto-compaction

จัดการ context เชิงรุก

  • Clear ระหว่าง tasks: ใช้ /clear เพื่อเริ่มใหม่เมื่อสลับไปทำงานที่ไม่เกี่ยวข้อง
  • เพิ่ม compaction instructions: /compact Focus on code samples and API usage

ปรับแต่ง compaction behavior ใน CLAUDE.md:

# Compact instructions

When you are using compact, please focus on test output and code changes

เลือกโมเดลที่เหมาะสม

Sonnet จัดการ coding tasks ส่วนใหญ่ได้ดีและมีต้นทุนน้อยกว่า Opus ใช้ Opus สำหรับการตัดสินใจ architectural ที่ซับซ้อน

ลด MCP server overhead

MCP tool definitions ถูก defer ตามค่าเริ่มต้น ดังนั้นเฉพาะชื่อ tools เท่านั้นที่เข้า context จนกว่า Claude จะใช้ tool เฉพาะ

  • ใช้ CLI tools เมื่อมี: Tools เช่น gh, aws, gcloud และ sentry-cli ยัง context-efficient กว่า MCP servers
  • ปิดใช้งาน servers ที่ไม่ได้ใช้: รัน /mcp เพื่อดู servers ที่กำหนดค่าไว้

ติดตั้ง code intelligence plugins สำหรับ typed languages

Code intelligence plugins ให้ Claude ค้นหา symbol แบบแม่นยำแทนการค้นหาแบบ text

Offload processing ไปยัง hooks และ skills

Custom hooks สามารถ preprocess data ก่อนที่ Claude จะเห็น แทนที่ Claude จะอ่านไฟล์ log 10,000 บรรทัดเพื่อหาข้อผิดพลาด hook สามารถ grep หา ERROR และส่งคืนเฉพาะบรรทัดที่ตรงกัน

{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/filter-test-output.sh"
}
]
}
]
}
}
#!/bin/bash
input=$(cat)
cmd=$(echo "$input" | jq -r '.tool_input.command')

if [[ "$cmd" =~ ^(npm test|pytest|go test) ]]; then
filtered_cmd="$cmd 2>&1 | grep -A 5 -E '(FAIL|ERROR|error:)' | head -100"
echo "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"allow\",\"updatedInput\":{\"command\":\"$filtered_cmd\"}}}"
else
echo "{}"
fi

ย้าย instructions จาก CLAUDE.md ไปยัง skills

CLAUDE.md โหลดเข้า context ตอนเริ่ม session Skills โหลดตามต้องการเมื่อ invoke เท่านั้น ดังนั้นการย้าย specialized instructions เข้า skills ทำให้ base context เล็กลง มุ่งหมายให้ CLAUDE.md ต่ำกว่า 200 บรรทัด

ปรับ extended thinking

Extended thinking เปิดใช้งานตามค่าเริ่มต้นเพราะปรับปรุง performance อย่างมาก Thinking tokens คิดราคาเป็น output tokens สำหรับ tasks ที่ง่ายกว่า คุณสามารถลดต้นทุนโดย:

  • ลด effort level ด้วย /effort
  • ปิดใช้งาน thinking ใน /config
  • ลด budget ด้วย MAX_THINKING_TOKENS=8000

มอบหมาย verbose operations ให้ subagents

รัน tests, ดึง documentation หรือ process log files สามารถใช้ context มาก มอบหมายให้ subagents เพื่อให้ verbose output อยู่ใน subagent's context ขณะที่ส่งคืนเฉพาะ summary กลับมายัง conversation หลัก

เขียน prompts ที่เฉพาะเจาะจง

Requests คลุมเครือเช่น "improve this codebase" trigger การ scan แบบกว้าง Requests เฉพาะเช่น "add input validation to the login function in auth.ts" ให้ Claude ทำงานได้อย่างมีประสิทธิภาพ

ทำงานอย่างมีประสิทธิภาพใน complex tasks

  • ใช้ plan mode สำหรับ complex tasks: กด Shift+Tab เพื่อเข้า plan mode ก่อน implementation
  • แก้ไขทิศทางแต่เนิ่นๆ: กด Escape เพื่อหยุดทันที ใช้ /rewind เพื่อ restore ไปยัง checkpoint ก่อนหน้า
  • ให้ verification targets: รวม test cases หรือ expected output ใน prompt
  • ทดสอบแบบ incremental: เขียนไฟล์เดียว ทดสอบ จากนั้นดำเนินต่อ

Background token usage

Claude Code ใช้ tokens สำหรับ background functionality บางอย่างแม้เมื่อ idle:

  • Conversation summarization: background jobs ที่สรุปการสนทนาก่อนหน้า
  • Command processing: บาง commands เช่น /usage อาจสร้าง requests

Background processes เหล่านี้ใช้ tokens จำนวนน้อย (โดยทั่วไปต่ำกว่า $0.04 ต่อ session)