Go - Automatic Tracking
Automatic tracking uses wrapper classes that handle token counting and tracking for you.
Frictionless Onboarding
Use client.CreateOrGetCustomer() to automatically sync your users with Paygent. You can call this right before tracking usage to ensure the customer exists without manual setup.
📖 Real-World Scenario
You're building a chatbot and making dozens of OpenAI API calls. Manually extracting usage data and calling sendUsage() for each one is tedious and error-prone. You just want the tracking to happen automatically.
💡 Solution
Use Paygent's automatic wrappers. Just wrap your API client and add 3 tracking parameters:
1package main
2
3import (
4 "context"
5 "os"
6
7 paygent "github.com/paygent-org/paygent-sdk-go"
8 "github.com/paygent-org/paygent-sdk-go/integrations"
9 "github.com/sashabaranov/go-openai"
10)
11
12func main() {
13 // Initialize Paygent client
14 paygentClient := paygent.NewClient(os.Getenv("PAYGENT_API_KEY"))
15
16 // Initialize OpenAI client
17 openaiClient := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
18
19 // Create PaygentOpenAI wrapper for automatic tracking
20 paygentOpenAI := integrations.NewPaygentOpenAI(openaiClient, paygentClient)
21
22 // Make OpenAI call with automatic tracking
23 // Just add 3 tracking parameters to your request!
24 params := integrations.ChatCompletionParams{
25 Indicator: "message-sent",
26 ExternalAgentID: "chatbot-agent",
27 ExternalCustomerID: "customer-123",
28
29 Model: "gpt-4o",
30 Messages: []openai.ChatCompletionMessage{
31 {Role: "user", Content: "Hello!"},
32 },
33 }
34
35 resp, _ := paygentOpenAI.CreateChatCompletion(context.Background(), params)
36
37 // Usage automatically tracked! No manual code needed!
38}✅ Result
Zero manual tracking code. Every OpenAI call is automatically tracked with accurate token counts and costs. 60% less code!
Manual vs Automatic
| Feature | Manual Tracking | Automatic Tracking |
|---|---|---|
| Code Lines | ~40 lines | ~15 lines |
| Token Counting | Manual extraction | Automatic |
| Error Risk | Can forget to track | Impossible to miss |
| Maintenance | High | Low |
| Recommended | For custom use cases | ✓ For most users |
Was this page helpful?
Need help? Contact us at support@withpaygent.com