llm-cost-estimator-now 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +202 -0
- package/dist/cli.cjs +959 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +938 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +451 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +168 -0
- package/dist/index.d.ts +168 -0
- package/dist/index.js +403 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mayank Sardhara
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# llm-cost-estimator
|
|
2
|
+
|
|
3
|
+
Estimate token usage and cost across OpenAI, Anthropic, Gemini, and more — as a programmatic Node.js library, CLI tool, and terminal dashboard.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Offline Token Counting**: Accurate token estimation offline (using `js-tiktoken` for OpenAI and fine-tuned word/char heuristics for Anthropic/Gemini).
|
|
8
|
+
- **Offline Cost Logic**: Bundles rates for popular models and merges them with optional local JSON overrides.
|
|
9
|
+
- **Library API**: Clean, well-typed programmatic API for TypeScript and JavaScript.
|
|
10
|
+
- **CLI Commands**: Direct CLI tool for one-off estimations, piping/logging usage in shell scripts, and CI/CD cost budget checks.
|
|
11
|
+
- **Interactive Terminal Dashboard**: Real-time terminal dashboard built with React and Ink that watches log files and refreshes usage stats.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install llm-cost-estimator
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
To run the CLI tool globally:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install -g llm-cost-estimator
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Programmatic Library Usage
|
|
30
|
+
|
|
31
|
+
### 1. Cost & Token Estimation
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { estimateCost } from 'llm-cost-estimator';
|
|
35
|
+
|
|
36
|
+
const result = estimateCost({
|
|
37
|
+
provider: 'openai',
|
|
38
|
+
model: 'gpt-4o-mini',
|
|
39
|
+
input: 'Translate the following phrase: Hello, how are you today?',
|
|
40
|
+
expectedOutputTokens: 150
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
console.log(result);
|
|
44
|
+
/*
|
|
45
|
+
{
|
|
46
|
+
provider: 'openai',
|
|
47
|
+
model: 'gpt-4o-mini',
|
|
48
|
+
inputTokens: 11,
|
|
49
|
+
outputTokens: 150,
|
|
50
|
+
inputCost: 0.00000165,
|
|
51
|
+
outputCost: 0.00009,
|
|
52
|
+
totalCost: 0.00009165,
|
|
53
|
+
currency: 'USD',
|
|
54
|
+
pricingSource: 'bundled',
|
|
55
|
+
estimated: false
|
|
56
|
+
}
|
|
57
|
+
*/
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 2. Model Comparison
|
|
61
|
+
|
|
62
|
+
Compare cost across multiple candidate models to select the best/cheapest option:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { compareModels } from 'llm-cost-estimator';
|
|
66
|
+
|
|
67
|
+
const results = compareModels({
|
|
68
|
+
input: 'Draft a short email.',
|
|
69
|
+
expectedOutputTokens: 100,
|
|
70
|
+
candidates: [
|
|
71
|
+
{ provider: 'openai', model: 'gpt-4o' },
|
|
72
|
+
{ provider: 'openai', model: 'gpt-4o-mini' },
|
|
73
|
+
{ provider: 'anthropic', model: 'claude-3-5-sonnet' }
|
|
74
|
+
]
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Results are returned sorted from cheapest to most expensive
|
|
78
|
+
console.log(results);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 3. Usage Logging & Summarization
|
|
82
|
+
|
|
83
|
+
Log runs to a local `.jsonl` log file and generate aggregate statistics:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { logUsage, getUsageSummary } from 'llm-cost-estimator';
|
|
87
|
+
|
|
88
|
+
// Log actual API consumption post-request
|
|
89
|
+
logUsage({
|
|
90
|
+
provider: 'openai',
|
|
91
|
+
model: 'gpt-4o',
|
|
92
|
+
inputTokens: 850,
|
|
93
|
+
outputTokens: 250,
|
|
94
|
+
tag: 'production-v1'
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Fetch summary grouped by model, provider, tag, or day
|
|
98
|
+
const summary = getUsageSummary({
|
|
99
|
+
groupBy: 'model',
|
|
100
|
+
since: '24h' // accepts '24h', '7d', '30d' or Date object
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## CLI Usage
|
|
107
|
+
|
|
108
|
+
### 1. Estimate Cost
|
|
109
|
+
|
|
110
|
+
Estimate tokens and cost for a prompt directly:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
llm-cost estimate -p openai -m gpt-4o-mini -i "Hello world" -o 50
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Read input from a text file:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
llm-cost estimate -p openai -m gpt-4o -f prompt.txt -o 100
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Pipe stdin:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
cat prompt.txt | llm-cost estimate -p anthropic -m claude-3-5-sonnet -o 200
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Add a max cost constraint for CI/CD checks:
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
llm-cost estimate -p openai -m gpt-4o -i "Perform a complex build" --max-cost 0.005
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Output results as JSON:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
llm-cost estimate -p openai -m gpt-4o -i "Query" --json
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### 2. Compare Models
|
|
141
|
+
|
|
142
|
+
Compare pricing for a prompt across several candidate models:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
llm-cost compare openai/gpt-4o openai/gpt-4o-mini anthropic/claude-3-5-sonnet -i "Compare performance of these systems"
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### 3. Log Usage
|
|
149
|
+
|
|
150
|
+
Manually append a run to the usage log:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
llm-cost log -p openai -m gpt-4o -i 1000 -o 400 --tag release-test
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 4. Summary
|
|
157
|
+
|
|
158
|
+
Print tabular summaries of logged runs:
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
llm-cost summary --group-by model --since 7d
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### 5. Interactive Dashboard
|
|
165
|
+
|
|
166
|
+
Open the live-updating terminal usage dashboard (built with Ink and React):
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
llm-cost dashboard
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
## Configuration & Pricing Overrides
|
|
175
|
+
|
|
176
|
+
You can override default pricing rates or define new models by creating a JSON configuration file.
|
|
177
|
+
|
|
178
|
+
By default, the library looks for overrides at `~/.llm-cost-estimator/pricing-override.json`. You can also point to a custom file via the `LLM_COST_PRICING_OVERRIDE` environment variable.
|
|
179
|
+
|
|
180
|
+
Example override structure:
|
|
181
|
+
|
|
182
|
+
```json
|
|
183
|
+
{
|
|
184
|
+
"currency": "USD",
|
|
185
|
+
"models": {
|
|
186
|
+
"openai/gpt-4o": {
|
|
187
|
+
"inputPer1M": 2.00,
|
|
188
|
+
"outputPer1M": 8.00
|
|
189
|
+
},
|
|
190
|
+
"custom-provider/my-new-model": {
|
|
191
|
+
"inputPer1M": 1.00,
|
|
192
|
+
"outputPer1M": 4.00
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
MIT © [Mayank Sardhara](https://github.com/mayank8025)
|