agentleFS
SirCharanSirCharan/claude-code-harness/skills/cost-aware-llm-pipeline/SKILL.md

cost-aware-llm-pipeline

Skill0 starsChanged 5 months agoMITView on GitHub

Cost/latency optimization for apps that call LLM APIs — model routing by task complexity, budget guards, narrow retry, and prompt caching. Use when building or tuning LLM calls in any pipeline (signal generation, batch processing, or any multi-model workflow), or staying under a spend cap. Directly relevant to a shared LLM provider daily budget cap that can exhaust silently (429/403). Trigger on: "LLM cost", "model routing", "which model for this", "API budget", "rate limit / 403", "prompt caching", "batch LLM", "reduce token spend in <app>". Adapted from ECC (affaan-m/ECC, MIT).

21 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 18 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Start on the cheapest model.

    line 19

“Common” is measured: kept by at least 6 in 10 of the 18 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Cost-Aware LLM Pipeline

Control LLM spend without losing quality, by composing four independent levers. Build pipelines so they degrade gracefully and stay cheap. If multiple apps share a single provider key against a daily cap, exhaustion will silently 403/429 all of them — track per-app spend and stop the lower-priority app first.

1. Route model by task complexity ✅

Start on the cheapest model; escalate only when complexity crosses a threshold. Current tiers: claude-haiku-4-5 (cheap/fast, ~1×) → claude-sonnet-4-6 (balanced, ~4×) → claude-opus-4-8 (reasoning, ~5×) → claude-fable-5 (frontier).

HAIKU, SONNET, OPUS = "claude-haiku-4-5", "claude-sonnet-4-6", "claude-opus-4-8"
def select_model(text_len: int, n_items: int, force: str | None = None) -> str:
    if force: return force
    if text_len >= 10_000 or n_items >= 30: return SONNET   # complex
    return HAIKU                                             # simple → 3–4× cheaper

Reserve Opus/Fable for genuine reasoning (the decide step), not enrichment/formatting. Log every routing decision so thresholds can be tuned from real data (a central per-call cost log is the model).

2. Budget guard (fail early, not silently) ✅

Track cumulative spend with an immutable tracker; check before the call, raise before you blow the cap.

@dataclass(frozen=True, slots=True)
class CostTracker:
    budget_usd: float = 1.00
    spent: float = 0.0
    def add(self, usd): return replace(self, spent=self.spent + usd)
    @property
    def over(self): return self.spent > self.budget_usd

For a shared provider key: track per-app daily spend and stop the lower-priority app first rather than letting both exhaust. Surface "budget exhausted" as a visible state, never a silent empty result.

3. Narrow retry ✅

Retry only transient errors (connection, rate-limit, 5xx) with exponential backoff; fail fast on auth/ bad-request (retrying those just burns budget). _RETRYABLE = (APIConnectionError, RateLimitError, InternalServerError).

4. Prompt caching ✅

Mark the stable prefix (system prompt, tool defs, long static context) with cache_control: ephemeral; cache reads ≈ 0.1× input cost, 5-min TTL. Put stable content first, variable input last (see working-with-claude §4). Worth it for any system prompt >1024 tokens — saves cost and latency.

Best practices / anti-patterns

  • ✅ cheapest-first + threshold escalation · explicit budget before batches · log routing · cache long prefixes.
  • ✅ concise output instructions ("answer in N lines") — output tokens are 4–5× input price.
  • ✗ one expensive model for everything · retry on auth/validation errors · mutating cost state · hardcoded model strings scattered in code (use constants/config) · ignoring caching on repeated system prompts.

Sources

Adapted from ECC skills/cost-aware-llm-pipeline (MIT); model tiers/caching per the installed claude-api

  • working-with-claude skills; budget framing from practical cost-telemetry experience with shared provider keys.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.