Cost Aware Llm Pipeline logo

Cost Aware Llm Pipeline

CommunityPopular
xu-xiang
cost-aware-llm-pipeline

LLM API 使用成本优化模式 —— 基于任务复杂度的模型路由、预算跟踪、重试逻辑和提示缓存。

Overview

Publisherxu-xiang
Repositoryeverything-claude-code-zh
Skill namecost-aware-llm-pipeline
Stars
1.9K
Forks
318
Bundled files
Instructions only
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by xu-xiang on GitHub. Read the source before you install it.

Installation

Install the Cost Aware Llm Pipeline AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git /tmp/everything-claude-code-zh
mkdir -p .claude/skills
cp -r /tmp/everything-claude-code-zh/docs/zh-CN/skills/cost-aware-llm-pipeline .claude/skills/cost-aware-llm-pipeline
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Cost Aware Llm Pipeline in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Cost Aware Llm Pipeline on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Cost Aware Llm Pipeline is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

成本感知型 LLM 流水线

在保持质量的同时控制 LLM API 成本的模式。将模型路由、预算跟踪、重试逻辑和提示词缓存组合成一个可组合的流水线。

何时激活

  • 构建调用 LLM API(Claude、GPT 等)的应用程序时
  • 处理具有不同复杂度的批量项目时
  • 需要将 API 支出控制在预算范围内时
  • 需要在复杂任务上优化成本而不牺牲质量时

核心概念

1. 根据任务复杂度进行模型路由

自动为简单任务选择更便宜的模型,为复杂任务保留昂贵的模型。

python
MODEL_SONNET = "claude-sonnet-4-6"
MODEL_HAIKU = "claude-haiku-4-5-20251001"

_SONNET_TEXT_THRESHOLD = 10_000  # chars
_SONNET_ITEM_THRESHOLD = 30     # items

def select_model(
    text_length: int,
    item_count: int,
    force_model: str | None = None,
) -> str:
    """Select model based on task complexity."""
    if force_model is not None:
        return force_model
    if text_length >= _SONNET_TEXT_THRESHOLD or item_count >= _SONNET_ITEM_THRESHOLD:
        return MODEL_SONNET  # Complex task
    return MODEL_HAIKU  # Simple task (3-4x cheaper)

2. 不可变的成本跟踪

使用冻结的数据类跟踪累计支出。每个 API 调用都会返回一个新的跟踪器 —— 永不改变状态。

python
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 new tracker with added record (never mutates self)."""
        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_limit

3. 窄范围重试逻辑

仅在暂时性错误时重试。对于认证或错误请求错误,快速失败。

python
from anthropic import (
    APIConnectionError,
    InternalServerError,
    RateLimitError,
)

_RETRYABLE_ERRORS = (APIConnectionError, RateLimitError, InternalServerError)
_MAX_RETRIES = 3

def call_with_retry(func, *, max_retries: int = _MAX_RETRIES):
    """Retry only on transient errors, fail fast on others."""
    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 etc. → raise immediately

4. 提示词缓存

缓存长的系统提示词,以避免在每个请求上重新发送它们。

python
messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": system_prompt,
                "cache_control": {"type": "ephemeral"},  # Cache this
            },
            {
                "type": "text",
                "text": user_input,  # Variable part
            },
        ],
    }
]

组合

将所有四种技术组合到一个流水线函数中:

python
def process(text: str, config: Config, tracker: CostTracker) -> tuple[Result, CostTracker]:
    # 1. Route model
    model = select_model(len(text), estimated_items, config.force_model)

    # 2. Check budget
    if tracker.over_budget:
        raise BudgetExceededError(tracker.total_cost, tracker.budget_limit)

    # 3. Call with retry + caching
    response = call_with_retry(lambda: client.messages.create(
        model=model,
        messages=build_cached_messages(system_prompt, text),
    ))

    # 4. Track cost (immutable)
    record = CostRecord(model=model, input_tokens=..., output_tokens=..., cost_usd=...)
    tracker = tracker.add(record)

    return parse_result(response), tracker

价格参考(2025-2026)

模型输入(美元/百万令牌)输出(美元/百万令牌)相对成本
Haiku 4.5$0.80$4.001x
Sonnet 4.6$3.00$15.00~4x
Opus 4.5$15.00$75.00~19x

最佳实践

  • 从最便宜的模型开始,仅在达到复杂度阈值时才路由到昂贵的模型
  • 在处理批次之前设置明确的预算限制 —— 尽早失败而不是超支
  • 记录模型选择决策,以便您可以根据实际数据调整阈值
  • 对于超过 1024 个令牌的系统提示词,使用提示词缓存 —— 既能节省成本,又能降低延迟
  • 切勿在认证或验证错误时重试 —— 仅针对暂时性故障(网络、速率限制、服务器错误)重试

应避免的反模式

  • 无论复杂度如何,对所有请求都使用最昂贵的模型
  • 对所有错误都进行重试(在永久性故障上浪费预算)
  • 改变成本跟踪状态(使调试和审计变得困难)
  • 在整个代码库中硬编码模型名称(使用常量或配置)
  • 对重复的系统提示词忽略提示词缓存

适用场景

  • 任何调用 Claude、OpenAI 或类似 LLM API 的应用程序
  • 成本快速累积的批处理流水线
  • 需要智能路由的多模型架构
  • 需要预算护栏的生产系统

Frequently asked questions

What does the Cost Aware Llm Pipeline AI skill do?

LLM API 使用成本优化模式 —— 基于任务复杂度的模型路由、预算跟踪、重试逻辑和提示缓存。

Why use Cost Aware Llm Pipeline on TypingMind?

Because you install it once and use it with any model. Cost Aware Llm Pipeline is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Cost Aware Llm Pipeline in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/xu-xiang/everything-claude-code-zh/tree/main/docs/zh-CN/skills/cost-aware-llm-pipeline. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Cost Aware Llm Pipeline?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Cost Aware Llm Pipeline?

As many as you like. As long as a model supports skills, you can use Cost Aware Llm Pipeline with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Cost Aware Llm Pipeline AI skill free?

Yes. It is published on GitHub by xu-xiang under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