llm-cost-attr
v0.3.1
Published
FinOps cost attribution and optimization tool for LLM API usage. Scans codebases for LLM calls, estimates costs, recommends optimizations.
Maintainers
Readme
llm-cost-attr
FinOps cost attribution tool for LLM API usage. Scans your codebase, finds every LLM API call, estimates costs, and recommends optimizations.
Think of it as a cost linter for AI spend.
Install
npx llm-cost-attr scan .Or install globally:
npm install -g llm-cost-attr
llm-cost-attr scan .What it does
- Scans your codebase for LLM API calls across 5 providers
- Estimates monthly costs based on current model pricing
- Detects loops, caching patterns, and batch opportunities
- Recommends optimizations (model downgrades, caching, batching)
Supported Providers
| Provider | Detection Pattern |
|----------|------------------|
| OpenAI | openai.chat.completions.create(), openai.embeddings.create() |
| Anthropic | client.messages.create() |
| Google GenAI | model.generateContent(), model.embedContent() |
| LangChain | ChatGoogleGenerativeAI, ChatOpenAI, ChatAnthropic, .invoke(), .ainvoke() |
| Claude CLI | claude -p subprocess calls |
Works with both TypeScript/JavaScript and Python codebases.
Usage
Scan a repository
llm-cost-attr scan /path/to/repoSet monthly invocation volume
llm-cost-attr scan /path/to/repo --volume 10000Output formats
# Terminal (default)
llm-cost-attr scan /path/to/repo
# JSON (for CI pipelines)
llm-cost-attr scan /path/to/repo -f json -o report.json
# HTML dashboard
llm-cost-attr scan /path/to/repo -f html -o report.htmlCompare multiple repos
llm-cost-attr compare /repo1 /repo2 /repo3View pricing
llm-cost-attr pricingExample Output
LLM Cost Attribution Report
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Scanned: 193 files | Found: 5 LLM call sites
Estimated monthly cost: $9.07 @ 1K invocations/month
┌──────────────────────────┬───────────┬──────────────────┬──────────┬──────┬──────────┐
│ File │ Provider │ Model │ $/call │ Loop │ $/month │
├──────────────────────────┼───────────┼──────────────────┼──────────┼──────┼──────────┤
│ src/aggregation.ts:232 │ anthropic │ claude-haiku-4-5 │ $0.0016 │ 5x │ $8.00 │
│ src/llm-scorer.ts:178 │ anthropic │ claude-haiku-4-5 │ $0.0010 │ 5x │ $5.00 │
│ src/llm-scorer.ts:163 │ google │ gemini-2.0-flash │ $0.0001 │ 5x │ $0.31 │
└──────────────────────────┴───────────┴──────────────────┴──────────┴──────┴──────────┘
Optimization Opportunities:
📦 src/aggregation.ts:232
5 sequential calls in a loop. Consider batching or parallel execution.How it works
Static analysis — no runtime proxy, no API keys needed. The tool:
- Walks your codebase (respects
.gitignore) - Detects SDK imports (OpenAI, Anthropic, Google GenAI, LangChain)
- Finds API call sites using regex + heuristic analysis
- Extracts model names, max_tokens, prompt sizes
- Detects loops (including cross-function call chains)
- Checks for existing caching (Redis, in-memory)
- Calculates costs using current provider pricing
- Generates optimization recommendations
Pricing Data
Built-in pricing for all major models (updated March 2026):
- Anthropic: Opus 4 ($5/$25), Sonnet 4 ($3/$15), Haiku 4.5 ($0.25/$1.25)
- OpenAI: GPT-4o ($2.50/$10), GPT-4o-mini ($0.15/$0.60), o1/o3
- Google: Gemini 2.0 Flash ($0.10/$0.40), Gemini 2.0 Pro ($1.25/$5)
Per 1M tokens (input/output). Run llm-cost-attr pricing for the full list.
License
MIT
