The simplest way to make AI requests with automatic cost tracking.
import { ai } from 'cost-katana';
const response = await ai(model, prompt, options?);Parameters:
model(string): AI model name (e.g., 'gpt-4', 'claude-3-sonnet')prompt(string): Your prompt textoptions(object, optional):systemMessage(string): System prompttemperature(number): 0-2, default 0.7maxTokens(number): Max response tokens, default 1000cache(boolean): Enable caching, default falsecortex(boolean): Enable 40-75% optimization, default false
Returns:
{
text: string; // AI response
cost: number; // Cost in USD
tokens: number; // Total tokens used
model: string; // Model used
provider: string; // Provider name
cached?: boolean; // Whether response was cached
optimized?: boolean; // Whether Cortex was used
}Example:
const response = await ai('gpt-4', 'Explain quantum computing', {
temperature: 0.7,
maxTokens: 500,
cache: true,
cortex: true
});
console.log(response.text);
console.log(`Cost: $${response.cost}`);Create a chat session with conversation history and cost tracking.
import { chat } from 'cost-katana';
const session = chat(model, options?);Parameters:
model(string): AI model nameoptions(object, optional):systemMessage(string): System prompt for the sessiontemperature(number): 0-2, default 0.7maxTokens(number): Max tokens per response
Returns: Session object with methods:
send(message: string): Send a message and get responsemessages: Array of all messages in the conversationtotalCost: Total cost of the sessiontotalTokens: Total tokens used in the sessionclear(): Reset the conversation
Example:
const session = chat('gpt-4', {
systemMessage: 'You are a helpful coding assistant.'
});
await session.send('Hello!');
await session.send('Help me with Python');
await session.send('Show me an example');
console.log(`Total cost: $${session.totalCost}`);
console.log(`Messages: ${session.messages.length}`);Optional manual configuration.
import { configure } from 'cost-katana';
await configure(options);Parameters:
apiKey(string): Cost Katana API keyprojectId(string): Project IDcortex(boolean): Enable Cortex optimization globallycache(boolean): Enable caching globallyfirewall(boolean): Enable security firewallproviders(array): Provider configurationsname(string): Provider name ('openai', 'anthropic', etc.)apiKey(string): Provider API key
Example:
await configure({
apiKey: 'dak_your_key',
projectId: 'your_project',
cortex: true,
cache: true,
firewall: true,
providers: [
{ name: 'openai', apiKey: 'sk-...' },
{ name: 'anthropic', apiKey: 'sk-ant-...' }
]
});Cost Katana auto-detects configuration from environment variables:
# Any of these work:
COST_KATANA_API_KEY=dak_your_key
COST_KATANA_API_KEY=dak_your_key
API_KEY=dak_your_key
COSTKATANA_KEY=dak_your_key
# Project ID
COST_KATANA_PROJECT=your_project
PROJECT_ID=your_project
COSTKATANA_PROJECT_ID=your_projectOPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
GOOGLE_API_KEY=...
AWS_ACCESS_KEY_ID=...
AWS_SECRET_ACCESS_KEY=...
AWS_REGION=us-east-1COSTKATANA_GATEWAY_URL=https://api.costkatana.com/api/gateway
GATEWAY_URL=...gpt-4,gpt-4-turbo,gpt-4o,gpt-4o-minigpt-3.5-turbo,gpt-3.5-turbo-16k
claude-3-opus,claude-3-sonnet,claude-3-haikuclaude-3-5-sonnet
gemini-pro,gemini-ultra,gemini-flashgemini-2.0-flash,gemini-2.5-pro
nova-pro,nova-lite,nova-micro- Any Bedrock model ID
- Cohere, Grok, DeepSeek, Mistral, xAI (Grok)
import { ai } from 'cost-katana';
try {
const response = await ai('gpt-4', 'Hello');
} catch (error) {
// Error includes helpful troubleshooting steps
console.error(error.message);
// Error codes:
// - NO_API_KEY: No API keys configured
// - INVALID_MODEL: Model not found
// - RATE_LIMIT: Rate limit exceeded
// - BUDGET_EXCEEDED: Budget limit reached
// - NETWORK_ERROR: Connection failed
}If you need full control, the complete API is still available:
import AICostTracker, { AIProvider } from 'cost-katana';
const tracker = await AICostTracker.create({
providers: [
{ provider: AIProvider.OpenAI, apiKey: process.env.OPENAI_API_KEY }
],
optimization: {
enablePromptOptimization: true,
enableModelSuggestions: true,
enableCachingSuggestions: true
},
tracking: {}
});
// Full API methods available
const response = await tracker.makeRequest({...});
const analytics = await tracker.getAnalytics();
const suggestions = await tracker.getOptimizationSuggestions();See Advanced API Documentation for details.
const models = ['gpt-4', 'gpt-3.5-turbo', 'claude-3-haiku'];
for (const model of models) {
const response = await ai(model, 'Test prompt');
console.log(`${model}: $${response.cost}`);
}// First call - costs money
const r1 = await ai('gpt-4', 'FAQ question', { cache: true });
// Second call - free from cache
const r2 = await ai('gpt-4', 'FAQ question', { cache: true });// 40-75% cost reduction on long content
const response = await ai('gpt-4', 'Write a comprehensive guide', {
cortex: true,
maxTokens: 2000
});const session = chat('gpt-4');
const response1 = await session.send('What is AI?');
const response2 = await session.send('Tell me more');
console.log(`Total: $${session.totalCost}`);- Documentation: https://docs.costkatana.com
- Dashboard: https://costkatana.com
- GitHub: https://github.com/Hypothesize-Tech/costkatana-core
- Discord: https://discord.gg/D8nDArmKbY
- Email: support@costkatana.com