Skip to content
cccskills
FreePublished on GitHub

cc-cost-engineering

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

The English summary is being prepared. Showing the original description.

See how to install

Files included (3)

  • SKILL.md3.6 KB
  • references/cache-break-causes.md981 B
  • references/cost-matrix.md897 B

SKILL.md (original)

Review the instructions the agent will receive before you install.

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.md 和 references/cost-matrix.md。

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

Agent Skill design knowledge base — mechanisms, philosophy, patterns, pitfalls. Use when: designing new skills, reviewing skill quality, or deciding whether something should be a skill.

The English summary is being prepared. Showing the original description.

cablate/ai-toolkit16Updated Apr 6, 2026

Claude Code 專案配置審計。觸發:review/優化 CLAUDE.md、skills、settings、定期清洗累積內容、新專案上線前檢查。

The English summary is being prepared. Showing the original description.

cablate/ai-toolkit16Updated Apr 6, 2026

Agent 配置設計指南 — 基於 Claude Code 6 個 built-in agent 的逆向分析。Use when: 設計新 agent、優化現有 agent prompt、決定工具/模型配置、撰寫 dispatch prompt。

The English summary is being prepared. Showing the original description.

cablate/ai-toolkit16Updated Apr 6, 2026

Harness Engineering 設計模式 — 基於 Claude Code 原始碼逆向分析的 12 條可遷移原則。Use when: 設計 agent 系統架構、實作 tool orchestration、設計 context 管理策略、建構 agent loop。

The English summary is being prepared. Showing the original description.

cablate/ai-toolkit16Updated Apr 6, 2026

System Prompt 工程 — 基於 Claude Code 914 行系統提示詞的逆向分析。Use when: 撰寫 system prompt、設計 prompt 動態組裝、最佳化 prompt cache 效率、撰寫安全指令。

The English summary is being prepared. Showing the original description.

cablate/ai-toolkit16Updated Apr 6, 2026

AI Agent 安全設計模式 — 基於 Claude Code 七層縱深防禦架構的逆向分析。Use when: 設計工具安全機制、實作命令過濾、建構權限模型、設計 sandbox。

The English summary is being prepared. Showing the original description.

cablate/ai-toolkit16Updated Apr 6, 2026

See all skills by cablate

Report a problem with this skill