agentsclimarketplace

Cc cost engineering

Skill cablate/ai-toolkit/domain-skills/claude-code/cc-cost-engineering

Battle-tested Claude Code skills for relentless AI delivery

Install
npx -y skills add cablate/ai-toolkit --skill cc-cost-engineering

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 14 stars14 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its author says it does

Copied from the file, not written here

AI Agent 成本工程 — 基於 Claude Code 的成本追蹤、prompt cache 最佳化、token 預算控制逆向分析。Use when: 優化 token 消耗、設計成本控制機制、分析 cache 效率、選擇模型配置。

SKILL.md

3.6 KB, as published. Nobody here has run it

AI Agent 成本工程

基於 Claude Code v2.1.88 的成本控制機制逆向分析。

模型成本矩陣

模型Input/MTokOutput/MTok相對成本
Haiku 3.5$0.80$4.001x
Sonnet 4.6$3.00$15.003.75x
Opus 4.6$15.00$75.0018.75x
Opus 4.6 Fast$30.00$150.0037.5x

Cached tokens 便宜 75-90%。 這是 Claude Code 整個架構圍繞 cache 設計的原因。

Cost Envelope 模式

// Claude Code 的 cost-tracker.ts 核心邏輯
function addToTotalSessionCost(inputTokens, outputTokens, model, speed) {
  const costPerInputToken = MODEL_COSTS[model]?.input ?? UNKNOWN_FALLBACK
  const cost = inputTokens * costPerInputToken + outputTokens * costPerOutputToken
  sessionCost += cost
  // 支援 advisor 模型遞迴計費、Fast Mode OTEL 標記
}

實作要點

  1. 每個任務設預算上限,累計追蹤,超限就停
  2. 未知模型用 fallback 費率 + analytics 警告
  3. Session 成本持久化到 project config(以 sessionId 隔離)

Prompt Cache 12 種 Break 原因

#原因影響防禦
1System prompt 內容變化全 bust靜態/動態邊界
2Tool schema 變化全 bust不排序工具陣列
3MCP 工具動態載入部分 bustDeferred loading
4Agent list 變化全 bust移到 attachment
5currentDate 跨日全 bust已知缺陷,應移到 dynamic zone
6Compaction 改寫訊息全 bust不可避免,但可用 fork 繼承
7Feature flag 切換全 bustSticky Latch
8Beta header 變化Server-side bustLatch 機制
9版本更新全 bust不可避免
10Permission mode 變化Tool schema bust避免 mid-session 切換
11Plugin/MCP 重載Tool bust避免 /reload-plugins
12使用者切換 cwdenv_info bust固定工作目錄

使用者端避坑清單

成本影響避法
Auto Mode + Max 訂閱每次工具操作用 Opus 分類,佔 15-28%關 Auto Mode 或改手動
Agent SDK 的 file modification injection每輪吃 5-15% context用 CLI;或壓短 session
跨午夜長 session整段 cache 全滅午夜前結束
頻繁開關 session每次 max_tokens:1 探測用長 session + /compact
裝太多 MCP server每輪注入不快取指令只裝需要的
工作中切目錄env_info bust cache固定目錄,用絕對路徑
大量改檔後繼續對話file modification diff 每輪注入改完 /compact 或開新 session
Resume 舊 sessionstale check 必觸發不 resume,開新的

Token Estimation 三策略

1. API countTokens(精確,需 API call)
2. Haiku fallback(近似,便宜)
3. length / 4(粗估,免費)
JSON 用 bytes / 2;圖片/PDF 固定 2000 tokens

Compaction 成本陷阱

  • 壓縮後第一輪以 125% 費率重建 cache(input token 全是 cache_creation)
  • 可觸發最多 3 次連鎖 compaction
  • Compaction 用的 forked agent 帶全部 36 工具(已知缺陷,應帶 0 個
  • NO_TOOLS_PREAMBLE 解決 Sonnet 4.6 壓縮時 2.79% 誤呼叫工具

參考

詳見 references/cache-break-causes.mdreferences/cost-matrix.md

Keep looking

Skills are one crate of 328,083. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.