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-optimizer
3description: >
4 This skill should be used when the user asks to "estimate LLM costs",
5 "count tokens in prompts", "optimize prompt token usage",
6 "compare model pricing", or "reduce LLM API costs".
7license: MIT + Commons Clause
8metadata:
9 version: 1.1.0
10 author: borghei
11 category: engineering
12 domain: ai-cost-management
13 updated: 2026-06-29
14 tags: [llm, tokens, cost-optimization, prompt-engineering, pricing]
15---
16
17# LLM Cost Optimizer
18
19> **Category:** Engineering
20> **Domain:** AI Cost Management
21
22## Overview
23
24The **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.
25
26## Clarify First
27
28Before estimating or optimizing, confirm these inputs. If any is unknown or vague, ASK — do not assume:
29
30- [ ] **Input prompt/text** — the prompt file or text to count or optimize (the input via `--file`/`--text`/`--stdin`)
31- [ ] **Target models** — which models to estimate cost for (sets `--models` and the pricing comparison)
32- [ ] **Goal** — cost estimation vs prompt optimization, and any target reduction (selects `token_counter.py` vs `prompt_optimizer.py` and sets `--target-reduction`)
33
34Stop 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.
35
36## Quick Start
37
38```bash
39# Count tokens in a prompt file and estimate costs
40python scripts/token_counter.py --file prompt.txt --models gpt-4o claude-sonnet
41
42# Count tokens from stdin
43echo "Hello world" | python scripts/token_counter.py --stdin --models all
44
45# Analyze a prompt for optimization opportunities
46python scripts/prompt_optimizer.py --file system_prompt.txt
47
48# Optimize with target reduction
49python scripts/prompt_optimizer.py --file prompt.txt --target-reduction 30
50```
51
52## Tools Overview
53
54| Tool | Purpose | Key Flags |
55|------|---------|-----------|
56| `token_counter.py` | Count tokens and estimate costs across models | `--file`, `--text`, `--stdin`, `--models` |
57| `prompt_optimizer.py` | Analyze prompts for token reduction opportunities | `--file`, `--target-reduction`, `--format` |
58| `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` |
59
60## Workflows
61
62### Cost Estimation for New Project
631. Collect sample prompts (system prompt + user messages)
642. Run `token_counter.py` with target models
653. Multiply per-request cost by expected daily volume
664. Compare models on cost-quality tradeoff
67
68### Prompt Optimization Sprint
691. Identify highest-cost prompts from usage logs
702. Run `prompt_optimizer.py` on each
713. Apply suggested optimizations
724. Re-count tokens to verify reduction
735. A/B test optimized vs. original for quality
74
75## Reference Documentation
76
77- [LLM Pricing Guide](references/llm-pricing-guide.md) - Current pricing for major LLM providers, token estimation methods
78- [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)
79
80## Common Patterns
81
82### Token Reduction Techniques
83- Remove redundant instructions and examples
84- Use shorter variable names in few-shot examples
85- Compress verbose system prompts
86- Replace repeated context with references
87- Use structured output formats (JSON) to reduce response tokens
88- Batch multiple requests into single prompts where possible
89
90### Cost-Effective Model Selection
91- Use smaller models for classification/extraction tasks
92- Reserve large models for complex reasoning
93- Implement model routing based on query complexity
94- Cache responses for identical or similar queries
95- Cache the stable system-prompt/context/schema prefix (most-stable first, volatile last) and check the reuse break-even with `cache_savings_calculator.py`
96- 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