Install any skill in seconds. Free to start, no credit card required.
Get Started Free →xu-xiang/cost-aware-llm-pipeline
LLM API 使用成本优化模式——基于任务复杂度的模型路由、预算跟踪、重试逻辑和提示词缓存。
Passed 20 of 22 test cases with the skill, and 13 of 22 without it.
and 9 more in the full report
7 fixed14 passed either way2 still failing0 broke
Tokens, turns and time describe resource use, not a dollar price.
One of 6 test cases the skill fixed. Same model, same prompt, with and without the skill.
我们需要一个代表单次 LLM API 扣费结果的 Python 数据结构 CostRecord。请给出包含模型名称、输入 token 数、输出 token 数和美元金额的 dataclass 定义。
| Checked | Without skill | With skill |
|---|---|---|
| The CostRecord dataclass is decorated with frozen=True and slots=True. | Missed | Passed |
| The CostRecord class includes attributes model, input_tokens, output_tokens, and cost_usd. | Missed | Passed |
The instructions your model reads when the skill loads.
在保持质量的同时控制 LLM API 成本的模式。将模型路由 (Model Routing)、预算跟踪 (Budget Tracking)、重试逻辑 (Retry Logic) 和提示词缓存 (Prompt Caching) 组合成一个可复用的流水线。
为简单任务自动选择更便宜的模型,将昂贵的模型留给复杂任务。
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 倍)
使用冻结的数据类 (Frozen Dataclasses) 跟踪累计支出。每次 API 调用都会返回一个新的跟踪器——绝不修改原始状态。
test cases passed, vs 13 / 22 without
Published skills whose descriptions closely match this one, with their headline benchmark lift.
More skills from the same source and nearby in the registry, with their headline benchmark lift.