Node.js - Manual Tracking
Manual tracking gives you full control when you already know the exact token counts.
When to Use Manual Tracking
- ✓ You're calling AI APIs directly and have access to usage data
- ✓ You want maximum control over what's reported
- ✓ You're tracking non-OpenAI models or custom services
- ✓ You have your own token counting logic
Example: OpenAI Integration
manual-openai.ts
1import { PaygentClient } from '@paygent_org/paygent-sdk-node';
2import OpenAI from 'openai';
3
4const paygent = PaygentClient.newClient(process.env.PAYGENT_API_KEY);
5const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
6
7// Make OpenAI API call
8const response = await openai.chat.completions.create({
9 model: 'gpt-4o',
10 messages: [{ role: 'user', content: 'Explain quantum computing' }]
11});
12
13// Extract usage data from OpenAI response
14const usage = response.usage;
15
16// Track with Paygent
17await paygent.sendUsage(
18 'qa-agent',
19 'customer-456',
20 'question-answered',
21 {
22 serviceProvider: 'OpenAI',
23 model: response.model,
24 promptTokens: usage.prompt_tokens,
25 completionTokens: usage.completion_tokens,
26 totalTokens: usage.total_tokens
27 }
28);Automate Onboarding
To simplify your workflow, call client.createOrGetCustomer() before tracking usage. This ensures the customer exists in Paygent without needing manual setup in the dashboard.
Example: Gemini Integration
manual-gemini.ts
1import { PaygentClient } from '@paygent_org/paygent-sdk-node';
2import { GoogleGenerativeAI } from '@google/generative-ai';
3
4const paygent = PaygentClient.newClient(process.env.PAYGENT_API_KEY);
5const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY);
6
7// Make Gemini API call
8const model = genAI.getGenerativeModel({ model: 'gemini-2.0-flash-exp' });
9const result = await model.generateContent('Write a story about AI');
10
11// Extract usage data
12const usageMetadata = result.response.usageMetadata;
13
14// Track with Paygent
15await paygent.sendUsage(
16 'content-generator',
17 'customer-789',
18 'story-generated',
19 {
20 serviceProvider: 'Google DeepMind',
21 model: 'gemini-2.0-flash-exp',
22 promptTokens: usageMetadata.promptTokenCount,
23 completionTokens: usageMetadata.candidatesTokenCount,
24 totalTokens: usageMetadata.totalTokenCount
25 }
26);Was this page helpful?
Need help? Contact us at support@withpaygent.com