[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M70FR8GFABFCHKQ4JWJCX625":3,"skill-same:01M70FR8GFABFCHKQ4JWJCX625":78},{"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":28,"publishedAt":49,"linkOnly":50,"markdownContent":51,"markdownBody":52,"frontMatter":53,"refName":56,"commitSha":57,"skillDirectory":58,"forksCount":59,"repositoryHtmlUrl":60,"repositoryHomepageUrl":6,"ownerHtmlUrl":61,"ownerBlogUrl":6,"ownerDescription":62,"updatedAt":63,"install":64,"resources":77},"01M70FR8GFABFCHKQ4JWJCX625","cost-aware-llm-pipeline",null,"Cost optimization patterns for LLM API usage — model routing by task complexity, budget tracking, retry logic, and prompt caching. Use when LLM spend needs to come down, or when routing tasks across model tiers and budgets.","mdnaimul22\u002Fhuman-skills","mdnaimul22","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F64520358?v=4","01M70FH95MH2H796Q01TYSFZ03","Naimul","01M70FH96798CQR4YHA13F17GA",3,"MIT","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Faffaan-m_ECC\u002Fcost-aware-llm-pipeline\u002FSKILL.md",[18,23],{"categoryNo":19,"categoryName":20,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000023","Skill与智能体开发","SCENARIO","使用场景",{"categoryNo":24,"categoryName":25,"taxonomyType":26,"taxonomyTypeDesc":27},"01JBSEEDCAT000000000000030","开发工程师","PROFESSION","适用职业",[29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48],"cost aware llm pipeline","cost","pipeline","llm api","cost optimization","Optimization","for","down","usage","patterns","Budget","across","api","tasks","caching","when","model","retry","prompt caching","complexity","2026-11-10 10:12:30",false,"---\nname: cost-aware-llm-pipeline\ndescription: Cost optimization patterns for LLM API usage — model routing by task complexity, budget tracking, retry logic, and prompt caching. Use when LLM spend needs to come down, or when routing tasks across model tiers and budgets.\nmetadata:\n  origin: ECC\n---\n\n# Cost-Aware LLM Pipeline\n\nPatterns for controlling LLM API costs while maintaining quality. Combines model routing, budget tracking, retry logic, and prompt caching into a composable pipeline.\n\n## When to Activate\n\n- Building applications that call LLM APIs (Claude, GPT, etc.)\n- Processing batches of items with varying complexity\n- Need to stay within a budget for API spend\n- Optimizing cost without sacrificing quality on complex tasks\n\n## Core Concepts\n\n### 1. Model Routing by Task Complexity\n\nAutomatically select cheaper models for simple tasks, reserving expensive models for complex ones.\n\n```python\nMODEL_SONNET = \"claude-sonnet-5\"\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. Immutable Cost Tracking\n\nTrack cumulative spend with frozen dataclasses. Each API call returns a new tracker — never mutates state.\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. Narrow Retry Logic\n\nRetry only on transient errors. Fail fast on authentication or bad request errors.\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. Prompt Caching\n\nCache long system prompts to avoid resending them on every request.\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## Composition\n\nCombine all four techniques in a single pipeline function:\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## Pricing Reference (2026)\n\n| Model | Input ($\u002F1M tokens) | Output ($\u002F1M tokens) | Relative Cost |\n|-------|---------------------|----------------------|---------------|\n| Haiku 3.5 (legacy) | $0.80 | $4.00 | 0.8x |\n| Haiku 4.5 | $1.00 | $5.00 | 1x |\n| Sonnet 5 | $2.00 | $10.00 | 2x |\n| Sonnet 4.6 | $3.00 | $15.00 | 3x |\n| Opus 4.8 | $5.00 | $25.00 | 5x |\n| Fable 5 \u002F Mythos 5 | $10.00 | $50.00 | 10x |\n| Opus 4.0 \u002F 4.1 (legacy) | $15.00 | $75.00 | 15x |\n\n## Best Practices\n\n- **Start with the cheapest model** and only route to expensive models when complexity thresholds are met\n- **Set explicit budget limits** before processing batches — fail early rather than overspend\n- **Log model selection decisions** so you can tune thresholds based on real data\n- **Use prompt caching** for system prompts over 1024 tokens — saves both cost and latency\n- **Never retry on authentication or validation errors** — only transient failures (network, rate limit, server error)\n\n## Anti-Patterns to Avoid\n\n- Using the most expensive model for all requests regardless of complexity\n- Retrying on all errors (wastes budget on permanent failures)\n- Mutating cost tracking state (makes debugging and auditing difficult)\n- Hardcoding model names throughout the codebase (use constants or config)\n- Ignoring prompt caching for repetitive system prompts\n\n## When to Use\n\n- Any application calling Claude, OpenAI, or similar LLM APIs\n- Batch processing pipelines where cost adds up quickly\n- Multi-model architectures that need intelligent routing\n- Production systems that need budget guardrails\n","# Cost-Aware LLM Pipeline\n\nPatterns for controlling LLM API costs while maintaining quality. Combines model routing, budget tracking, retry logic, and prompt caching into a composable pipeline.\n\n## When to Activate\n\n- Building applications that call LLM APIs (Claude, GPT, etc.)\n- Processing batches of items with varying complexity\n- Need to stay within a budget for API spend\n- Optimizing cost without sacrificing quality on complex tasks\n\n## Core Concepts\n\n### 1. Model Routing by Task Complexity\n\nAutomatically select cheaper models for simple tasks, reserving expensive models for complex ones.\n\n```python\nMODEL_SONNET = \"claude-sonnet-5\"\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. Immutable Cost Tracking\n\nTrack cumulative spend with frozen dataclasses. Each API call returns a new tracker — never mutates state.\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. Narrow Retry Logic\n\nRetry only on transient errors. Fail fast on authentication or bad request errors.\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. Prompt Caching\n\nCache long system prompts to avoid resending them on every request.\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## Composition\n\nCombine all four techniques in a single pipeline function:\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## Pricing Reference (2026)\n\n| Model | Input ($\u002F1M tokens) | Output ($\u002F1M tokens) | Relative Cost |\n|-------|---------------------|----------------------|---------------|\n| Haiku 3.5 (legacy) | $0.80 | $4.00 | 0.8x |\n| Haiku 4.5 | $1.00 | $5.00 | 1x |\n| Sonnet 5 | $2.00 | $10.00 | 2x |\n| Sonnet 4.6 | $3.00 | $15.00 | 3x |\n| Opus 4.8 | $5.00 | $25.00 | 5x |\n| Fable 5 \u002F Mythos 5 | $10.00 | $50.00 | 10x |\n| Opus 4.0 \u002F 4.1 (legacy) | $15.00 | $75.00 | 15x |\n\n## Best Practices\n\n- **Start with the cheapest model** and only route to expensive models when complexity thresholds are met\n- **Set explicit budget limits** before processing batches — fail early rather than overspend\n- **Log model selection decisions** so you can tune thresholds based on real data\n- **Use prompt caching** for system prompts over 1024 tokens — saves both cost and latency\n- **Never retry on authentication or validation errors** — only transient failures (network, rate limit, server error)\n\n## Anti-Patterns to Avoid\n\n- Using the most expensive model for all requests regardless of complexity\n- Retrying on all errors (wastes budget on permanent failures)\n- Mutating cost tracking state (makes debugging and auditing difficult)\n- Hardcoding model names throughout the codebase (use constants or config)\n- Ignoring prompt caching for repetitive system prompts\n\n## When to Use\n\n- Any application calling Claude, OpenAI, or similar LLM APIs\n- Batch processing pipelines where cost adds up quickly\n- Multi-model architectures that need intelligent routing\n- Production systems that need budget guardrails\n",{"name":5,"metadata":54,"description":7},{"origin":55},"ECC","main","74715deacdc69119d739f38916f8ee4f0f979ebf","skills\u002Fstorage\u002Faffaan-m_ECC\u002Fcost-aware-llm-pipeline",0,"https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills","https:\u002F\u002Fgithub.com\u002Fmdnaimul22","AI Agents Workflows Automation | Specializing in Python, FastAPI, MCP Agents & Custom ERP.","2026-11-11 21:16:57",{"source":8,"skillName":5,"commands":65,"prompt":75,"zipAvailable":76},[66,69,72],{"tool":67,"command":68},"npx","npx skills add mdnaimul22\u002Fhuman-skills --skill cost-aware-llm-pipeline",{"tool":70,"command":71},"bunx","bunx skills add mdnaimul22\u002Fhuman-skills --skill cost-aware-llm-pipeline",{"tool":73,"command":74},"pnpm","pnpm dlx skills add mdnaimul22\u002Fhuman-skills --skill cost-aware-llm-pipeline","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Faffaan-m_ECC\u002Fcost-aware-llm-pipeline\u002FSKILL.md",true,[],[79,87,112,131,150,165],{"skillNo":80,"name":81,"nameZh":6,"description":82,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":83,"categories":84,"keywords":85,"publishedAt":86},"01M72JT3YP1SQHD0MANGDXQ4RT","higgsfield-websites","Build, edit, and deploy full-stack websites, apps and games via the Higgsfield CLI (`higgsfield website …`). Each is a React 19 + TanStack Start SSR app in one Cloudflare Worker (D1\u002FR2\u002FKV\u002FDO\u002FContainers). THREE product types, picked via `--type` on create: `website` (standalone, no Higgsfield integration — references\u002Fwebsite-flow.md), `app` (Sign in with Higgsfield + fnf SDK, Quanta — references\u002Fapp-flow.md), `game` (realtime multiplayer rooms — references\u002Fgame-flow.md). Routes to the right flow; each carries its own rules and deploy\u002Fpublish gates.\nUse when: \"build me a website\", \"make a landing page\", \"create a web app\", \"build a SaaS dashboard \u002F portfolio\", \"make me a game\", \"deploy this site\", \"publish\". Also owns GAME ART: \"make a spritesheet\", \"tileable texture\", \"animate a 3D character\", game music\u002FSFX — see the game-* references.\nNOT for: single image\u002Fvideo\u002Faudio generation (higgsfield-generate), product photos (higgsfield-product-photoshoot), marketplace cards (higgsfield-marketplace-cards).","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Fhiggsfield-ai_skills\u002Fhiggsfield-websites\u002FSKILL.md",[],[],"2026-11-10 22:39:42",{"skillNo":88,"name":89,"nameZh":6,"description":90,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":91,"categories":92,"keywords":103,"publishedAt":49},"01M70JPK8HAXXA85P7Q8NKZZYG","writing-skills","Use when creating new skills, editing existing skills, or verifying skills work before deployment","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Fobra_superpowers\u002Fwriting-skills\u002FSKILL.md",[93,96,99,100],{"categoryNo":94,"categoryName":95,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000018","写作与文案",{"categoryNo":97,"categoryName":98,"taxonomyType":26,"taxonomyTypeDesc":27},"01JBSEEDCAT000000000000036","内容创作者",{"categoryNo":19,"categoryName":20,"taxonomyType":21,"taxonomyTypeDesc":22},{"categoryNo":101,"categoryName":102,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000008","测试与质量",[104,105,106,107,108,109,110,44,111],"writing","writing skills","editing","deployment","use","Existing","new","work",{"skillNo":113,"name":114,"nameZh":6,"description":115,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":116,"categories":117,"keywords":121,"publishedAt":49},"01M70JP4PHBH1CQVVDWZ94W159","writing-plans","Use when you have a spec or requirements for a multi-step task, before touching code","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Fobra_superpowers\u002Fwriting-plans\u002FSKILL.md",[118,119,120],{"categoryNo":94,"categoryName":95,"taxonomyType":21,"taxonomyTypeDesc":22},{"categoryNo":97,"categoryName":98,"taxonomyType":26,"taxonomyTypeDesc":27},{"categoryNo":24,"categoryName":25,"taxonomyType":26,"taxonomyTypeDesc":27},[104,122,123,124,44,125,35,108,126,127,128,129,130],"writing plans","plans","you","code","requirements","task","multi","spec","multi-step task",{"skillNo":132,"name":133,"nameZh":6,"description":134,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":135,"categories":136,"keywords":137,"publishedAt":49},"01M70JNTYVGXFZT8PASZ844TH8","verification-before-completion","Use when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Fobra_superpowers\u002Fverification-before-completion\u002FSKILL.md",[],[138,139,140,141,142,143,108,144,145,146,44,147,111,148,149],"verification","verification before completion","complete","about","output","assertions","prs","evidence","claim","claims","commands","running",{"skillNo":151,"name":152,"nameZh":6,"description":153,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":154,"categories":155,"keywords":157,"publishedAt":49},"01M70JNGVSQDESWN0Q5BPJ875V","using-superpowers","Use when starting any conversation - establishes how to find and use skills, requiring skill invocation before ANY response including clarifying questions","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Fobra_superpowers\u002Fusing-superpowers\u002FSKILL.md",[156],{"categoryNo":19,"categoryName":20,"taxonomyType":21,"taxonomyTypeDesc":22},[158,159,160,161,108,44,162,163,164],"using superpowers","superpowers","conversation","how to","how","find","questions",{"skillNo":166,"name":167,"nameZh":6,"description":168,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":169,"categories":170,"keywords":171,"publishedAt":49},"01M70JN57CE588NPYPYTWH1DWG","using-git-worktrees","Use when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Fobra_superpowers\u002Fusing-git-worktrees\u002FSKILL.md",[],[172,173,174,175,176,177,44,111,178,179,180,181,182,183,184,123,108,185,186],"using git worktrees","Using git","git","worktrees","git worktrees","implementation","git worktree","worktree","native","Tools","isolation","fallback","feature","via","workspace"]