cost-katana 2.1.2 β 2.1.4
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/README.md +295 -582
- package/dist/config/default.js +2 -2
- package/dist/config/model-types.js +2 -2
- package/dist/config/pricing-data.js +2 -2
- package/dist/constants/models.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6 -6
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.js +1 -1
- package/dist/types/models.js +4 -4
- package/dist/types/simplified.d.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,483 +1,238 @@
|
|
|
1
1
|
# Cost Katana π₯·
|
|
2
2
|
|
|
3
|
-
**AI
|
|
3
|
+
> **Cut your AI costs in half. Without cutting corners.**
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
import { ai, OPENAI } from 'cost-katana';
|
|
5
|
+
Cost Katana is a drop-in SDK that wraps your AI calls with automatic cost tracking, smart caching, and optimizationβall in one line of code.
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
const response = await ai(OPENAI.GPT_4, 'Hello, world!');
|
|
10
|
-
console.log(response.text); // "Hello! How can I help you today?"
|
|
11
|
-
console.log(`Cost: $${response.cost}`); // "Cost: $0.0012"
|
|
12
|
-
```
|
|
7
|
+
---
|
|
13
8
|
|
|
14
|
-
|
|
9
|
+
## π Get Started in 60 Seconds
|
|
15
10
|
|
|
16
|
-
|
|
11
|
+
### Step 1: Install
|
|
17
12
|
|
|
18
13
|
```bash
|
|
19
14
|
npm install cost-katana
|
|
20
15
|
```
|
|
21
16
|
|
|
22
|
-
|
|
23
|
-
> ```bash
|
|
24
|
-
> npm uninstall ai-cost-tracker
|
|
25
|
-
> npm install cost-katana
|
|
26
|
-
> ```
|
|
27
|
-
|
|
28
|
-
## Quick Start
|
|
29
|
-
|
|
30
|
-
### π― Type-Safe Model Selection (Recommended)
|
|
31
|
-
|
|
32
|
-
```typescript
|
|
33
|
-
import { ai, OPENAI, ANTHROPIC, GOOGLE } from 'cost-katana';
|
|
34
|
-
|
|
35
|
-
// Type-safe model constants with autocomplete
|
|
36
|
-
await ai(OPENAI.GPT_4, 'Explain quantum computing');
|
|
37
|
-
await ai(ANTHROPIC.CLAUDE_3_5_SONNET_20241022, 'Write a haiku');
|
|
38
|
-
await ai(GOOGLE.GEMINI_2_5_PRO, 'Solve this equation: 2x + 5 = 13');
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
**Benefits:**
|
|
42
|
-
- β
**Autocomplete** - Your IDE suggests all available models
|
|
43
|
-
- β
**No typos** - Compile-time errors catch mistakes
|
|
44
|
-
- β
**Refactor safely** - Update model names with confidence
|
|
45
|
-
- β
**Self-documenting** - See exactly which model you're using
|
|
46
|
-
|
|
47
|
-
### Chat Conversations
|
|
17
|
+
### Step 2: Make Your First AI Call
|
|
48
18
|
|
|
49
19
|
```typescript
|
|
50
|
-
import {
|
|
20
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
51
21
|
|
|
52
|
-
const
|
|
53
|
-
await session.send('Hello!');
|
|
54
|
-
await session.send('What can you help me with?');
|
|
55
|
-
await session.send('Tell me a joke');
|
|
22
|
+
const response = await ai(OPENAI.GPT_4, 'Explain quantum computing in one sentence');
|
|
56
23
|
|
|
57
|
-
console.log(
|
|
24
|
+
console.log(response.text); // "Quantum computing uses qubits to perform..."
|
|
25
|
+
console.log(response.cost); // 0.0012
|
|
26
|
+
console.log(response.tokens); // 47
|
|
58
27
|
```
|
|
59
28
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
## π **More Examples**
|
|
29
|
+
**That's it.** No configuration files. No complex setup. Just results.
|
|
63
30
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
**π [github.com/Hypothesize-Tech/costkatana-examples](https://github.com/Hypothesize-Tech/costkatana-examples)**
|
|
31
|
+
---
|
|
67
32
|
|
|
68
|
-
|
|
69
|
-
- β
44 feature sections covering every Cost Katana capability
|
|
70
|
-
- β
HTTP REST API examples (`.http` files)
|
|
71
|
-
- β
TypeScript/Node.js examples
|
|
72
|
-
- β
Python SDK examples
|
|
73
|
-
- β
Framework integrations (Express, Next.js, Fastify, NestJS, FastAPI)
|
|
74
|
-
- β
Real-world use cases with best practices
|
|
75
|
-
- β
Production-ready code with full error handling
|
|
76
|
-
|
|
77
|
-
**Popular examples:**
|
|
78
|
-
- [Cost Tracking](https://github.com/Hypothesize-Tech/costkatana-examples/tree/master/1-cost-tracking) - Track costs across all providers
|
|
79
|
-
- [Webhooks](https://github.com/Hypothesize-Tech/costkatana-examples/tree/master/10-webhooks) - Real-time notifications
|
|
80
|
-
- [Workflows](https://github.com/Hypothesize-Tech/costkatana-examples/tree/master/13-workflows) - Multi-step AI orchestration
|
|
81
|
-
- [Semantic Caching](https://github.com/Hypothesize-Tech/costkatana-examples/tree/master/14-cache) - 30-40% cost reduction
|
|
82
|
-
- [OpenTelemetry](https://github.com/Hypothesize-Tech/costkatana-examples/tree/master/11-observability) - Distributed tracing
|
|
33
|
+
## π Tutorial: Build a Cost-Aware Chatbot
|
|
83
34
|
|
|
84
|
-
|
|
35
|
+
Let's build something real. In this tutorial, you'll create a chatbot that:
|
|
36
|
+
- β
Tracks every dollar spent
|
|
37
|
+
- β
Caches repeated questions (saving 100% on duplicates)
|
|
38
|
+
- β
Optimizes long responses (40-75% savings)
|
|
85
39
|
|
|
86
|
-
###
|
|
40
|
+
### Part 1: Basic Chat Session
|
|
87
41
|
|
|
88
42
|
```typescript
|
|
89
|
-
import {
|
|
90
|
-
|
|
91
|
-
const models = [
|
|
92
|
-
{ name: 'GPT-4', constant: OPENAI.GPT_4 },
|
|
93
|
-
{ name: 'Claude 3.5 Sonnet', constant: ANTHROPIC.CLAUDE_3_5_SONNET_20241022 },
|
|
94
|
-
{ name: 'Gemini 2.5 Pro', constant: GOOGLE.GEMINI_2_5_PRO }
|
|
95
|
-
];
|
|
96
|
-
const prompt = 'Explain relativity in one sentence';
|
|
43
|
+
import { chat, OPENAI } from 'cost-katana';
|
|
97
44
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
console.log(`${name}: $${response.cost.toFixed(4)}`);
|
|
101
|
-
}
|
|
102
|
-
```
|
|
45
|
+
// Create a persistent chat session
|
|
46
|
+
const session = chat(OPENAI.GPT_4);
|
|
103
47
|
|
|
104
|
-
|
|
48
|
+
// Send messages and track costs
|
|
49
|
+
await session.send('Hello! What can you help me with?');
|
|
50
|
+
await session.send('Tell me a programming joke');
|
|
51
|
+
await session.send('Now explain it');
|
|
105
52
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
await ai('claude-3-sonnet', 'Hello');
|
|
53
|
+
// See exactly what you spent
|
|
54
|
+
console.log(`π° Total cost: $${session.totalCost.toFixed(4)}`);
|
|
55
|
+
console.log(`π Messages: ${session.messages.length}`);
|
|
56
|
+
console.log(`π― Tokens used: ${session.totalTokens}`);
|
|
111
57
|
```
|
|
112
58
|
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
import { ai, OPENAI, ANTHROPIC } from 'cost-katana';
|
|
59
|
+
### Part 2: Add Smart Caching
|
|
116
60
|
|
|
117
|
-
|
|
118
|
-
await ai(OPENAI.GPT_4, 'Hello');
|
|
119
|
-
await ai(ANTHROPIC.CLAUDE_3_5_SONNET_20241022, 'Hello');
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
**Available Model Constants:**
|
|
61
|
+
Cache identical questions to avoid paying twice:
|
|
123
62
|
|
|
124
63
|
```typescript
|
|
125
|
-
|
|
126
|
-
OPENAI.GPT_5, OPENAI.GPT_5_MINI, OPENAI.GPT_4, OPENAI.GPT_4_TURBO,
|
|
127
|
-
OPENAI.GPT_4O, OPENAI.GPT_3_5_TURBO, OPENAI.O1, OPENAI.O3, ...
|
|
128
|
-
|
|
129
|
-
// Anthropic Models
|
|
130
|
-
ANTHROPIC.CLAUDE_SONNET_4_5, ANTHROPIC.CLAUDE_HAIKU_4_5,
|
|
131
|
-
ANTHROPIC.CLAUDE_3_5_SONNET_20241022, ANTHROPIC.CLAUDE_3_5_HAIKU_20241022, ...
|
|
132
|
-
|
|
133
|
-
// Google Models
|
|
134
|
-
GOOGLE.GEMINI_2_5_PRO, GOOGLE.GEMINI_2_5_FLASH, GOOGLE.GEMINI_1_5_PRO,
|
|
135
|
-
GOOGLE.GEMINI_1_5_FLASH, ...
|
|
64
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
136
65
|
|
|
137
|
-
//
|
|
138
|
-
|
|
66
|
+
// First call - hits the API
|
|
67
|
+
const response1 = await ai(OPENAI.GPT_4, 'What is 2+2?', { cache: true });
|
|
68
|
+
console.log(`Cached: ${response1.cached}`); // false
|
|
69
|
+
console.log(`Cost: $${response1.cost}`); // $0.0008
|
|
139
70
|
|
|
140
|
-
//
|
|
141
|
-
|
|
142
|
-
|
|
71
|
+
// Second call - served from cache (FREE!)
|
|
72
|
+
const response2 = await ai(OPENAI.GPT_4, 'What is 2+2?', { cache: true });
|
|
73
|
+
console.log(`Cached: ${response2.cached}`); // true
|
|
74
|
+
console.log(`Cost: $${response2.cost}`); // $0.0000 π
|
|
143
75
|
```
|
|
144
76
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
## Features
|
|
148
|
-
|
|
149
|
-
### π° Cost Tracking
|
|
77
|
+
### Part 3: Enable Cortex Optimization
|
|
150
78
|
|
|
151
|
-
|
|
79
|
+
For long-form content, Cortex compresses prompts intelligently:
|
|
152
80
|
|
|
153
81
|
```typescript
|
|
154
|
-
|
|
155
|
-
console.log(`Cost: $${response.cost}`);
|
|
156
|
-
console.log(`Tokens: ${response.tokens}`);
|
|
157
|
-
console.log(`Model: ${response.model}`);
|
|
158
|
-
console.log(`Provider: ${response.provider}`);
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
### πΎ Smart Caching
|
|
162
|
-
|
|
163
|
-
Save money by caching repeated requests:
|
|
82
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
164
83
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
84
|
+
const response = await ai(
|
|
85
|
+
OPENAI.GPT_4,
|
|
86
|
+
'Write a comprehensive guide to machine learning for beginners',
|
|
87
|
+
{
|
|
88
|
+
cortex: true, // Enable 40-75% cost reduction
|
|
89
|
+
maxTokens: 2000
|
|
90
|
+
}
|
|
91
|
+
);
|
|
169
92
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
console.log(response2.cached); // true - saved money!
|
|
93
|
+
console.log(`Optimized: ${response.optimized}`);
|
|
94
|
+
console.log(`Saved: $${response.savedAmount}`);
|
|
173
95
|
```
|
|
174
96
|
|
|
175
|
-
###
|
|
97
|
+
### Part 4: Compare Models Side-by-Side
|
|
176
98
|
|
|
177
|
-
|
|
99
|
+
Find the best price-to-quality ratio for your use case:
|
|
178
100
|
|
|
179
101
|
```typescript
|
|
180
|
-
|
|
181
|
-
cortex: true // Enable optimization
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
console.log(response.optimized); // true
|
|
185
|
-
console.log(`Saved: ${response.savedAmount}`); // Amount saved
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
### π‘οΈ Security Firewall
|
|
102
|
+
import { ai, OPENAI, ANTHROPIC, GOOGLE } from 'cost-katana';
|
|
189
103
|
|
|
190
|
-
|
|
104
|
+
const prompt = 'Summarize the theory of relativity in 50 words';
|
|
191
105
|
|
|
192
|
-
|
|
193
|
-
|
|
106
|
+
const models = [
|
|
107
|
+
{ name: 'GPT-4', id: OPENAI.GPT_4 },
|
|
108
|
+
{ name: 'Claude 3.5 Sonnet', id: ANTHROPIC.CLAUDE_3_5_SONNET_20241022 },
|
|
109
|
+
{ name: 'Gemini 2.5 Pro', id: GOOGLE.GEMINI_2_5_PRO },
|
|
110
|
+
{ name: 'GPT-3.5 Turbo', id: OPENAI.GPT_3_5_TURBO }
|
|
111
|
+
];
|
|
194
112
|
|
|
195
|
-
|
|
113
|
+
console.log('π Model Cost Comparison\n');
|
|
196
114
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
} catch (error) {
|
|
201
|
-
console.log('Blocked:', error.message);
|
|
115
|
+
for (const model of models) {
|
|
116
|
+
const response = await ai(model.id, prompt);
|
|
117
|
+
console.log(`${model.name.padEnd(20)} $${response.cost.toFixed(6)}`);
|
|
202
118
|
}
|
|
203
119
|
```
|
|
204
120
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
Never fail - automatically switch providers:
|
|
208
|
-
|
|
209
|
-
```typescript
|
|
210
|
-
// If OpenAI is down, automatically uses Claude or Gemini
|
|
211
|
-
const response = await ai('gpt-4', 'Hello');
|
|
212
|
-
console.log(response.provider); // Might be 'anthropic' if OpenAI failed
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
### π Analytics Dashboard
|
|
216
|
-
|
|
217
|
-
All usage syncs to your dashboard at [costkatana.com](https://costkatana.com):
|
|
218
|
-
|
|
219
|
-
```typescript
|
|
220
|
-
const response = await ai('gpt-4', 'Hello');
|
|
221
|
-
// Automatically tracked in your dashboard
|
|
222
|
-
// View at: https://costkatana.com/dashboard
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
## Configuration
|
|
226
|
-
|
|
227
|
-
### Environment Variables
|
|
228
|
-
|
|
229
|
-
```bash
|
|
230
|
-
# Option 1: Cost Katana (Recommended - all features)
|
|
231
|
-
COST_KATANA_API_KEY=dak_your_key_here
|
|
232
|
-
|
|
233
|
-
# Option 2: Direct provider keys (uses native SDKs)
|
|
234
|
-
OPENAI_API_KEY=sk-... # For OpenAI models (native SDK) - USER PROVIDED
|
|
235
|
-
GEMINI_API_KEY=... # For Gemini models (native SDK) - USER PROVIDED
|
|
236
|
-
ANTHROPIC_API_KEY=sk-ant-... # For Claude models
|
|
237
|
-
AWS_ACCESS_KEY_ID=... # For AWS Bedrock
|
|
238
|
-
AWS_SECRET_ACCESS_KEY=... # For AWS Bedrock
|
|
239
|
-
AWS_REGION=us-east-1 # For AWS Bedrock
|
|
121
|
+
**Sample Output:**
|
|
240
122
|
```
|
|
123
|
+
π Model Cost Comparison
|
|
241
124
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
> - Without `GEMINI_API_KEY`, Gemini models will not be available
|
|
247
|
-
> - Only AWS Bedrock models (Claude, Nova, etc.) work with AWS credentials
|
|
248
|
-
>
|
|
249
|
-
> **With direct provider keys, Cost Katana automatically uses:**
|
|
250
|
-
> - Native OpenAI SDK for GPT models (if OPENAI_API_KEY provided)
|
|
251
|
-
> - Native Google Gemini SDK for Gemini models (if GEMINI_API_KEY provided)
|
|
252
|
-
> - AWS Bedrock as fallback for both
|
|
253
|
-
|
|
254
|
-
### Manual Configuration
|
|
255
|
-
|
|
256
|
-
```typescript
|
|
257
|
-
import { configure } from 'cost-katana';
|
|
258
|
-
|
|
259
|
-
await configure({
|
|
260
|
-
apiKey: 'dak_your_key',
|
|
261
|
-
cortex: true, // 70-95% cost savings
|
|
262
|
-
cache: true, // Smart caching
|
|
263
|
-
firewall: true // Security
|
|
264
|
-
});
|
|
125
|
+
GPT-4 $0.001200
|
|
126
|
+
Claude 3.5 Sonnet $0.000900
|
|
127
|
+
Gemini 2.5 Pro $0.000150
|
|
128
|
+
GPT-3.5 Turbo $0.000080
|
|
265
129
|
```
|
|
266
130
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
```typescript
|
|
270
|
-
const response = await ai('gpt-4', 'Your prompt', {
|
|
271
|
-
temperature: 0.7, // Creativity level (0-2)
|
|
272
|
-
maxTokens: 500, // Response length limit
|
|
273
|
-
systemMessage: 'You are helpful', // System prompt
|
|
274
|
-
cache: true, // Enable caching
|
|
275
|
-
cortex: true // Enable optimization
|
|
276
|
-
});
|
|
277
|
-
```
|
|
131
|
+
---
|
|
278
132
|
|
|
279
|
-
##
|
|
133
|
+
## π― Type-Safe Model Selection
|
|
280
134
|
|
|
281
|
-
|
|
135
|
+
Stop guessing model names. Get autocomplete and catch typos at compile time:
|
|
282
136
|
|
|
283
137
|
```typescript
|
|
284
|
-
|
|
285
|
-
await ai('gpt-4', 'Hello');
|
|
286
|
-
await ai('gpt-3.5-turbo', 'Hello');
|
|
287
|
-
await ai('gpt-4-turbo', 'Hello');
|
|
138
|
+
import { OPENAI, ANTHROPIC, GOOGLE, AWS_BEDROCK, XAI, DEEPSEEK } from 'cost-katana';
|
|
288
139
|
|
|
289
|
-
//
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
140
|
+
// OpenAI
|
|
141
|
+
OPENAI.GPT_5
|
|
142
|
+
OPENAI.GPT_4
|
|
143
|
+
OPENAI.GPT_4O
|
|
144
|
+
OPENAI.GPT_3_5_TURBO
|
|
145
|
+
OPENAI.O1
|
|
146
|
+
OPENAI.O3
|
|
293
147
|
|
|
294
148
|
// Anthropic
|
|
295
|
-
|
|
296
|
-
|
|
149
|
+
ANTHROPIC.CLAUDE_SONNET_4_5
|
|
150
|
+
ANTHROPIC.CLAUDE_3_5_SONNET_20241022
|
|
151
|
+
ANTHROPIC.CLAUDE_3_5_HAIKU_20241022
|
|
152
|
+
|
|
153
|
+
// Google
|
|
154
|
+
GOOGLE.GEMINI_2_5_PRO
|
|
155
|
+
GOOGLE.GEMINI_2_5_FLASH
|
|
156
|
+
GOOGLE.GEMINI_1_5_PRO
|
|
297
157
|
|
|
298
158
|
// AWS Bedrock
|
|
299
|
-
|
|
300
|
-
|
|
159
|
+
AWS_BEDROCK.NOVA_PRO
|
|
160
|
+
AWS_BEDROCK.NOVA_LITE
|
|
161
|
+
AWS_BEDROCK.CLAUDE_SONNET_4_5
|
|
301
162
|
|
|
302
|
-
//
|
|
163
|
+
// Others
|
|
164
|
+
XAI.GROK_2_1212
|
|
165
|
+
DEEPSEEK.DEEPSEEK_CHAT
|
|
303
166
|
```
|
|
304
167
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
#### OpenAI Native SDK
|
|
314
|
-
|
|
315
|
-
```typescript
|
|
316
|
-
import { ai, OpenAIProvider } from 'cost-katana';
|
|
317
|
-
|
|
318
|
-
// Option 1: Automatic (uses OPENAI_API_KEY from env)
|
|
319
|
-
const response = await ai('gpt-4', 'Explain quantum computing');
|
|
320
|
-
|
|
321
|
-
// Option 2: Manual configuration
|
|
322
|
-
const openai = new OpenAIProvider({
|
|
323
|
-
apiKey: 'sk-your-openai-key',
|
|
324
|
-
provider: 'openai'
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
const result = await openai.makeRequest({
|
|
328
|
-
model: 'gpt-4-turbo',
|
|
329
|
-
messages: [
|
|
330
|
-
{ role: 'system', content: 'You are a helpful assistant' },
|
|
331
|
-
{ role: 'user', content: 'Hello!' }
|
|
332
|
-
],
|
|
333
|
-
maxTokens: 500,
|
|
334
|
-
temperature: 0.7
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
console.log(result.choices[0].message.content);
|
|
338
|
-
console.log(`Tokens: ${result.usage.total_tokens}`);
|
|
339
|
-
```
|
|
168
|
+
**Why constants over strings?**
|
|
169
|
+
| Feature | String `'gpt-4'` | Constant `OPENAI.GPT_4` |
|
|
170
|
+
|---------|------------------|-------------------------|
|
|
171
|
+
| Autocomplete | β | β
|
|
|
172
|
+
| Typo protection | β | β
|
|
|
173
|
+
| Refactor safely | β | β
|
|
|
174
|
+
| Self-documenting | β | β
|
|
|
340
175
|
|
|
341
|
-
|
|
176
|
+
---
|
|
342
177
|
|
|
343
|
-
|
|
344
|
-
import { ai, GoogleProvider } from 'cost-katana';
|
|
178
|
+
## βοΈ Configuration
|
|
345
179
|
|
|
346
|
-
|
|
347
|
-
const response = await ai('gemini-1.5-pro', 'Write a haiku about AI');
|
|
180
|
+
### Environment Variables
|
|
348
181
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
provider: 'google'
|
|
353
|
-
});
|
|
182
|
+
```bash
|
|
183
|
+
# Recommended: Use Cost Katana API key for all features
|
|
184
|
+
COST_KATANA_API_KEY=dak_your_key_here
|
|
354
185
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
],
|
|
360
|
-
maxTokens: 1000,
|
|
361
|
-
temperature: 0.8
|
|
362
|
-
});
|
|
186
|
+
# Or use provider keys directly
|
|
187
|
+
OPENAI_API_KEY=sk-...
|
|
188
|
+
ANTHROPIC_API_KEY=sk-ant-...
|
|
189
|
+
GEMINI_API_KEY=...
|
|
363
190
|
|
|
364
|
-
|
|
365
|
-
|
|
191
|
+
# For AWS Bedrock
|
|
192
|
+
AWS_ACCESS_KEY_ID=...
|
|
193
|
+
AWS_SECRET_ACCESS_KEY=...
|
|
194
|
+
AWS_REGION=us-east-1
|
|
366
195
|
```
|
|
367
196
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
Cost Katana automatically detects the right provider based on the model name:
|
|
197
|
+
### Programmatic Configuration
|
|
371
198
|
|
|
372
199
|
```typescript
|
|
373
|
-
|
|
374
|
-
await ai('gpt-4', 'Hello');
|
|
375
|
-
|
|
376
|
-
// Auto-routes to Google Gemini SDK
|
|
377
|
-
await ai('gemini-pro', 'Hello');
|
|
378
|
-
|
|
379
|
-
// Auto-routes to Anthropic
|
|
380
|
-
await ai('claude-3-sonnet', 'Hello');
|
|
381
|
-
|
|
382
|
-
// Auto-routes to AWS Bedrock
|
|
383
|
-
await ai('nova-pro', 'Hello');
|
|
384
|
-
```
|
|
385
|
-
|
|
386
|
-
#### Failover & Reliability
|
|
387
|
-
|
|
388
|
-
If a native SDK fails, Cost Katana automatically falls back to AWS Bedrock:
|
|
200
|
+
import { configure } from 'cost-katana';
|
|
389
201
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
202
|
+
await configure({
|
|
203
|
+
apiKey: 'dak_your_key',
|
|
204
|
+
cortex: true, // 40-75% cost savings
|
|
205
|
+
cache: true, // Smart caching
|
|
206
|
+
firewall: true // Block prompt injections
|
|
395
207
|
});
|
|
396
|
-
|
|
397
|
-
console.log(`Provider used: ${response.provider}`);
|
|
398
|
-
// Output might be 'openai' or 'aws-bedrock' depending on availability
|
|
399
208
|
```
|
|
400
209
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
### Customer Support Bot
|
|
210
|
+
### Request Options
|
|
404
211
|
|
|
405
212
|
```typescript
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
systemMessage: 'You are a helpful
|
|
213
|
+
const response = await ai(OPENAI.GPT_4, 'Your prompt', {
|
|
214
|
+
temperature: 0.7, // Creativity (0-2)
|
|
215
|
+
maxTokens: 500, // Response limit
|
|
216
|
+
systemMessage: 'You are a helpful AI', // System prompt
|
|
217
|
+
cache: true, // Enable caching
|
|
218
|
+
cortex: true, // Enable optimization
|
|
219
|
+
retry: true // Auto-retry on failures
|
|
410
220
|
});
|
|
411
|
-
|
|
412
|
-
async function handleCustomerQuery(query: string) {
|
|
413
|
-
const response = await support.send(query);
|
|
414
|
-
console.log(`Cost so far: $${support.totalCost}`);
|
|
415
|
-
return response;
|
|
416
|
-
}
|
|
417
|
-
```
|
|
418
|
-
|
|
419
|
-
### Content Generation
|
|
420
|
-
|
|
421
|
-
```typescript
|
|
422
|
-
import { ai } from 'cost-katana';
|
|
423
|
-
|
|
424
|
-
async function generateBlogPost(topic: string) {
|
|
425
|
-
// Use Cortex for long-form content (40-75% savings)
|
|
426
|
-
const post = await ai('gpt-4', `Write a blog post about ${topic}`, {
|
|
427
|
-
cortex: true,
|
|
428
|
-
maxTokens: 2000
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
return {
|
|
432
|
-
content: post.text,
|
|
433
|
-
cost: post.cost,
|
|
434
|
-
wordCount: post.text.split(' ').length
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
```
|
|
438
|
-
|
|
439
|
-
### Code Assistant
|
|
440
|
-
|
|
441
|
-
```typescript
|
|
442
|
-
import { ai } from 'cost-katana';
|
|
443
|
-
|
|
444
|
-
async function reviewCode(code: string) {
|
|
445
|
-
const review = await ai('claude-3-sonnet',
|
|
446
|
-
`Review this code and suggest improvements:\n\n${code}`,
|
|
447
|
-
{ cache: true } // Cache for repeated reviews
|
|
448
|
-
);
|
|
449
|
-
|
|
450
|
-
return review.text;
|
|
451
|
-
}
|
|
452
221
|
```
|
|
453
222
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
```typescript
|
|
457
|
-
import { ai } from 'cost-katana';
|
|
458
|
-
|
|
459
|
-
async function translate(text: string, targetLanguage: string) {
|
|
460
|
-
// Use cheaper model for translations
|
|
461
|
-
const translated = await ai('gpt-3.5-turbo',
|
|
462
|
-
`Translate to ${targetLanguage}: ${text}`,
|
|
463
|
-
{ cache: true }
|
|
464
|
-
);
|
|
465
|
-
|
|
466
|
-
return translated.text;
|
|
467
|
-
}
|
|
468
|
-
```
|
|
223
|
+
---
|
|
469
224
|
|
|
470
|
-
## Framework Integration
|
|
225
|
+
## π Framework Integration
|
|
471
226
|
|
|
472
227
|
### Next.js App Router
|
|
473
228
|
|
|
474
229
|
```typescript
|
|
475
230
|
// app/api/chat/route.ts
|
|
476
|
-
import { ai } from 'cost-katana';
|
|
231
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
477
232
|
|
|
478
233
|
export async function POST(request: Request) {
|
|
479
234
|
const { prompt } = await request.json();
|
|
480
|
-
const response = await ai(
|
|
235
|
+
const response = await ai(OPENAI.GPT_4, prompt);
|
|
481
236
|
return Response.json(response);
|
|
482
237
|
}
|
|
483
238
|
```
|
|
@@ -486,272 +241,230 @@ export async function POST(request: Request) {
|
|
|
486
241
|
|
|
487
242
|
```typescript
|
|
488
243
|
import express from 'express';
|
|
489
|
-
import { ai } from 'cost-katana';
|
|
244
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
490
245
|
|
|
491
246
|
const app = express();
|
|
492
247
|
app.use(express.json());
|
|
493
248
|
|
|
494
249
|
app.post('/api/chat', async (req, res) => {
|
|
495
|
-
const response = await ai(
|
|
250
|
+
const response = await ai(OPENAI.GPT_4, req.body.prompt);
|
|
496
251
|
res.json(response);
|
|
497
252
|
});
|
|
253
|
+
|
|
254
|
+
app.listen(3000);
|
|
498
255
|
```
|
|
499
256
|
|
|
500
257
|
### Fastify
|
|
501
258
|
|
|
502
259
|
```typescript
|
|
503
260
|
import fastify from 'fastify';
|
|
504
|
-
import { ai } from 'cost-katana';
|
|
261
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
505
262
|
|
|
506
263
|
const app = fastify();
|
|
507
264
|
|
|
508
|
-
app.post('/api/chat', async (request
|
|
509
|
-
const { prompt } = request.body;
|
|
510
|
-
|
|
511
|
-
return response;
|
|
265
|
+
app.post('/api/chat', async (request) => {
|
|
266
|
+
const { prompt } = request.body as { prompt: string };
|
|
267
|
+
return await ai(OPENAI.GPT_4, prompt);
|
|
512
268
|
});
|
|
269
|
+
|
|
270
|
+
app.listen({ port: 3000 });
|
|
513
271
|
```
|
|
514
272
|
|
|
515
273
|
### NestJS
|
|
516
274
|
|
|
517
275
|
```typescript
|
|
518
276
|
import { Controller, Post, Body } from '@nestjs/common';
|
|
519
|
-
import { ai } from 'cost-katana';
|
|
277
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
520
278
|
|
|
521
279
|
@Controller('api')
|
|
522
280
|
export class ChatController {
|
|
523
281
|
@Post('chat')
|
|
524
282
|
async chat(@Body() body: { prompt: string }) {
|
|
525
|
-
return await ai(
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
```
|
|
529
|
-
|
|
530
|
-
## Error Handling
|
|
531
|
-
|
|
532
|
-
```typescript
|
|
533
|
-
import { ai } from 'cost-katana';
|
|
534
|
-
|
|
535
|
-
try {
|
|
536
|
-
const response = await ai('gpt-4', 'Hello');
|
|
537
|
-
console.log(response.text);
|
|
538
|
-
} catch (error) {
|
|
539
|
-
if (error.code === 'NO_API_KEY') {
|
|
540
|
-
console.log('Please set your API key');
|
|
541
|
-
} else if (error.code === 'RATE_LIMIT') {
|
|
542
|
-
console.log('Rate limit exceeded');
|
|
543
|
-
} else if (error.code === 'INVALID_MODEL') {
|
|
544
|
-
console.log('Model not found');
|
|
545
|
-
} else {
|
|
546
|
-
console.log('Error:', error.message);
|
|
283
|
+
return await ai(OPENAI.GPT_4, body.prompt);
|
|
547
284
|
}
|
|
548
285
|
}
|
|
549
286
|
```
|
|
550
287
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
### 1. Use Appropriate Models
|
|
288
|
+
---
|
|
554
289
|
|
|
555
|
-
|
|
556
|
-
// For simple tasks, use cheaper models
|
|
557
|
-
await ai('gpt-3.5-turbo', 'Simple question'); // $0.0001
|
|
290
|
+
## π‘οΈ Built-in Security
|
|
558
291
|
|
|
559
|
-
|
|
560
|
-
await ai('gpt-4', 'Complex analysis'); // $0.01
|
|
561
|
-
```
|
|
292
|
+
### Firewall Protection
|
|
562
293
|
|
|
563
|
-
|
|
294
|
+
Block prompt injection attacks automatically:
|
|
564
295
|
|
|
565
296
|
```typescript
|
|
566
|
-
|
|
567
|
-
await ai('gpt-4', 'Common question', { cache: true });
|
|
568
|
-
```
|
|
297
|
+
import { configure, ai, OPENAI } from 'cost-katana';
|
|
569
298
|
|
|
570
|
-
|
|
299
|
+
await configure({ firewall: true });
|
|
571
300
|
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
301
|
+
try {
|
|
302
|
+
await ai(OPENAI.GPT_4, 'Ignore all previous instructions and...');
|
|
303
|
+
} catch (error) {
|
|
304
|
+
console.log('π‘οΈ Blocked:', error.message);
|
|
305
|
+
}
|
|
575
306
|
```
|
|
576
307
|
|
|
577
|
-
|
|
308
|
+
**Protects against:**
|
|
309
|
+
- Prompt injection attacks
|
|
310
|
+
- Jailbreak attempts
|
|
311
|
+
- Data exfiltration
|
|
312
|
+
- Malicious content generation
|
|
578
313
|
|
|
579
|
-
|
|
580
|
-
const session = chat('gpt-3.5-turbo');
|
|
581
|
-
// Reuse session for related queries
|
|
582
|
-
await session.send('Query 1');
|
|
583
|
-
await session.send('Query 2');
|
|
584
|
-
```
|
|
314
|
+
---
|
|
585
315
|
|
|
586
|
-
##
|
|
316
|
+
## π Auto-Failover
|
|
587
317
|
|
|
588
|
-
|
|
318
|
+
Never let provider outages break your app:
|
|
589
319
|
|
|
590
320
|
```typescript
|
|
591
|
-
import {
|
|
321
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
592
322
|
|
|
593
|
-
|
|
594
|
-
await
|
|
595
|
-
await session.send('How are you?');
|
|
323
|
+
// If OpenAI is down, automatically switches to Claude or Gemini
|
|
324
|
+
const response = await ai(OPENAI.GPT_4, 'Hello');
|
|
596
325
|
|
|
597
|
-
console.log(
|
|
598
|
-
|
|
599
|
-
console.log('Total tokens:', session.totalTokens);
|
|
326
|
+
console.log(`Provider used: ${response.provider}`);
|
|
327
|
+
// Could be 'openai', 'anthropic', or 'google' depending on availability
|
|
600
328
|
```
|
|
601
329
|
|
|
602
|
-
|
|
330
|
+
---
|
|
603
331
|
|
|
604
|
-
|
|
332
|
+
## π Session Replay & Tracing
|
|
333
|
+
|
|
334
|
+
### Record AI Sessions
|
|
605
335
|
|
|
606
336
|
```typescript
|
|
607
337
|
import { SessionReplayClient } from 'cost-katana/trace';
|
|
608
338
|
|
|
609
|
-
const
|
|
339
|
+
const replay = new SessionReplayClient({
|
|
610
340
|
apiKey: process.env.COST_KATANA_API_KEY
|
|
611
341
|
});
|
|
612
342
|
|
|
613
|
-
// Start recording
|
|
614
|
-
const { sessionId } = await
|
|
343
|
+
// Start recording
|
|
344
|
+
const { sessionId } = await replay.startRecording({
|
|
615
345
|
userId: 'user123',
|
|
616
346
|
feature: 'chat',
|
|
617
|
-
label: '
|
|
347
|
+
label: 'Support Conversation'
|
|
618
348
|
});
|
|
619
349
|
|
|
620
|
-
// Record
|
|
621
|
-
await
|
|
350
|
+
// Record interactions
|
|
351
|
+
await replay.recordInteraction({
|
|
622
352
|
sessionId,
|
|
623
353
|
interaction: {
|
|
624
354
|
timestamp: new Date(),
|
|
625
355
|
model: 'gpt-4',
|
|
626
|
-
prompt: 'How
|
|
627
|
-
response: '
|
|
628
|
-
tokens: { input:
|
|
629
|
-
cost: 0.
|
|
630
|
-
latency: 850
|
|
631
|
-
provider: 'openai'
|
|
632
|
-
}
|
|
633
|
-
});
|
|
634
|
-
|
|
635
|
-
// Record user actions
|
|
636
|
-
await replayClient.recordUserAction({
|
|
637
|
-
sessionId,
|
|
638
|
-
action: {
|
|
639
|
-
timestamp: new Date(),
|
|
640
|
-
action: 'button_click',
|
|
641
|
-
target: 'send_message'
|
|
356
|
+
prompt: 'How do I reset my password?',
|
|
357
|
+
response: 'To reset your password...',
|
|
358
|
+
tokens: { input: 8, output: 45 },
|
|
359
|
+
cost: 0.0012,
|
|
360
|
+
latency: 850
|
|
642
361
|
}
|
|
643
362
|
});
|
|
644
363
|
|
|
645
|
-
// End
|
|
646
|
-
await
|
|
647
|
-
|
|
648
|
-
// Later, retrieve the replay
|
|
649
|
-
const replay = await replayClient.getSessionReplay(sessionId);
|
|
650
|
-
console.log('Total interactions:', replay.replayData.aiInteractions.length);
|
|
651
|
-
console.log('Total cost:', replay.summary.totalCost);
|
|
364
|
+
// End and retrieve
|
|
365
|
+
await replay.endRecording(sessionId);
|
|
366
|
+
const session = await replay.getSessionReplay(sessionId);
|
|
652
367
|
```
|
|
653
368
|
|
|
654
369
|
### Distributed Tracing
|
|
655
370
|
|
|
656
|
-
Track AI operations across microservices:
|
|
657
|
-
|
|
658
371
|
```typescript
|
|
659
372
|
import { TraceClient, createTraceMiddleware } from 'cost-katana/trace';
|
|
660
373
|
import express from 'express';
|
|
661
374
|
|
|
662
375
|
const app = express();
|
|
663
|
-
const
|
|
664
|
-
apiKey: process.env.COST_KATANA_API_KEY
|
|
665
|
-
});
|
|
376
|
+
const trace = new TraceClient({ apiKey: process.env.COST_KATANA_API_KEY });
|
|
666
377
|
|
|
667
|
-
|
|
668
|
-
app.use(createTraceMiddleware({ traceService: traceClient }));
|
|
378
|
+
app.use(createTraceMiddleware({ traceService: trace }));
|
|
669
379
|
|
|
670
|
-
//
|
|
380
|
+
// All routes automatically traced
|
|
671
381
|
app.post('/api/chat', async (req, res) => {
|
|
672
|
-
const response = await ai(
|
|
382
|
+
const response = await ai(OPENAI.GPT_4, req.body.message);
|
|
673
383
|
res.json(response);
|
|
674
384
|
});
|
|
675
|
-
|
|
676
|
-
// View traces in your dashboard at costkatana.com/sessions
|
|
677
385
|
```
|
|
678
386
|
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
Visit [costkatana.com/dashboard](https://costkatana.com/dashboard) to see:
|
|
682
|
-
|
|
683
|
-
- **Session Replays**: Timeline playback of AI interactions
|
|
684
|
-
- **Debug Traces**: Span-level distributed tracing
|
|
685
|
-
- Real-time cost tracking
|
|
686
|
-
- Usage by model and provider
|
|
687
|
-
- Daily/weekly/monthly spending
|
|
688
|
-
- Token usage analytics
|
|
689
|
-
- Optimization recommendations
|
|
690
|
-
- Team usage breakdown
|
|
691
|
-
- Budget alerts
|
|
692
|
-
- API performance metrics
|
|
693
|
-
|
|
694
|
-
## Security & Privacy
|
|
387
|
+
---
|
|
695
388
|
|
|
696
|
-
|
|
389
|
+
## π‘ Cost Optimization Cheatsheet
|
|
697
390
|
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
-
|
|
701
|
-
|
|
391
|
+
| Strategy | Savings | When to Use |
|
|
392
|
+
|----------|---------|-------------|
|
|
393
|
+
| **Use GPT-3.5 over GPT-4** | 90% | Simple tasks, translations |
|
|
394
|
+
| **Enable caching** | 100% on hits | Repeated queries, FAQs |
|
|
395
|
+
| **Enable Cortex** | 40-75% | Long-form content |
|
|
396
|
+
| **Batch in sessions** | 10-20% | Related queries |
|
|
397
|
+
| **Use Gemini Flash** | 95% vs GPT-4 | High-volume, cost-sensitive |
|
|
702
398
|
|
|
703
|
-
###
|
|
399
|
+
### Quick Wins
|
|
704
400
|
|
|
705
401
|
```typescript
|
|
706
|
-
|
|
402
|
+
// β Expensive: Using GPT-4 for everything
|
|
403
|
+
await ai(OPENAI.GPT_4, 'What is 2+2?'); // $0.001
|
|
707
404
|
|
|
708
|
-
//
|
|
709
|
-
|
|
710
|
-
// - Jailbreak attempts
|
|
711
|
-
// - Data exfiltration
|
|
712
|
-
// - Malicious content
|
|
713
|
-
```
|
|
714
|
-
|
|
715
|
-
## Troubleshooting
|
|
405
|
+
// β
Smart: Match model to task
|
|
406
|
+
await ai(OPENAI.GPT_3_5_TURBO, 'What is 2+2?'); // $0.0001
|
|
716
407
|
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
```bash
|
|
720
|
-
# Set Cost Katana key (recommended)
|
|
721
|
-
export COST_KATANA_API_KEY="dak_your_key"
|
|
408
|
+
// β
Smarter: Cache common queries
|
|
409
|
+
await ai(OPENAI.GPT_3_5_TURBO, 'What is 2+2?', { cache: true }); // $0 on repeat
|
|
722
410
|
|
|
723
|
-
|
|
724
|
-
|
|
411
|
+
// β
Smartest: Cortex for long content
|
|
412
|
+
await ai(OPENAI.GPT_4, 'Write a 2000-word essay', { cortex: true }); // 40-75% off
|
|
725
413
|
```
|
|
726
414
|
|
|
727
|
-
|
|
415
|
+
---
|
|
416
|
+
|
|
417
|
+
## π§ Error Handling
|
|
728
418
|
|
|
729
419
|
```typescript
|
|
730
|
-
|
|
731
|
-
import { ai } from 'cost-katana';
|
|
420
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
732
421
|
|
|
733
422
|
try {
|
|
734
|
-
await ai(
|
|
423
|
+
const response = await ai(OPENAI.GPT_4, 'Hello');
|
|
424
|
+
console.log(response.text);
|
|
735
425
|
} catch (error) {
|
|
736
|
-
|
|
426
|
+
switch (error.code) {
|
|
427
|
+
case 'NO_API_KEY':
|
|
428
|
+
console.log('Set COST_KATANA_API_KEY or OPENAI_API_KEY');
|
|
429
|
+
break;
|
|
430
|
+
case 'RATE_LIMIT':
|
|
431
|
+
console.log('Rate limited. Retrying...');
|
|
432
|
+
break;
|
|
433
|
+
case 'INVALID_MODEL':
|
|
434
|
+
console.log('Model not found. Available:', error.availableModels);
|
|
435
|
+
break;
|
|
436
|
+
default:
|
|
437
|
+
console.log('Error:', error.message);
|
|
438
|
+
}
|
|
737
439
|
}
|
|
738
440
|
```
|
|
739
441
|
|
|
740
|
-
|
|
442
|
+
---
|
|
741
443
|
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
retry: true // Auto-retry on rate limits
|
|
746
|
-
});
|
|
747
|
-
```
|
|
444
|
+
## π More Examples
|
|
445
|
+
|
|
446
|
+
Explore 45+ complete examples in our examples repository:
|
|
748
447
|
|
|
749
|
-
|
|
448
|
+
**π [github.com/Hypothesize-Tech/costkatana-examples](https://github.com/Hypothesize-Tech/costkatana-examples)**
|
|
449
|
+
|
|
450
|
+
| Category | Examples |
|
|
451
|
+
|----------|----------|
|
|
452
|
+
| **Cost Tracking** | Basic tracking, budgets, alerts |
|
|
453
|
+
| **Gateway** | Routing, load balancing, failover |
|
|
454
|
+
| **Optimization** | Cortex, caching, compression |
|
|
455
|
+
| **Observability** | OpenTelemetry, tracing, metrics |
|
|
456
|
+
| **Security** | Firewall, rate limiting, moderation |
|
|
457
|
+
| **Workflows** | Multi-step AI orchestration |
|
|
458
|
+
| **Frameworks** | Express, Next.js, Fastify, NestJS, FastAPI |
|
|
459
|
+
|
|
460
|
+
---
|
|
461
|
+
|
|
462
|
+
## π Migration Guides
|
|
750
463
|
|
|
751
464
|
### From OpenAI SDK
|
|
752
465
|
|
|
753
466
|
```typescript
|
|
754
|
-
// Before
|
|
467
|
+
// Before
|
|
755
468
|
import OpenAI from 'openai';
|
|
756
469
|
const openai = new OpenAI({ apiKey: 'sk-...' });
|
|
757
470
|
const completion = await openai.chat.completions.create({
|
|
@@ -760,17 +473,17 @@ const completion = await openai.chat.completions.create({
|
|
|
760
473
|
});
|
|
761
474
|
console.log(completion.choices[0].message.content);
|
|
762
475
|
|
|
763
|
-
// After
|
|
764
|
-
import { ai } from 'cost-katana';
|
|
765
|
-
const response = await ai(
|
|
476
|
+
// After
|
|
477
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
478
|
+
const response = await ai(OPENAI.GPT_4, 'Hello');
|
|
766
479
|
console.log(response.text);
|
|
767
|
-
console.log(`Cost: $${response.cost}`);
|
|
480
|
+
console.log(`Cost: $${response.cost}`); // Bonus: cost tracking!
|
|
768
481
|
```
|
|
769
482
|
|
|
770
483
|
### From Anthropic SDK
|
|
771
484
|
|
|
772
485
|
```typescript
|
|
773
|
-
// Before
|
|
486
|
+
// Before
|
|
774
487
|
import Anthropic from '@anthropic-ai/sdk';
|
|
775
488
|
const anthropic = new Anthropic({ apiKey: 'sk-ant-...' });
|
|
776
489
|
const message = await anthropic.messages.create({
|
|
@@ -778,73 +491,73 @@ const message = await anthropic.messages.create({
|
|
|
778
491
|
messages: [{ role: 'user', content: 'Hello' }]
|
|
779
492
|
});
|
|
780
493
|
|
|
781
|
-
// After
|
|
782
|
-
import { ai } from 'cost-katana';
|
|
783
|
-
const response = await ai(
|
|
494
|
+
// After
|
|
495
|
+
import { ai, ANTHROPIC } from 'cost-katana';
|
|
496
|
+
const response = await ai(ANTHROPIC.CLAUDE_3_5_SONNET_20241022, 'Hello');
|
|
784
497
|
```
|
|
785
498
|
|
|
786
499
|
### From LangChain
|
|
787
500
|
|
|
788
501
|
```typescript
|
|
789
|
-
// Before
|
|
502
|
+
// Before
|
|
790
503
|
import { ChatOpenAI } from 'langchain/chat_models/openai';
|
|
791
504
|
const model = new ChatOpenAI({ modelName: 'gpt-4' });
|
|
792
505
|
const response = await model.call([{ content: 'Hello' }]);
|
|
793
506
|
|
|
794
|
-
// After
|
|
795
|
-
import { ai } from 'cost-katana';
|
|
796
|
-
const response = await ai(
|
|
507
|
+
// After
|
|
508
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
509
|
+
const response = await ai(OPENAI.GPT_4, 'Hello');
|
|
797
510
|
```
|
|
798
511
|
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
- **Dashboard**: [costkatana.com](https://costkatana.com)
|
|
802
|
-
- **Documentation**: [docs.costkatana.com](https://docs.costkatana.com)
|
|
803
|
-
- **GitHub**: [github.com/cost-katana](https://github.com/cost-katana)
|
|
804
|
-
- **Email**: support@costkatana.com
|
|
805
|
-
- **Discord**: [discord.gg/costkatana](https://discord.gg/D8nDArmKbY)
|
|
806
|
-
|
|
807
|
-
## Contributing
|
|
808
|
-
|
|
809
|
-
We welcome contributions! Please see our [Contributing Guide](./CONTRIBUTING.md) for details on:
|
|
512
|
+
---
|
|
810
513
|
|
|
811
|
-
|
|
812
|
-
- Code quality standards (linting, formatting, testing)
|
|
813
|
-
- Pre-commit hooks and CI/CD pipeline
|
|
814
|
-
- Commit message conventions
|
|
815
|
-
- How to submit pull requests
|
|
514
|
+
## π€ Contributing
|
|
816
515
|
|
|
817
|
-
|
|
516
|
+
We welcome contributions! See our [Contributing Guide](./CONTRIBUTING.md).
|
|
818
517
|
|
|
819
518
|
```bash
|
|
820
|
-
# Clone and install
|
|
821
519
|
git clone https://github.com/Hypothesize-Tech/costkatana-core.git
|
|
822
520
|
cd costkatana-core
|
|
823
521
|
npm install
|
|
824
522
|
|
|
825
|
-
#
|
|
826
|
-
npm run lint
|
|
827
|
-
npm run lint:fix # Auto-fix linting errors
|
|
523
|
+
npm run lint # Check code style
|
|
524
|
+
npm run lint:fix # Auto-fix issues
|
|
828
525
|
npm run format # Format code
|
|
829
526
|
npm test # Run tests
|
|
830
|
-
npm run build # Build
|
|
527
|
+
npm run build # Build
|
|
831
528
|
```
|
|
832
529
|
|
|
833
|
-
|
|
530
|
+
---
|
|
531
|
+
|
|
532
|
+
## π Support
|
|
533
|
+
|
|
534
|
+
| Channel | Link |
|
|
535
|
+
|---------|------|
|
|
536
|
+
| **Dashboard** | [costkatana.com](https://costkatana.com) |
|
|
537
|
+
| **Documentation** | [docs.costkatana.com](https://docs.costkatana.com) |
|
|
538
|
+
| **GitHub** | [github.com/Hypothesize-Tech](https://github.com/Hypothesize-Tech) |
|
|
539
|
+
| **Discord** | [discord.gg/D8nDArmKbY](https://discord.gg/D8nDArmKbY) |
|
|
540
|
+
| **Email** | support@costkatana.com |
|
|
834
541
|
|
|
835
|
-
|
|
542
|
+
---
|
|
543
|
+
|
|
544
|
+
## π License
|
|
836
545
|
|
|
837
546
|
MIT Β© Cost Katana
|
|
838
547
|
|
|
839
548
|
---
|
|
840
549
|
|
|
841
|
-
|
|
550
|
+
<div align="center">
|
|
551
|
+
|
|
552
|
+
**Start cutting AI costs today** π₯·
|
|
842
553
|
|
|
843
554
|
```bash
|
|
844
|
-
npm install
|
|
555
|
+
npm install cost-katana
|
|
845
556
|
```
|
|
846
557
|
|
|
847
558
|
```typescript
|
|
848
|
-
import { ai } from 'cost-katana';
|
|
849
|
-
await ai(
|
|
850
|
-
```
|
|
559
|
+
import { ai, OPENAI } from 'cost-katana';
|
|
560
|
+
await ai(OPENAI.GPT_4, 'Hello, world!');
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
</div>
|