@revmax/agent-sdk
Version:
Official Node.js SDK for RevMax - billing, customer management, and usage tracking
80 lines (69 loc) ⢠2.51 kB
JavaScript
/**
* RevMax SDK - Single Event with Usage Costs
*
* Track an event with detailed cost breakdown (LLM tokens, API calls, etc.)
* This is useful for tracking the actual cost of AI/LLM operations.
*
* Usage:
* REVMAX_API_KEY=revx_pk_xxx node 02-single-event-with-costs.js
*/
const { RevMaxClient } = require('../dist');
// Configuration
const API_KEY = process.env.REVMAX_API_KEY || 'revx_pk_your_api_key_here';
const BASE_URL = process.env.REVMAX_API_URL || 'http://localhost:3005/v1/sdk';
const AGENT_ID = 'd1d6e514-faae-4e47-a100-0f2757d69849';
const CUSTOMER_EXTERNAL_ID = 'customer-4398bad1-9c3b-46f7-be7b-25e8770d0524';
async function main() {
console.log('š RevMax SDK - Single Event with Cost Tracking\n');
// Create and connect client
const client = new RevMaxClient(API_KEY, {
baseURL: BASE_URL,
logging: { enabled: true, level: 'info' },
});
await client.connect();
console.log(`ā
Connected to: ${client.getOrganization().name}\n`);
// Track a QBR report generation with LLM token costs
const result = await client.trackEvent({
customerExternalId: CUSTOMER_EXTERNAL_ID,
agentId: AGENT_ID,
signalName: 'qbr_generated', // Quarterly Business Review generated
quantity: 1,
usageDate: new Date().toISOString(),
metadata: {
// Track which services were used and their consumption
usageCost: [
{
serviceName: 'Anthropic Claude Sonnet Input',
provider: 'Anthropic',
units: 25000, // 25k input tokens
unitType: 'token',
},
{
serviceName: 'Anthropic Claude Sonnet Output',
provider: 'Anthropic',
units: 5000, // 5k output tokens
unitType: 'token',
},
],
// Additional context
reportType: 'quarterly',
customerId: CUSTOMER_EXTERNAL_ID,
},
});
console.log('\nā
Event tracked with cost data!');
console.log('Event ID:', result.signalEvent?.id);
// Show cost calculation from timeline
const costEvent = result.timeline?.find((t) => t.event_type === 'cost');
if (costEvent) {
console.log(`š° Calculated Cost: $${costEvent.amount}`);
}
// Show usage cost breakdown
console.log('\nš Usage Breakdown:');
result.signalEvent?.metadata?.usageCost?.forEach((cost) => {
console.log(` - ${cost.serviceName}: ${cost.units.toLocaleString()} ${cost.unitType}s`);
});
}
main().catch((err) => {
console.error('ā Error:', err.message);
process.exit(1);
});