[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6G291WCSY5FCXCFMBEXBN8T":3,"skill-same:01M6G291WCSY5FCXCFMBEXBN8T":53},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":24,"linkOnly":25,"markdownContent":26,"markdownBody":27,"frontMatter":28,"refName":30,"commitSha":31,"skillDirectory":32,"forksCount":33,"repositoryHtmlUrl":34,"repositoryHomepageUrl":6,"ownerHtmlUrl":35,"ownerBlogUrl":36,"ownerDescription":37,"updatedAt":38,"install":39,"resources":52},"01M6G291WCSY5FCXCFMBEXBN8T","cost-aware-llm-pipeline",null,"LLM API 使用成本优化模式 —— 基于任务复杂度的模型路由、预算跟踪、重试逻辑和提示缓存。","oabdelmaksoud\u002FAgention","oabdelmaksoud","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F50699149?v=4","01M6D5CVVN778ZCDP1WHS816SP","Omar Abdelmaksoud","01M6D8A4TE76V3DCSH3D94VG1V",2,"MIT","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fdocs\u002Fzh-CN\u002Fskills\u002Fcost-aware-llm-pipeline\u002FSKILL.md",[],[19,20,21,22,23],"pipeline","cost","cost aware llm pipeline","api","预算","2026-11-10 10:12:30",false,"---\nname: cost-aware-llm-pipeline\ndescription: LLM API 使用成本优化模式 —— 基于任务复杂度的模型路由、预算跟踪、重试逻辑和提示缓存。\norigin: ECC\n---\n\n# 成本感知型 LLM 流水线\n\n在保持质量的同时控制 LLM API 成本的模式。将模型路由、预算跟踪、重试逻辑和提示词缓存组合成一个可组合的流水线。\n\n## 何时激活\n\n* 构建调用 LLM API（Claude、GPT 等）的应用程序时\n* 处理具有不同复杂度的批量项目时\n* 需要将 API 支出控制在预算范围内时\n* 需要在复杂任务上优化成本而不牺牲质量时\n\n## 核心概念\n\n### 1. 根据任务复杂度进行模型路由\n\n自动为简单任务选择更便宜的模型，为复杂任务保留昂贵的模型。\n\n```python\nMODEL_SONNET = \"claude-sonnet-4-6\"\nMODEL_HAIKU = \"claude-haiku-4-5-20251001\"\n\n_SONNET_TEXT_THRESHOLD = 10_000  # chars\n_SONNET_ITEM_THRESHOLD = 30     # items\n\ndef select_model(\n    text_length: int,\n    item_count: int,\n    force_model: str | None = None,\n) -> str:\n    \"\"\"Select model based on task complexity.\"\"\"\n    if force_model is not None:\n        return force_model\n    if text_length >= _SONNET_TEXT_THRESHOLD or item_count >= _SONNET_ITEM_THRESHOLD:\n        return MODEL_SONNET  # Complex task\n    return MODEL_HAIKU  # Simple task (3-4x cheaper)\n```\n\n### 2. 不可变的成本跟踪\n\n使用冻结的数据类跟踪累计支出。每个 API 调用都会返回一个新的跟踪器 —— 永不改变状态。\n\n```python\nfrom dataclasses import dataclass\n\n@dataclass(frozen=True, slots=True)\nclass CostRecord:\n    model: str\n    input_tokens: int\n    output_tokens: int\n    cost_usd: float\n\n@dataclass(frozen=True, slots=True)\nclass CostTracker:\n    budget_limit: float = 1.00\n    records: tuple[CostRecord, ...] = ()\n\n    def add(self, record: CostRecord) -> \"CostTracker\":\n        \"\"\"Return new tracker with added record (never mutates self).\"\"\"\n        return CostTracker(\n            budget_limit=self.budget_limit,\n            records=(*self.records, record),\n        )\n\n    @property\n    def total_cost(self) -> float:\n        return sum(r.cost_usd for r in self.records)\n\n    @property\n    def over_budget(self) -> bool:\n        return self.total_cost > self.budget_limit\n```\n\n### 3. 窄范围重试逻辑\n\n仅在暂时性错误时重试。对于认证或错误请求错误，快速失败。\n\n```python\nfrom anthropic import (\n    APIConnectionError,\n    InternalServerError,\n    RateLimitError,\n)\n\n_RETRYABLE_ERRORS = (APIConnectionError, RateLimitError, InternalServerError)\n_MAX_RETRIES = 3\n\ndef call_with_retry(func, *, max_retries: int = _MAX_RETRIES):\n    \"\"\"Retry only on transient errors, fail fast on others.\"\"\"\n    for attempt in range(max_retries):\n        try:\n            return func()\n        except _RETRYABLE_ERRORS:\n            if attempt == max_retries - 1:\n                raise\n            time.sleep(2 ** attempt)  # Exponential backoff\n    # AuthenticationError, BadRequestError etc. → raise immediately\n```\n\n### 4. 提示词缓存\n\n缓存长的系统提示词，以避免在每个请求上重新发送它们。\n\n```python\nmessages = [\n    {\n        \"role\": \"user\",\n        \"content\": [\n            {\n                \"type\": \"text\",\n                \"text\": system_prompt,\n                \"cache_control\": {\"type\": \"ephemeral\"},  # Cache this\n            },\n            {\n                \"type\": \"text\",\n                \"text\": user_input,  # Variable part\n            },\n        ],\n    }\n]\n```\n\n## 组合\n\n将所有四种技术组合到一个流水线函数中：\n\n```python\ndef process(text: str, config: Config, tracker: CostTracker) -> tuple[Result, CostTracker]:\n    # 1. Route model\n    model = select_model(len(text), estimated_items, config.force_model)\n\n    # 2. Check budget\n    if tracker.over_budget:\n        raise BudgetExceededError(tracker.total_cost, tracker.budget_limit)\n\n    # 3. Call with retry + caching\n    response = call_with_retry(lambda: client.messages.create(\n        model=model,\n        messages=build_cached_messages(system_prompt, text),\n    ))\n\n    # 4. Track cost (immutable)\n    record = CostRecord(model=model, input_tokens=..., output_tokens=..., cost_usd=...)\n    tracker = tracker.add(record)\n\n    return parse_result(response), tracker\n```\n\n## 价格参考（2025-2026）\n\n| 模型 | 输入（美元\u002F百万令牌） | 输出（美元\u002F百万令牌） | 相对成本 |\n|-------|---------------------|----------------------|---------------|\n| Haiku 4.5 | $0.80 | $4.00 | 1x |\n| Sonnet 4.6 | $3.00 | $15.00 | ~4x |\n| Opus 4.5 | $15.00 | $75.00 | ~19x |\n\n## 最佳实践\n\n* **从最便宜的模型开始**，仅在达到复杂度阈值时才路由到昂贵的模型\n* **在处理批次之前设置明确的预算限制** —— 尽早失败而不是超支\n* **记录模型选择决策**，以便您可以根据实际数据调整阈值\n* **对于超过 1024 个令牌的系统提示词，使用提示词缓存** —— 既能节省成本，又能降低延迟\n* **切勿在认证或验证错误时重试** —— 仅针对暂时性故障（网络、速率限制、服务器错误）重试\n\n## 应避免的反模式\n\n* 无论复杂度如何，对所有请求都使用最昂贵的模型\n* 对所有错误都进行重试（在永久性故障上浪费预算）\n* 改变成本跟踪状态（使调试和审计变得困难）\n* 在整个代码库中硬编码模型名称（使用常量或配置）\n* 对重复的系统提示词忽略提示词缓存\n\n## 适用场景\n\n* 任何调用 Claude、OpenAI 或类似 LLM API 的应用程序\n* 成本快速累积的批处理流水线\n* 需要智能路由的多模型架构\n* 需要预算护栏的生产系统\n","# 成本感知型 LLM 流水线\n\n在保持质量的同时控制 LLM API 成本的模式。将模型路由、预算跟踪、重试逻辑和提示词缓存组合成一个可组合的流水线。\n\n## 何时激活\n\n* 构建调用 LLM API（Claude、GPT 等）的应用程序时\n* 处理具有不同复杂度的批量项目时\n* 需要将 API 支出控制在预算范围内时\n* 需要在复杂任务上优化成本而不牺牲质量时\n\n## 核心概念\n\n### 1. 根据任务复杂度进行模型路由\n\n自动为简单任务选择更便宜的模型，为复杂任务保留昂贵的模型。\n\n```python\nMODEL_SONNET = \"claude-sonnet-4-6\"\nMODEL_HAIKU = \"claude-haiku-4-5-20251001\"\n\n_SONNET_TEXT_THRESHOLD = 10_000  # chars\n_SONNET_ITEM_THRESHOLD = 30     # items\n\ndef select_model(\n    text_length: int,\n    item_count: int,\n    force_model: str | None = None,\n) -> str:\n    \"\"\"Select model based on task complexity.\"\"\"\n    if force_model is not None:\n        return force_model\n    if text_length >= _SONNET_TEXT_THRESHOLD or item_count >= _SONNET_ITEM_THRESHOLD:\n        return MODEL_SONNET  # Complex task\n    return MODEL_HAIKU  # Simple task (3-4x cheaper)\n```\n\n### 2. 不可变的成本跟踪\n\n使用冻结的数据类跟踪累计支出。每个 API 调用都会返回一个新的跟踪器 —— 永不改变状态。\n\n```python\nfrom dataclasses import dataclass\n\n@dataclass(frozen=True, slots=True)\nclass CostRecord:\n    model: str\n    input_tokens: int\n    output_tokens: int\n    cost_usd: float\n\n@dataclass(frozen=True, slots=True)\nclass CostTracker:\n    budget_limit: float = 1.00\n    records: tuple[CostRecord, ...] = ()\n\n    def add(self, record: CostRecord) -> \"CostTracker\":\n        \"\"\"Return new tracker with added record (never mutates self).\"\"\"\n        return CostTracker(\n            budget_limit=self.budget_limit,\n            records=(*self.records, record),\n        )\n\n    @property\n    def total_cost(self) -> float:\n        return sum(r.cost_usd for r in self.records)\n\n    @property\n    def over_budget(self) -> bool:\n        return self.total_cost > self.budget_limit\n```\n\n### 3. 窄范围重试逻辑\n\n仅在暂时性错误时重试。对于认证或错误请求错误，快速失败。\n\n```python\nfrom anthropic import (\n    APIConnectionError,\n    InternalServerError,\n    RateLimitError,\n)\n\n_RETRYABLE_ERRORS = (APIConnectionError, RateLimitError, InternalServerError)\n_MAX_RETRIES = 3\n\ndef call_with_retry(func, *, max_retries: int = _MAX_RETRIES):\n    \"\"\"Retry only on transient errors, fail fast on others.\"\"\"\n    for attempt in range(max_retries):\n        try:\n            return func()\n        except _RETRYABLE_ERRORS:\n            if attempt == max_retries - 1:\n                raise\n            time.sleep(2 ** attempt)  # Exponential backoff\n    # AuthenticationError, BadRequestError etc. → raise immediately\n```\n\n### 4. 提示词缓存\n\n缓存长的系统提示词，以避免在每个请求上重新发送它们。\n\n```python\nmessages = [\n    {\n        \"role\": \"user\",\n        \"content\": [\n            {\n                \"type\": \"text\",\n                \"text\": system_prompt,\n                \"cache_control\": {\"type\": \"ephemeral\"},  # Cache this\n            },\n            {\n                \"type\": \"text\",\n                \"text\": user_input,  # Variable part\n            },\n        ],\n    }\n]\n```\n\n## 组合\n\n将所有四种技术组合到一个流水线函数中：\n\n```python\ndef process(text: str, config: Config, tracker: CostTracker) -> tuple[Result, CostTracker]:\n    # 1. Route model\n    model = select_model(len(text), estimated_items, config.force_model)\n\n    # 2. Check budget\n    if tracker.over_budget:\n        raise BudgetExceededError(tracker.total_cost, tracker.budget_limit)\n\n    # 3. Call with retry + caching\n    response = call_with_retry(lambda: client.messages.create(\n        model=model,\n        messages=build_cached_messages(system_prompt, text),\n    ))\n\n    # 4. Track cost (immutable)\n    record = CostRecord(model=model, input_tokens=..., output_tokens=..., cost_usd=...)\n    tracker = tracker.add(record)\n\n    return parse_result(response), tracker\n```\n\n## 价格参考（2025-2026）\n\n| 模型 | 输入（美元\u002F百万令牌） | 输出（美元\u002F百万令牌） | 相对成本 |\n|-------|---------------------|----------------------|---------------|\n| Haiku 4.5 | $0.80 | $4.00 | 1x |\n| Sonnet 4.6 | $3.00 | $15.00 | ~4x |\n| Opus 4.5 | $15.00 | $75.00 | ~19x |\n\n## 最佳实践\n\n* **从最便宜的模型开始**，仅在达到复杂度阈值时才路由到昂贵的模型\n* **在处理批次之前设置明确的预算限制** —— 尽早失败而不是超支\n* **记录模型选择决策**，以便您可以根据实际数据调整阈值\n* **对于超过 1024 个令牌的系统提示词，使用提示词缓存** —— 既能节省成本，又能降低延迟\n* **切勿在认证或验证错误时重试** —— 仅针对暂时性故障（网络、速率限制、服务器错误）重试\n\n## 应避免的反模式\n\n* 无论复杂度如何，对所有请求都使用最昂贵的模型\n* 对所有错误都进行重试（在永久性故障上浪费预算）\n* 改变成本跟踪状态（使调试和审计变得困难）\n* 在整个代码库中硬编码模型名称（使用常量或配置）\n* 对重复的系统提示词忽略提示词缓存\n\n## 适用场景\n\n* 任何调用 Claude、OpenAI 或类似 LLM API 的应用程序\n* 成本快速累积的批处理流水线\n* 需要智能路由的多模型架构\n* 需要预算护栏的生产系统\n",{"name":5,"origin":29,"description":7},"ECC","main","a318f48de3c5718e8ea2e9f6fae7790e8a759e85","upstream-sync\u002Fwork\u002Fecc\u002Fdocs\u002Fzh-CN\u002Fskills\u002Fcost-aware-llm-pipeline",0,"https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud","https:\u002F\u002Fwww.linkedin.com\u002Fin\u002Fo-abdelmaksoud\u002F","Full-Stack Engineer | AI Systems Architect | Building autonomous multi-agent systems","2026-03-08 21:23:03",{"source":8,"skillName":5,"commands":40,"prompt":50,"zipAvailable":51},[41,44,47],{"tool":42,"command":43},"npx","npx skills add oabdelmaksoud\u002FAgention --skill cost-aware-llm-pipeline",{"tool":45,"command":46},"bunx","bunx skills add oabdelmaksoud\u002FAgention --skill cost-aware-llm-pipeline",{"tool":48,"command":49},"pnpm","pnpm dlx skills add oabdelmaksoud\u002FAgention --skill cost-aware-llm-pipeline","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fdocs\u002Fzh-CN\u002Fskills\u002Fcost-aware-llm-pipeline\u002FSKILL.md",true,[],[54,78,92,125,151,171],{"skillNo":55,"name":56,"nameZh":6,"description":57,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":58,"categories":59,"keywords":65,"publishedAt":24},"01M6G2QFCBNBYAJF2KTK85G9A6","visa-doc-translate","Translate visa application documents (images) to English and create a bilingual PDF with original and translation","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fvisa-doc-translate\u002FSKILL.md",[60],{"categoryNo":61,"categoryName":62,"taxonomyType":63,"taxonomyTypeDesc":64},"01JBSEEDCAT000000000000002","文档处理","SCENARIO","使用场景",[66,67,68,69,70,71,72,73,74,75,76,77],"translate","doc","visa doc translate","bilingual","with","english","application","PDF","translation","documents","create","images",{"skillNo":79,"name":80,"nameZh":6,"description":81,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":82,"categories":83,"keywords":84,"publishedAt":24},"01M6G2QB09TSK6BY4ME5X20EVQ","verification-loop","A comprehensive verification system for Claude Code sessions.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fverification-loop\u002FSKILL.md",[],[85,86,87,88,89,90,91],"verification","loop","verification loop","sessions","code","claude code sessions","system",{"skillNo":93,"name":94,"nameZh":6,"description":95,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":96,"categories":97,"keywords":109,"publishedAt":24},"01M6G2Q6V2F7G03VAAF27FXPJR","tdd-workflow","Use this skill when writing new features, fixing bugs, or refactoring code. Enforces test-driven development with 80%+ coverage including unit, integration, and E2E tests.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Ftdd-workflow\u002FSKILL.md",[98,101,104],{"categoryNo":99,"categoryName":100,"taxonomyType":63,"taxonomyTypeDesc":64},"01JBSEEDCAT000000000000008","测试与质量",{"categoryNo":102,"categoryName":103,"taxonomyType":63,"taxonomyTypeDesc":64},"01JBSEEDCAT000000000000023","Skill与智能体开发",{"categoryNo":105,"categoryName":106,"taxonomyType":107,"taxonomyTypeDesc":108},"01JBSEEDCAT000000000000030","开发工程师","PROFESSION","适用职业",[110,111,112,113,114,115,116,117,118,89,119,120,121,122,70,123,124],"workflow","tdd","tdd workflow","use","unit","coverage","refactoring","e2e","integration","development","e2e tests","tests","driven","test","writing",{"skillNo":126,"name":127,"nameZh":6,"description":128,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":129,"categories":130,"keywords":131,"publishedAt":24},"01M6G2Q359YJRWAR2PFCEQAXF3","swiftui-patterns","SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS\u002FmacOS UI best practices.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswiftui-patterns\u002FSKILL.md",[],[132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,70],"patterns","swiftui","swiftui patterns","architecture","macos","performance","state management","best practices","state","ui","Optimization","ios","architecture patterns","Composition","performance optimization","view","management","swiftui architecture","navigation",{"skillNo":152,"name":153,"nameZh":6,"description":154,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":155,"categories":156,"keywords":158,"publishedAt":24},"01M6G2PY8B2FWZTWPZ84H83Y7C","swift-protocol-di-testing","Protocol-based dependency injection for testable Swift code — mock file system, network, and external APIs using focused protocols and Swift Testing.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-protocol-di-testing\u002FSKILL.md",[157],{"categoryNo":99,"categoryName":100,"taxonomyType":63,"taxonomyTypeDesc":64},[159,160,161,162,89,163,164,165,166,91,167,168,169,170],"swift protocol di testing","protocol","swift","testing","protocols","file","network","injection","dependency","swift testing","dependency injection","mock",{"skillNo":172,"name":173,"nameZh":6,"description":174,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":175,"categories":176,"keywords":177,"publishedAt":24},"01M6G2PSCN2GXV9FFZDRRRWVB6","swift-concurrency-6-2","Swift 6.2 Approachable Concurrency — single-threaded by default, @concurrent for explicit background offloading, isolated conformances for main actor types.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-concurrency-6-2\u002FSKILL.md",[],[178,179,180,161,181,30,182],"concurrency","swift concurrency","swift concurrency 6 2","default","types"]