Llm cost optimizer

From borghei/Claude-Skills · View interactive version

Llm cost optimizer is an AI agent skill from the GitHub repository borghei/Claude-Skills. Estimates LLM token counts and per-request cost across models, flags wasteful prompt text, and models prompt-cache break-even. Use when budgeting LLM spend, comparing models on cost, or trimming prompt tokens.

How to use this skill

Skill file

LLM Cost Optimizer

The agent turns "what will this cost?" into a number with its assumptions shown: estimated tokens, cost per request and per 1,000 requests by model, savings from caching, and prompt text that costs tokens without changing the result. The built-in price table is an illustrative snapshot, not verified current pricing: for real numbers, copy rates from each provider's pricing page into a JSON file and pass it with --prices FILE (shape: assets/sample_prices.json).

When to Use

Overview

The LLM Cost Optimizer skill provides tools for counting tokens, estimating costs across different LLM providers, and optimizing prompts to reduce token usage without sacrificing quality. Essential for teams managing LLM API budgets at scale.

Clarify First

Before estimating or optimizing, settle the inputs below. When one is missing or vague and would change the result, ask about it; when it wouldn't, use a sensible default and say which one you used.

Ask about the two or three inputs that matter most for this request, not the whole list. If the user wants a result straight away, produce it and state your assumptions at the top.

Quick Start

# Count tokens and estimate costs. Built-in prices are illustrative; real numbers come from --prices FILE
python3 scripts/token_counter.py --text "Summarize the attached contract in five bullets." --models claude-opus-5-5 claude-sonnet gpt-4o-mini

# Count tokens from stdin
echo "Hello world" | python3 scripts/token_counter.py --stdin --models all

# Costs from your own price file (copy current rates from the providers' pricing pages)
python3 scripts/token_counter.py --file assets/sample_prompt.txt --prices assets/sample_prices.json

# Analyze a prompt: three findings, one per line (role preamble, CoT scaffold, all-caps emphasis)
python3 scripts/prompt_optimizer.py --file assets/sample_prompt.txt

Exit codes: 0 report produced; 2 unusable input (missing file, empty input, invalid numbers, unreadable --prices).

Tools Overview

| Tool | Purpose | Key Flags |

|------|---------|-----------|

| token_counter.py | Count tokens and estimate costs across models | --file, --text, --stdin, --models, --output-ratio, --prices FILE |

| prompt_optimizer.py | Analyze prompts for token reduction opportunities | --file, --target-reduction, --format |

| cache_savings_calculator.py | Model prompt-cache economics: naive vs cached cost, break-even reuse, % savings | --requests, --cached-tokens, --cache-write-multiplier, --cache-read-multiplier, --base-input-price, --json |

Workflows

Cost Estimation for New Project

  1. Collect sample prompts (system prompt + user messages)
  2. Run token_counter.py with target models
  3. Multiply per-request cost by expected daily volume
  4. Compare models on cost-quality tradeoff

Prompt Optimization Sprint

  1. Identify highest-cost prompts from usage logs
  2. Run prompt_optimizer.py on each
  3. Apply suggested optimizations
  4. Re-count tokens to verify reduction
  5. A/B test optimized vs. original for quality

Scope & Limitations

In Scope:

Out of Scope:

Important Caveats:

Reference Documentation

Anti-Patterns

Mistake: Budgeting from the built-in price snapshot.

Why it matters: Prices and model lines change several times a year; a stale table can be off by a factor of two or more.

Instead: Pass --prices with rates copied from each provider's pricing page, and record the date.

Mistake: Picking the cheapest model from a one-request estimate.

Why it matters: At small sizes every model rounds to fractions of a cent; quality differences cost far more than the token bill.

Instead: Compare per-1K-request cost at real volume, and only among models that pass the quality bar.

Mistake: Applying every optimizer suggestion.

Why it matters: Some "redundant" text carries meaning; removing it can lower quality more than it saves.

Instead: Apply edits, then run the prompt's eval suite before shipping the shorter version.

Common Patterns

Token Reduction Techniques

Cost-Effective Model Selection

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.