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 CHANGED
@@ -1,483 +1,238 @@
1
1
  # Cost Katana πŸ₯·
2
2
 
3
- **AI that just works. With automatic cost tracking.**
3
+ > **Cut your AI costs in half. Without cutting corners.**
4
4
 
5
- ```typescript
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
- // NEW: Type-safe model selection (recommended)
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
- That's it. No setup. No configuration. No complexity. **And no typos!**
9
+ ## πŸš€ Get Started in 60 Seconds
15
10
 
16
- ## Installation
11
+ ### Step 1: Install
17
12
 
18
13
  ```bash
19
14
  npm install cost-katana
20
15
  ```
21
16
 
22
- > **Note**: Package was previously named `ai-cost-tracker`. If you're upgrading, uninstall the old package first:
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 { chat, OPENAI } from 'cost-katana';
20
+ import { ai, OPENAI } from 'cost-katana';
51
21
 
52
- const session = chat(OPENAI.GPT_4);
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(`Total cost: $${session.totalCost}`);
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
- **Looking for more comprehensive examples?** Check out our complete examples repository
65
-
66
- **πŸ”— [github.com/Hypothesize-Tech/costkatana-examples](https://github.com/Hypothesize-Tech/costkatana-examples)**
31
+ ---
67
32
 
68
- **What's included:**
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
- ### Compare Models
40
+ ### Part 1: Basic Chat Session
87
41
 
88
42
  ```typescript
