cost-aware-llm-pipeline — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited cost-aware-llm-pipeline (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
品質を維持しながらLLM APIのコストをコントロールするためのパターン。モデルルーティング、予算追跡、リトライロジック、プロンプトキャッシングを組み合わせた合成可能なパイプライン。
シンプルなタスクには自動的に安価なモデルを選択し、複雑なタスクのために高価なモデルを予約します。
MODEL_STANDARD = "codex-standard-4-6"
MODEL_FAST = "codex-fast-4-5-20251001"
_STANDARD_TEXT_THRESHOLD = 10_000 # 文字数
_STANDARD_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 >= _STANDARD_TEXT_THRESHOLD or item_count >= _STANDARD_ITEM_THRESHOLD:
return MODEL_STANDARD # 複雑なタスク
return MODEL_FAST # シンプルなタスク(3〜4倍安価)凍結データクラスで累積支出を追跡します。各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":
"""追加されたレコードで新しいトラッカーを返す(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一時的なエラーのみリトライします。認証やリクエストエラーでは素早く失敗します。
from openai 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) # 指数バックオフ
# AuthenticationError、BadRequestErrorなど → 即座に例外発生長いシステムプロンプトをキャッシュして、リクエストごとに再送信しないようにします。
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": system_prompt,
"cache_control": {"type": "ephemeral"}, # これをキャッシュ
},
{
"type": "text",
"text": user_input, # 可変部分
},
],
}
]4つのテクニックすべてを単一のパイプライン関数に組み合わせます:
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| モデル | 入力($/1Mトークン) | 出力($/1Mトークン) | 相対コスト |
|---|---|---|---|
| Fast 4.5 | $0.80 | $4.00 | 1x |
| Standard 4.6 | $3.00 | $15.00 | 約4x |
| Deep 4.5 | $15.00 | $75.00 | 約19x |
~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.