Python - Automatic Tracking

Automatic tracking uses monkeypatching to intercept calls to LLM providers. This means you don't have to change how you call the LLM, other than adding three optional tracking parameters.

📖 Real-World Scenario

You're building a chatbot and making dozens of OpenAI API calls. Manually extracting usage data and calling send_usage() for each one is tedious. You just want the tracking to happen automatically without changing your architecture.

💡 Solution

Call paygent_sdk.init() once. Then, simply pass tracking IDs directly into your standard model calls.

automatic_tracking.py
1import paygent_sdk
2from openai import OpenAI
3
4# Initialize once at application start
5paygent_sdk.init(api_key="your-paygent-api-key")
6
7client = OpenAI()
8
9# Standard call - usage is tracked automatically!
10response = client.chat.completions.create(
11    model="gpt-4o",
12    messages=[{"role": "user", "content": "Explain monkeypatching"}],
13    
14    # These parameters are intercepted by Paygent
15    paygent_agent_id="research-agent",
16    paygent_customer_id="user-789",
17    paygent_indicator="explanation-requested"
18)

✅ Result

Zero manual extraction code. Every supported LLM call is automatically tracked with accurate token counts and costs. No wrappers required!

Manual vs Automatic

FeatureManual TrackingAutomatic Tracking
SetupClient instancepaygent_sdk.init()
Library CallsExtra call per requestNative provider calls
Token ExtractionManual code neededFully automatic
MaintenanceHigherMinimal
RecommendedFor advanced/unsupported✓ Best for most users

Was this page helpful?

Need help? Contact us at support@withpaygent.com