AgentStack
Browse Sign in
Browse Why AgentStack Sell Docs
Sign in
SKILL verified MIT Self-run

Cc Cost Engineering

skill-cablate-ai-toolkit-cc-cost-engineering · by cablate

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

— No reviews yet
0 installs
31 views
0.0% view→install

Install

$ agentstack add skill-cablate-ai-toolkit-cc-cost-engineering

✓ scanned · ✓ verified, works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • ✓ Prompt-injection patterns
  • ✓ Secret / credential exfiltration
  • ✓ Dangerous shell & filesystem operations
  • ✓ Untrusted network calls
  • ✓ Known-malicious package signatures

What it can access

  • ✓ Network access No
  • ✓ Filesystem access No
  • ✓ Shell / process execution No
  • ✓ Environment & secrets No
  • ✓ Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

View the full security report →

Verified badge

Passed review? Show it. Paste this badge into your README, it links to the public security report.

AgentStack Verified badge Links to your public security report.
[![AgentStack Verified](https://agentstack.voostack.com/badges/verified.svg)](https://agentstack.voostack.com/security/report/skill-cablate-ai-toolkit-cc-cost-engineering)

Reliability & compatibility

✓ Security review passed
0 installs to date
— no reviews yet
○ 5mo ago

Declared compatibility

Claude CodeClaude Desktop

Compatibility is declared by the source manifest. End-to-end runtime verification is coming, see below.

Preview Execution monitoring

We're building live execution health for every listing: tool-call success rate, median latency, uptime, and last-checked timestamps, measured, not self-reported. It isn't live yet, so we don't show numbers we can't stand behind.

How agent discovery & health will work →
Are you the author of Cc Cost Engineering? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

AI Agent 成本工程

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

模型成本矩陣

| 模型 | Input/MTok | Output/MTok | 相對成本 | |------|-----------|-------------|----------| | Haiku 3.5 | $0.80 | $4.00 | 1x | | Sonnet 4.6 | $3.00 | $15.00 | 3.75x | | Opus 4.6 | $15.00 | $75.00 | 18.75x | | Opus 4.6 Fast | $30.00 | $150.00 | 37.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 原因

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

Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet, be the first.

Versions

  • v0.1.0 Imported from the upstream source.