> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.withpaygent.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.withpaygent.com/_mcp/server.

# Automatic Tracking

Automatic tracking simplifies your integration by intercepting calls to LLM providers. You don't have to manually extract token counts or call `sendUsage()` for each API request.

**Scenario:** You're building a chatbot and making dozens of OpenAI API calls. Manually extracting usage data and calling the Paygent tracking API for each one is tedious and error-prone. You just want the tracking to happen automatically.

**Solution:** Use Paygent's automatic wrappers or interceptors.

#### Python

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.

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

**`automatic_tracking.py`**

```python title="automatic_tracking.py"
import paygent_sdk
from openai import OpenAI

# Initialize once at application start
paygent_sdk.init(api_key="your-paygent-api-key")

client = OpenAI()

# Standard call - usage is tracked automatically!
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Explain automatic tracking"}],
    
    # These parameters are intercepted by Paygent
    paygent_agent_id="research-agent",
    paygent_customer_id="user-789",
    paygent_indicator="explanation-requested"
)
```

#### Node.js

Use Paygent's automatic wrappers. Just wrap your API client and add 3 tracking parameters:

**`automatic-openai.ts`**

```typescript title="automatic-openai.ts"
import { PaygentClient, PaygentOpenAI } from '@paygent_org/paygent-sdk-node';
import OpenAI from 'openai';

// Initialize Paygent client
const paygentClient = PaygentClient.newClient(process.env.PAYGENT_API_KEY);

// Initialize OpenAI client
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

// Create PaygentOpenAI wrapper for automatic tracking
const paygentOpenai = new PaygentOpenAI(openai, paygentClient);

// Make OpenAI call with automatic tracking
// Just add 3 tracking parameters to your normal OpenAI call!
const response = await paygentOpenai.chat.completions.create({
  indicator: 'message-sent',
  external_agent_id: 'chatbot-agent',
  external_customer_id: 'customer-123',
  
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }]
});

// Usage automatically tracked! No manual code needed!
```

> **Tip**
>
> **Pro-Tip: Auto-Onboarding.** Before your first AI call, use `createOrGetCustomer` with your user's ID. This allows Paygent to automatically handle data isolation and billing attribution for new users.

#### Go

*Note: Go automatic tracking wrappers are currently in beta.*

In Go, automatic tracking typically involves wrapping your HTTP transport or using custom Paygent interceptors for specific LLM SDKs (like go-openai). For now, use [Manual Tracking](./manual-tracking) to guarantee stable, predictable behavior across all your Go microservices.

## Manual vs Automatic

| Feature          | Manual Tracking                | Automatic Tracking            |
| ---------------- | ------------------------------ | ----------------------------- |
| Setup            | Client instance setup per call | Initialize once / Wrap client |
| Token Extraction | Manual code needed             | Fully automatic               |
| Error Risk       | Can forget to track            | Impossible to miss            |
| Maintenance      | High                           | Low                           |
| Recommended      | For custom use cases           | ✓ Best for most users         |