/cost-aware-llm-pipeline
LLM API 使用成本优化模式——基于任务复杂度的模型路由、预算跟踪、重试逻辑和提示词缓存。
$ npx -y skills add xu-xiang/everything-claude-code-zh --skill cost-aware-llm-pipeline --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/cost-aware-llm-pipeline
Context preview
The summary Claude sees to decide when to auto-load this skill.
LLM API 使用成本优化模式——基于任务复杂度的模型路由、预算跟踪、重试逻辑和提示词缓存。
SKILL.md
cost-aware-llm-pipeline.SKILL.mdname: cost-aware-llm-pipeline
description: LLM API 使用成本优化模式——基于任务复杂度的模型路由、预算跟踪、重试逻辑和提示词缓存。
origin: ECC
成本感知型 LLM 流水线 (Cost-Aware LLM Pipeline)
在保持质量的同时控制 LLM API 成本的模式。将模型路由 (Model Routing)、预算跟踪 (Budget Tracking)、重试逻辑 (Retry Logic) 和提示词缓存 (Prompt Caching) 组合成一个可复用的流水线。
何时启用
- 构建调用 LLM API(Claude、GPT 等)的应用
- 处理具有不同复杂度的批量项目
- 需要将 API 支出控制在预算范围内
- 在不牺牲复杂任务质量的前提下优化成本
核心概念
1. 基于任务复杂度的模型路由 (Model Routing)
为简单任务自动选择更便宜的模型,将昂贵的模型留给复杂任务。
MODEL_SONNET = "claude-sonnet-4-6"
MODEL_HAIKU = "claude-haiku-4-5-20251001"
_SONNET_TEXT_THRESHOLD = 10_000 # 字符数阈值
_SONNET_ITEM_THRESHOLD = 30 # 项目数阈值
def select_model(
text_length: int,
item_count: int,
force_model: str | None = None,
) -> str:
"""根据任务复杂度选择模型。"""
if force_model is not None:
return force_model
if text_length >= _SONNET_TEXT_THRESHOLD or item_count >= _SONNET_ITEM_THRESHOLD:
return MODEL_SONNET # 复杂任务
return MODEL_HAIKU # 简单任务 (便宜 3-4 倍)2. 不可变成本跟踪 (Immutable Cost Tracking)
使用冻结的数据类 (Frozen Dataclasses) 跟踪累计支出。每次 API 调用都会返回一个新的跟踪器——绝不修改原始状态。
from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class CostRecord:
model: str
input_tokens: int
output_tokens: int
cost_usd: float
@dataclass(frozen=True, slots=True)
class CostTracker:
budget_limit: float = 1.00
records: tuple[CostRecord, ...] = ()
def add(self, record: CostRecord) -> "CostTracker":
"""返回添加了新记录的新跟踪器 (绝不修改自身状态)。"""
return CostTracker(
budget_limit=self.budget_limit,
records=(*self.records, record),
)
@property
def total_cost(self) -> float:
return sum(r.cost_usd for r in self.records)
@property
def over_budget(self) -> bool:
return self.total_cost > self.budget_limit3. 精细化重试逻辑 (Narrow Retry Logic)
仅在瞬时错误 (Transient Errors) 时重试。对身份验证或错误请求执行快速失败 (Fail Fast)。
from anthropic import (
APIConnectionError,
InternalServerError,
RateLimitError,
)
_RETRYABLE_ERRORS = (APIConnectionError, RateLimitError, InternalServerError)
_MAX_RETRIES = 3
def call_with_retry(func, *, max_retries: int = _MAX_RETRIES):
"""仅在瞬时错误时重试,其他错误立即报错。"""
for attempt in range(max_retries):
try:
return func()
except _RETRYABLE_ERRORS:
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt) # 指数退避 (Exponential backoff)
# AuthenticationError, BadRequestError 等 -> 立即抛出异常4. 提示词缓存 (Prompt Caching)
缓存较长的系统提示词 (System Prompts),避免在每次请求时重复发送。
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": system_prompt,
"cache_control": {"type": "ephemeral"}, # 缓存此内容
},
{
"type": "text",
"text": user_input, # 变量部分
},
],
}
]组合使用
在单个流水线函数中组合所有四项技术:
def process(text: str, config: Config, tracker: CostTracker) -> tuple[Result, CostTracker]:
# 1. 路由模型
model = select_model(len(text), estimated_items, config.force_model)
# 2. 检查预算
if tracker.over_budget:
raise BudgetExceededError(tracker.total_cost, tracker.budget_limit)
# 3. 带重试与缓存的调用
response = call_with_retry(lambda: client.messages.create(
model=model,
messages=build_cached_messages(system_prompt, text),
))
# 4. 跟踪成本 (不可变模式)
record = CostRecord(model=model, input_tokens=..., output_tokens=..., cost_usd=...)
tracker = tracker.add(record)
return parse_result(response), tracker价格参考 (2025-2026)
| 模型 | 输入 ($/1M tokens) | 输出 ($/1M tokens) | 相对成本 | |-------|---------------------|----------------------|---------------| | Haiku 4.5 | $0.80 | $4.00 | 1x | | Sonnet 4.6 | $3.00 | $15.00 | ~4x | | Opus 4.5 | $15.00 | $75.00 | ~19x |
最佳实践
- **从最便宜的模型开始**,仅在达到复杂度阈值时路由到昂贵模型。
- **在批量处理前设置明确的预算限制**——宁愿提前失败也不要超支。
- **记录模型选择决策**,以便根据真实数据调整阈值。
- **为超过 1024 tokens 的系统提示词使用提示词缓存**——既能节省成本又能降低延迟。
- **绝不在身份验证或校验错误时重试**——仅重试瞬时故障(网络、频率限制、服务器错误)。
应避免的反模式 (Anti-Patterns)
- 不分复杂度,对所有请求都使用最昂贵的模型。
- 对所有错误进行重试(在永久性失败上浪费预算)。
- 修改成本跟踪状态(增加调试和审计难度)。
- 在整个代码库中硬编码模型名称(应使用常量或配置)。
- 忽视对重复系统提示词的提示词缓存。
使用场景
- 任何调用 Claude、OpenAI 或类似 LLM API 的应用。
- 成本累积迅速的批量处理流水线。
- 需要智能路由的多模型架构。
- 需要预算安全护栏 (Guardrails) 的生产系统。
Read more
name: cost-aware-llm-pipeline description: LLM API 使用成本优化模式——基于任务复杂度的模型路由、预算跟踪、重试逻辑和提示词缓存。 origin: ECC
成本感知型 LLM 流水线 (Cost-Aware LLM Pipeline)
在保持质量的同时控制 LLM API 成本的模式。将模型路由 (Model Routing)、预算跟踪 (Budget Tracking)、重试逻辑 (Retry Logic) 和提示词缓存 (Prompt Caching) 组合成一个可复用的流水线。
何时启用
- 构建调用 LLM API(Claude、GPT 等)的应用
- 处理具有不同复杂度的批量项目
- 需要将 API 支出控制在预算范围内
- 在不牺牲复杂任务质量的前提下优化成本
核心概念
1. 基于任务复杂度的模型路由 (Model Routing)
为简单任务自动选择更便宜的模型,将昂贵的模型留给复杂任务。
MODEL_SONNET = "claude-sonnet-4-6"
MODEL_HAIKU = "claude-haiku-4-5-20251001"
_SONNET_TEXT_THRESHOLD = 10_000 # 字符数阈值
_SONNET_ITEM_THRESHOLD = 30 # 项目数阈值
def select_model(
text_length: int,
item_count: int,
force_model: str | None = None,
) -> str:
"""根据任务复杂度选择模型。"""
if force_model is not None:
return force_model
if text_length >= _SONNET_TEXT_THRESHOLD or item_count >= _SONNET_ITEM_THRESHOLD:
return MODEL_SONNET # 复杂任务
return MODEL_HAIKU # 简单任务 (便宜 3-4 倍)2. 不可变成本跟踪 (Immutable Cost Tracking)
使用冻结的数据类 (Frozen Dataclasses) 跟踪累计支出。每次 API 调用都会返回一个新的跟踪器——绝不修改原始状态。
from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class CostRecord:
model: str
input_tokens: int
output_tokens: int
cost_usd: float
@dataclass(frozen=True, slots=True)
class CostTracker:
budget_limit: float = 1.00
records: tuple[CostRecord, ...] = ()
def add(self, record: CostRecord) -> "CostTracker":
"""返回添加了新记录的新跟踪器 (绝不修改自身状态)。"""
return CostTracker(
budget_limit=self.budget_limit,
records=(*self.records, record),
)
@property
def total_cost(self) -> float:
return sum(r.cost_usd for r in self.records)
@property
def over_budget(self) -> bool:
return self.total_cost > self.budget_limit3. 精细化重试逻辑 (Narrow Retry Logic)
仅在瞬时错误 (Transient Errors) 时重试。对身份验证或错误请求执行快速失败 (Fail Fast)。
from anthropic import (
APIConnectionError,
InternalServerError,
RateLimitError,
)
_RETRYABLE_ERRORS = (APIConnectionError, RateLimitError, InternalServerError)
_MAX_RETRIES = 3
def call_with_retry(func, *, max_retries: int = _MAX_RETRIES):
"""仅在瞬时错误时重试,其他错误立即报错。"""
for attempt in range(max_retries):
try:
return func()
except _RETRYABLE_ERRORS:
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt) # 指数退避 (Exponential backoff)
# AuthenticationError, BadRequestError 等 -> 立即抛出异常4. 提示词缓存 (Prompt Caching)
缓存较长的系统提示词 (System Prompts),避免在每次请求时重复发送。
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": system_prompt,
"cache_control": {"type": "ephemeral"}, # 缓存此内容
},
{
"type": "text",
"text": user_input, # 变量部分
},
],
}
]组合使用
在单个流水线函数中组合所有四项技术:
def process(text: str, config: Config, tracker: CostTracker) -> tuple[Result, CostTracker]:
# 1. 路由模型
model = select_model(len(text), estimated_items, config.force_model)
# 2. 检查预算
if tracker.over_budget:
raise BudgetExceededError(tracker.total_cost, tracker.budget_limit)
# 3. 带重试与缓存的调用
response = call_with_retry(lambda: client.messages.create(
model=model,
messages=build_cached_messages(system_prompt, text),
))
# 4. 跟踪成本 (不可变模式)
record = CostRecord(model=model, input_tokens=..., output_tokens=..., cost_usd=...)
tracker = tracker.add(record)
return parse_result(response), tracker价格参考 (2025-2026)
| 模型 | 输入 ($/1M tokens) | 输出 ($/1M tokens) | 相对成本 | |-------|---------------------|----------------------|---------------| | Haiku 4.5 | $0.80 | $4.00 | 1x | | Sonnet 4.6 | $3.00 | $15.00 | ~4x | | Opus 4.5 | $15.00 | $75.00 | ~19x |
最佳实践
- **从最便宜的模型开始**,仅在达到复杂度阈值时路由到昂贵模型。
- **在批量处理前设置明确的预算限制**——宁愿提前失败也不要超支。
- **记录模型选择决策**,以便根据真实数据调整阈值。
- **为超过 1024 tokens 的系统提示词使用提示词缓存**——既能节省成本又能降低延迟。
- **绝不在身份验证或校验错误时重试**——仅重试瞬时故障(网络、频率限制、服务器错误)。
应避免的反模式 (Anti-Patterns)
- 不分复杂度,对所有请求都使用最昂贵的模型。
- 对所有错误进行重试(在永久性失败上浪费预算)。
- 修改成本跟踪状态(增加调试和审计难度)。
- 在整个代码库中硬编码模型名称(应使用常量或配置)。
- 忽视对重复系统提示词的提示词缓存。
使用场景
- 任何调用 Claude、OpenAI 或类似 LLM API 的应用。
- 成本累积迅速的批量处理流水线。
- 需要智能路由的多模型架构。
- 需要预算安全护栏 (Guardrails) 的生产系统。
🌐 Language / 语言 / 語言 为 AI 智能体(Agent)框架打造的性能优化系统。源自 Anthropic 黑客松获胜作品。 这不仅仅是配置文件。它是一个完整的系统:包含技能(Skills)、本能(Instincts)、内存优化、持续学习、安全扫描以及研究优先的开发模式。这些生产级的智能体(Agents)、钩子(Hooks)、命令(Commands)、规则(Rules)以及 MCP 配置,是在构建真实产品的 10 个多月高强度日常使用中演化而来的。 适用于 Claude Code, Codex,
Repo: xu-xiang/everything-claude-code-zh
Other skills on everything-claude-code.
- /oneskill
发现技能(Skill),迭代查询,并在任何环境中自动安装技能。
Open skill - /api-design
生产级 API 的 REST API 设计模式,包括资源命名、状态码、分页、过滤、错误响应、版本控制和速率限制。
Open skill - /article-writing
编写文章、指南、博客帖子、教程、新闻通讯(newsletter)以及其他长篇内容。这些内容具有从提供的示例或品牌指南中提取出的独特语气。当用户需要比段落更长的精美文案,且对语气一致性、结构和可信度有要求时,请使用此技能(Skill)。
Open skill - /autonomous-loops
自主运行 Claude Code 循环的模式与架构 —— 从简单的顺序流水线到 RFC 驱动的多智能体 DAG 系统。
Open skill - /backend-patterns
后端架构模式、API 设计、数据库优化以及针对 Node.js、Express 和 Next.js API 路由的服务端最佳实践。
Open skill - /clickhouse-io
ClickHouse 数据库模式、查询优化、分析以及高性能分析负载的数据工程最佳实践。
Open skill

