LLM Cost Optimizer
Category: Engineering
Domain: AI Cost Management
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, confirm these inputs. If any is unknown or vague, ASK — do not assume:
Stop rule: ask only the 2-3 that most change the output. If the user says "just draft it," proceed and list your assumptions at the top of the artifact.
Quick Start
# Count tokens in a prompt file and estimate costs
python scripts/token_counter.py --file prompt.txt --models gpt-4o claude-sonnet
# Count tokens from stdin
echo "Hello world" | python scripts/token_counter.py --stdin --models all
# Analyze a prompt for optimization opportunities
python scripts/prompt_optimizer.py --file system_prompt.txt
# Optimize with target reduction
python scripts/prompt_optimizer.py --file prompt.txt --target-reduction 30
Tools Overview
| Tool |
Purpose |
Key Flags |
token_counter.py |
Count tokens and estimate costs across models |
--file, --text, --stdin, --models |
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
- Collect sample prompts (system prompt + user messages)
- Run
token_counter.py with target models
- Multiply per-request cost by expected daily volume
- Compare models on cost-quality tradeoff
Prompt Optimization Sprint
- Identify highest-cost prompts from usage logs
- Run
prompt_optimizer.py on each
- Apply suggested optimizations
- Re-count tokens to verify reduction
- A/B test optimized vs. original for quality
Reference Documentation
- LLM Pricing Guide - Current pricing for major LLM providers, token estimation methods
- Caching & Batch Economics - Prompt/context caching break-even math, batch-API cost tradeoff, reasoning-effort cost impact, structured-output token overhead (model-agnostic, user-supplied rates)
Common Patterns
Token Reduction Techniques
- Remove redundant instructions and examples
- Use shorter variable names in few-shot examples
- Compress verbose system prompts
- Replace repeated context with references
- Use structured output formats (JSON) to reduce response tokens
- Batch multiple requests into single prompts where possible
Cost-Effective Model Selection
- Use smaller models for classification/extraction tasks
- Reserve large models for complex reasoning
- Implement model routing based on query complexity
- Cache responses for identical or similar queries
- Cache the stable system-prompt/context/schema prefix (most-stable first, volatile last) and check the reuse break-even with
cache_savings_calculator.py
- Route bulk, non-interactive work to the batch API (~half cost for added latency); right-size reasoning effort per route — high only where accuracy demands it
1---2name: llm-cost-optimizer3description: This skill should be used when the user asks to "estimate LLM costs", "count tokens in prompts", "optimize prompt token usage", "compare model pricing", or "reduce LLM API costs".4license: MIT + Commons Clause5---6
7# LLM Cost Optimizer
8
9> **Category:** Engineering
10> **Domain:** AI Cost Management
11
12## Overview
13
14The **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.
15
16## Clarify First
17
18Before estimating or optimizing, confirm these inputs. If any is unknown or vague, ASK — do not assume:
19
20- [ ] **Input prompt/text** — the prompt file or text to count or optimize (the input via `--file`/`--text`/`--stdin`)
21- [ ] **Target models** — which models to estimate cost for (sets `--models` and the pricing comparison)
22- [ ] **Goal** — cost estimation vs prompt optimization, and any target reduction (selects `token_counter.py` vs `prompt_optimizer.py` and sets `--target-reduction`)
23
24Stop rule: ask only the 2-3 that most change the output. If the user says "just draft it," proceed and list your assumptions at the top of the artifact.
25
26## Quick Start
27
28```bash
29# Count tokens in a prompt file and estimate costs
30python scripts/token_counter.py --file prompt.txt --models gpt-4o claude-sonnet
31
32# Count tokens from stdin
33echo "Hello world" | python scripts/token_counter.py --stdin --models all
34
35# Analyze a prompt for optimization opportunities
36python scripts/prompt_optimizer.py --file system_prompt.txt
37
38# Optimize with target reduction
39python scripts/prompt_optimizer.py --file prompt.txt --target-reduction 30
40```
41
42## Tools Overview
43
44| Tool | Purpose | Key Flags |
45|------|---------|-----------|
46| `token_counter.py` | Count tokens and estimate costs across models | `--file`, `--text`, `--stdin`, `--models` |
47| `prompt_optimizer.py` | Analyze prompts for token reduction opportunities | `--file`, `--target-reduction`, `--format` |
48| `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` |
49
50## Workflows
51
52### Cost Estimation for New Project
531. Collect sample prompts (system prompt + user messages)
542. Run `token_counter.py` with target models
553. Multiply per-request cost by expected daily volume
564. Compare models on cost-quality tradeoff
57
58### Prompt Optimization Sprint
591. Identify highest-cost prompts from usage logs
602. Run `prompt_optimizer.py` on each
613. Apply suggested optimizations
624. Re-count tokens to verify reduction
635. A/B test optimized vs. original for quality
64
65## Reference Documentation
66
67- [LLM Pricing Guide](references/llm-pricing-guide.md) - Current pricing for major LLM providers, token estimation methods
68- [Caching & Batch Economics](references/caching-and-batch-economics.md) - Prompt/context caching break-even math, batch-API cost tradeoff, reasoning-effort cost impact, structured-output token overhead (model-agnostic, user-supplied rates)
69
70## Common Patterns
71
72### Token Reduction Techniques
73- Remove redundant instructions and examples
74- Use shorter variable names in few-shot examples
75- Compress verbose system prompts
76- Replace repeated context with references
77- Use structured output formats (JSON) to reduce response tokens
78- Batch multiple requests into single prompts where possible
79
80### Cost-Effective Model Selection
81- Use smaller models for classification/extraction tasks
82- Reserve large models for complex reasoning
83- Implement model routing based on query complexity
84- Cache responses for identical or similar queries
85- Cache the stable system-prompt/context/schema prefix (most-stable first, volatile last) and check the reuse break-even with `cache_savings_calculator.py`
86- Route bulk, non-interactive work to the batch API (~half cost for added latency); right-size reasoning effort per route — high only where accuracy demands it