Skip to content
Oturumlar ve İş Akışları Orta

Çok Turlu Maliyet Takibi

Her yanıttan total_cost_usd çıkararak oturum turları boyunca kümülatif maliyeti takip edin

Komut

$ RESULT=$("color:#7C5CFC">claude -p "Step 1" "color:#d97757">--output-format json)
  SESSION=$("color:#7C5CFC">echo "$RESULT" | "color:#7C5CFC">jq -r '.session_id')
  COST1=$("color:#7C5CFC">echo "$RESULT" | "color:#7C5CFC">jq -r '.total_cost_usd')
  
  RESULT2=$("color:#7C5CFC">claude -p "Step 2" "color:#d97757">--resume "$SESSION" "color:#d97757">--output-format json)
  COST2=$("color:#7C5CFC">echo "$RESULT2" | "color:#7C5CFC">jq -r '.total_cost_usd')
  "color:#7C5CFC">echo "Total: $(">echo "$COST1 + $COST2" | bc)"

Yanıt

{
  "total_cost_usd": 0.045,
  "usage": {
    "input_tokens": 156,
    "cache_read_input_tokens": 14253,
    "output_tokens": 87
  }
}

Ayrıştırma Kodu

059669">">let totalCost = 0;
059669">">for (059669">">const prompt 059669">">of prompts) {
  059669">">const data = JSON.parse(execFileSync(059669059669">">'claude', [
    059669059669">">'-p', prompt, 059669059669">">'--resume', sessionId, 059669059669">">'--output-format', 059669059669">">'json'
  ], opts));
  totalCost += data.total_cost_usd;
  console.log(059669">`Turn cost: $${data.total_cost_usd}, Running: $${totalCost}`);
}

Tuzaklar

! total_cost_usd yalnızca MEVCUT çağrı içindir, kümülatif oturum toplamı değildir
! Faturalama ve denetim için maliyetleri kendi veritabanınızda takip edin

İlgili Tarifler