89
- import { ai, OPENAI, ANTHROPIC, GOOGLE } from 'cost-katana';
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
- for (const { name, constant } of models) {
99
- const response = await ai(constant, prompt);
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
- ### πŸ”„ Migration Guide (from string model names)
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
- **Old way (deprecated, still works):**
107
- ```typescript
108
- // String model names (shows deprecation warning)
109
- await ai('gpt-4', 'Hello');
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
- **New way (recommended):**
114
- ```typescript
115
- import { ai, OPENAI, ANTHROPIC } from 'cost-katana';
59
+ ### Part 2: Add Smart Caching
116
60
 
117
- // Type-safe constants with autocomplete
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
- // OpenAI Models
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
- // AWS Bedrock Models
138
- AWS_BEDROCK.NOVA_PRO, AWS_BEDROCK.NOVA_LITE, AWS_BEDROCK.CLAUDE_SONNET_4_5, ...
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
- // Other Providers
141
- XAI.GROK_2_1212, DEEPSEEK.DEEPSEEK_CHAT, MISTRAL.MISTRAL_LARGE_LATEST,
142
- COHERE.COMMAND_R_PLUS, GROQ.LLAMA_3_3_70B_VERSATILE, META.LLAMA_3_3_70B_INSTRUCT, ...
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
- > **Note**: String model names will continue to work but show a deprecation warning. They will be removed in a future major version.
146
-
147
- ## Features
148
-
149
- ### πŸ’° Cost Tracking
77
+ ### Part 3: Enable Cortex Optimization
150
78
 
151
- Every response includes cost information:
79
+ For long-form content, Cortex compresses prompts intelligently:
152
80
 
153
81
  ```typescript
154
- const response = await ai('gpt-4', 'Write a story');
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
- ```typescript
166
- // First call - costs money
167
- const response1 = await ai('gpt-4', 'What is 2+2?', { cache: true });
168
- console.log(response1.cached); // false
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
- // Second call - free from cache
171
- const response2 = await ai('gpt-4', 'What is 2+2?', { cache: true });
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
- ### ⚑ Cortex Optimization
97
+ ### Part 4: Compare Models Side-by-Side
176
98
 
177
- Reduce costs by 70-95% with Cortex:
99
+ Find the best price-to-quality ratio for your use case:
178
100
 
179
101
  ```typescript
180
- const response = await ai('gpt-4', 'Write a complete guide to Python', {
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
- Block malicious prompts automatically:
104
+ const prompt = 'Summarize the theory of relativity in 50 words';
191
105
 
192
- ```typescript
193
- import { configure } from 'cost-katana';
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
- await configure({ firewall: true });
113
+ console.log('πŸ“Š Model Cost Comparison\n');
196
114
 
197
- // Malicious prompts are blocked
198
- try {
199
- await ai('gpt-4', 'ignore instructions and...');
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
- ### πŸ”„ Auto-Failover
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
- > **⚠️ Important for Self-Hosted Users**:
243
- > - **OpenAI and Gemini providers require YOUR OWN API keys**
244
- > - Cost Katana **does not provide** OpenAI or Google API keys
245
- > - Without `OPENAI_API_KEY`, OpenAI models (GPT-*) will not be available
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
- ### Advanced Options
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
- ## Multi-Provider Support
133
+ ## 🎯 Type-Safe Model Selection
280
134
 
281
- Works with all major AI providers with **native SDK support** for optimal performance:
135
+ Stop guessing model names. Get autocomplete and catch typos at compile time:
282
136
 
283
137
  ```typescript
284
- // OpenAI (Native SDK)
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
- // Google Gemini (Native SDK)
290
- await ai('gemini-pro', 'Hello');
291
- await ai('gemini-1.5-flash', 'Hello');
292
- await ai('gemini-1.5-pro', 'Hello');
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
- await ai('claude-3-sonnet', 'Hello');
296
- await ai('claude-3-haiku', 'Hello');
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
- await ai('nova-pro', 'Hello');
300
- await ai('nova-lite', 'Hello');
159
+ AWS_BEDROCK.NOVA_PRO
160
+ AWS_BEDROCK.NOVA_LITE
161
+ AWS_BEDROCK.CLAUDE_SONNET_4_5
301
162
 
302
- // And many more...
163
+ // Others
164
+ XAI.GROK_2_1212
165
+ DEEPSEEK.DEEPSEEK_CHAT
303
166
  ```
304
167
 
305
- ### Native SDK Integration
306
-
307
- Cost Katana uses **native SDKs** for OpenAI and Google Gemini, providing:
308
- - βœ… **Better Performance** - Direct API calls, no middleman
309
- - βœ… **Lower Latency** - Optimized request/response handling
310
- - βœ… **Automatic Failover** - Falls back to AWS Bedrock if native SDK fails
311
- - βœ… **Full Feature Support** - Access to all provider-specific features
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
- #### Google Gemini Native SDK
176
+ ---
342
177
 
343
- ```typescript
344
- import { ai, GoogleProvider } from 'cost-katana';
178
+ ## βš™οΈ Configuration
345
179
 
346
- // Option 1: Automatic (uses GOOGLE_API_KEY from env)
347
- const response = await ai('gemini-1.5-pro', 'Write a haiku about AI');
180
+ ### Environment Variables
348
181
 
349
- // Option 2: Manual configuration
350
- const gemini = new GoogleProvider({
351
- apiKey: 'your-google-ai-key',
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
- const result = await gemini.makeRequest({
356
- model: 'gemini-1.5-flash',
357
- messages: [
358
- { role: 'user', content: 'Explain machine learning' }
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
- console.log(result.choices[0].message.content);
365
- console.log(`Tokens: ${result.usage.totalTokenCount}`);
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
- #### Provider Auto-Detection
369
-
370
- Cost Katana automatically detects the right provider based on the model name:
197
+ ### Programmatic Configuration
371
198
 
372
199
  ```typescript
373
- // Auto-routes to OpenAI SDK
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
- ```typescript
391
- // Primary: OpenAI SDK
392
- // Fallback: AWS Bedrock (if OpenAI fails)
393
- const response = await ai('gpt-4', 'Hello', {
394
- fallback: true // Enable automatic failover
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
- ## Real-World Examples
402
-
403
- ### Customer Support Bot
210
+ ### Request Options
404
211
 
405
212
  ```typescript
406
- import { chat } from 'cost-katana';
407
-
408
- const support = chat('gpt-3.5-turbo', {
409
- systemMessage: 'You are a helpful customer support agent.'
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
- ### Translation Service
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('gpt-4', prompt);
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('gpt-4', req.body.prompt);
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, reply) => {
509
- const { prompt } = request.body;
510
- const response = await ai('gpt-4', prompt);
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('gpt-4', body.prompt);
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
- ## Cost Optimization Tips
552
-
553
- ### 1. Use Appropriate Models
288
+ ---
554
289
 
555
- ```typescript
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
- // For complex tasks, use powerful models
560
- await ai('gpt-4', 'Complex analysis'); // $0.01
561
- ```
292
+ ### Firewall Protection
562
293
 
563
- ### 2. Enable Caching
294
+ Block prompt injection attacks automatically:
564
295
 
565
296
  ```typescript
566
- // Cache repeated queries
567
- await ai('gpt-4', 'Common question', { cache: true });
568
- ```
297
+ import { configure, ai, OPENAI } from 'cost-katana';
569
298
 
570
- ### 3. Use Cortex for Long Content
299
+ await configure({ firewall: true });
571
300
 
572
- ```typescript
573
- // 70-95% savings on long-form content
574
- await ai('gpt-4', 'Write a book chapter', { cortex: true });
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
- ### 4. Batch Similar Requests
308
+ **Protects against:**
309
+ - Prompt injection attacks
310
+ - Jailbreak attempts
311
+ - Data exfiltration
312
+ - Malicious content generation
578
313
 
579
- ```typescript
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
- ## Monitoring & Analytics
316
+ ## πŸ”„ Auto-Failover
587
317
 
588
- ### Track Usage
318
+ Never let provider outages break your app:
589
319
 
590
320
  ```typescript
591
- import { chat } from 'cost-katana';
321
+ import { ai, OPENAI } from 'cost-katana';
592
322
 
593
- const session = chat('gpt-4');
594
- await session.send('Hello');
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('Messages:', session.messages.length);
598
- console.log('Total cost:', session.totalCost);
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
- ### Session Replay & Recording
330
+ ---
603
331
 
604
- Record and replay AI interactions for debugging and analysis:
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 replayClient = new SessionReplayClient({
339
+ const replay = new SessionReplayClient({
610
340
  apiKey: process.env.COST_KATANA_API_KEY
611
341
  });
612
342
 
613
- // Start recording a chat session
614
- const { sessionId } = await replayClient.startRecording({
343
+ // Start recording
344
+ const { sessionId } = await replay.startRecording({
615
345
  userId: 'user123',
616
346
  feature: 'chat',
617
- label: 'Customer Support Chat'
347
+ label: 'Support Conversation'
618
348
  });
619
349
 
620
- // Record AI interactions
621
- await replayClient.recordInteraction({
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 can I help you?',
627
- response: 'I need assistance with...',
628
- tokens: { input: 10, output: 20 },
629
- cost: 0.0015,
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 recording
646
- await replayClient.endRecording(sessionId);
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 traceClient = new TraceClient({
664
- apiKey: process.env.COST_KATANA_API_KEY
665
- });
376
+ const trace = new TraceClient({ apiKey: process.env.COST_KATANA_API_KEY });
666
377
 
667
- // Add tracing middleware
668
- app.use(createTraceMiddleware({ traceService: traceClient }));
378
+ app.use(createTraceMiddleware({ traceService: trace }));
669
379
 
670
- // Your routes automatically get traced
380
+ // All routes automatically traced
671
381
  app.post('/api/chat', async (req, res) => {
672
- const response = await ai('gpt-4', req.body.message);
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
- ### Dashboard Features
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
- ### Data Protection
389
+ ## πŸ’‘ Cost Optimization Cheatsheet
697
390
 
698
- - All API keys encrypted at rest
699
- - No prompt/response logging without permission
700
- - GDPR compliant data handling
701
- - SOC2 Type II certified infrastructure
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
- ### Firewall Protection
399
+ ### Quick Wins
704
400
 
705
401
  ```typescript
706
- await configure({ firewall: true });
402
+ // ❌ Expensive: Using GPT-4 for everything
403
+ await ai(OPENAI.GPT_4, 'What is 2+2?'); // $0.001
707
404
 
708
- // Automatically blocks:
709
- // - Prompt injection attacks
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
- ### No API Keys Found
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
- # Or set provider keys directly
724
- export OPENAI_API_KEY="sk-..."
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
- ### Model Not Available
415
+ ---
416
+
417
+ ## πŸ”§ Error Handling
728
418
 
729
419
  ```typescript
730
- // Check available models
731
- import { ai } from 'cost-katana';
420
+ import { ai, OPENAI } from 'cost-katana';
732
421
 
733
422
  try {
734
- await ai('model-name', 'test');
423
+ const response = await ai(OPENAI.GPT_4, 'Hello');
424
+ console.log(response.text);
735
425
  } catch (error) {
736
- console.log('Available models:', error.availableModels);
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
- ### Rate Limits
442
+ ---
741
443
 
742
- ```typescript
743
- // Automatic retry with backoff
744
- const response = await ai('gpt-4', 'Hello', {
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
- ## Migration Guide
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 (OpenAI SDK)
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 (Cost Katana)
764
- import { ai } from 'cost-katana';
765
- const response = await ai('gpt-4', 'Hello');
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}`); // Bonus: cost tracking!
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 (Anthropic SDK)
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 (Cost Katana)
782
- import { ai } from 'cost-katana';
783
- const response = await ai('claude-3-sonnet', 'Hello');
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 (LangChain)
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 (Cost Katana)
795
- import { ai } from 'cost-katana';
796
- const response = await ai('gpt-4', 'Hello');
507
+ // After
508
+ import { ai, OPENAI } from 'cost-katana';
509
+ const response = await ai(OPENAI.GPT_4, 'Hello');
797
510
  ```
798
511
 
799
- ## Support
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
- - Development setup and workflow
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
- ### Quick Development Start
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
- # Development commands
826
- npm run lint # Check linting
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 the project
527
+ npm run build # Build
831
528
  ```
832
529
 
833
- **Code Quality**: We maintain **0 linting errors** with automatic pre-commit hooks. All commits are checked for code quality before merging.
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
- ## License
542
+ ---
543
+
544
+ ## πŸ“„ License
836
545
 
837
546
  MIT Β© Cost Katana
838
547
 
839
548
  ---
840
549
 
841
- **Start saving on AI costs today!**
550
+ <div align="center">
551
+
552
+ **Start cutting AI costs today** πŸ₯·
842
553
 
843
554
  ```bash
844
- npm install ai-cost-tracker
555
+ npm install cost-katana
845
556
  ```
846
557
 
847
558
  ```typescript
848
- import { ai } from 'cost-katana';
849
- await ai('gpt-4', 'Hello, world!');
850
- ```
559
+ import { ai, OPENAI } from 'cost-katana';
560
+ await ai(OPENAI.GPT_4, 'Hello, world!');
561
+ ```
562
+
563
+ </div>