> 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.

# Python Voice Tracking — Manual Integration

Implement complete voice billing flows in Python using Paygent's Session-based tracking.

> **Warning**
>
> **Prerequisite:** Complete the [Voice Agent Setup](/documentation/voice-tracking/voice-agent-setup) before implementing voice tracking.

## Voice Pricing

As an architect with decades of experience in high-throughput billing systems, I can tell you that flexibility is the heartbeat of a successful AI platform.

### Scenario A: Per-minute ON

**Real-World Scenario:** You run a dental clinic AI that handles appointment bookings. You want to charge based on the actual conversation time because longer calls use more compute resources.

When you enable the **Per-minute** toggle on your Indicator, Paygent calculates the total duration reported in your `set_voice_indicator` call.

```text
Pricing: $0.15 / min
Duration: 5.0 mins
Result: 5 * 0.15 = $0.75
```

*Result: Perfect for connectivity-based SaaS where you pass on infrastructure costs with a margin.*

![Indicator Step 2 Configuration](/_fern-img/2899757667b8ed69eec2fce1554916a88c6330eca813ad2068c1a5b458778965.webp)
![Flat Based Configuration](/_fern-img/6b7c00ba007afe72895f6d3e367c53075c30670c5f58ce63ecd29a005658329c.webp)
*Configuring the Activity Indicator with the Per-minute toggle ON.*

### Scenario B: Per-minute OFF

**Real-World Scenario:** You've built a world-class Lead Qualification agent. Your customers don't care how long the call lasts; they only care that the lead was qualified successfully.

Disable the **Per-minute** toggle. Now, no matter if the call lasts 1 minute or 20 minutes, the customer is charged a single fixed rate.

```text
Pricing: $0.20 / call
Duration: Any
Result: Flat $0.20
```

*Result: Ideal for outcome-based services where your value is tied to task completion, not duration.*

![Indicator Step 2 Configuration (Off)](/_fern-img/df6a83e141d2b5a3547d8fa6be824c5925e19e04b554d14ecfa05f0b66d22d5d.webp)
*Configuring the Activity Indicator with the Per-minute toggle OFF.*

## Custom Pricing

In modern AI architectures, one size rarely fits all. You might use **GPT-5** for high-stakes negotiation agents, but switch to **Gemini 2.5** for simple data entry tasks.

> **Info**
>
> **Tip:** Custom Pricing is only visible when the **Per-minute** switch is ON in Step 2. This allows you to charge based on the specific infrastructure used.

![Custom Based Configuration](/_fern-img/8d1807d4e4e930bccb557ed869947145bc162a7714a8e0b46d62c3c386de7d6b.webp)

### Real-World Custom Example:

Paygent automatically detects the models you track via `track_voice_stt`, `track_voice_llm`, and `track_voice_tts`. You can then set unique per-minute rates for each tier:

* **LLM: GPT-5 (Premium):** \$0.50 / min
* **LLM: Gemini 2.5 (Standard):** \$0.20 / min
* **STT: Deepgram Nova-2:** \$0.05 / min
* **TTS: Azure Neural:** \$0.08 / min

> "With Custom Pricing, you no longer subsidize expensive models with cheap ones. Your margins stay protected while you provide the best possible AI experience."

## Complete Voice Session Flow

This example shows how to manage a voice session from start to finish:

**`voice_session_flow.py`**

```python title="voice_session_flow.py"
import time
from paygent_sdk import Client
from paygent_sdk.models import CustomerCreateOrGetRequest
from paygent_sdk.constants import ServiceProvider, DeepgramSTTModels, AmazonPollyTTSModels

client = Client(api_key="your-paygent-api-key")

customer_id = "customer-123"
agent_id = "voice-agent-id"

# 1. Create or Get Customer (Automated)
client.create_or_get_customer(CustomerCreateOrGetRequest(
    name="Customer Name",
    external_id=customer_id
))

# 2. Define a unique session ID
# Use your own unique session ID for this customer (e.g., call SID or UUID)
session_id = f"voice_call_{int(time.time() * 1000)}"

# 3. Initialize Voice Session
client.initialize_voice_session(session_id, agent_id, customer_id)

# 4. Track User Speech (STT Usage)
client.track_voice_stt(session_id, {
    'serviceProvider': ServiceProvider.DEEPGRAM,
    'model': DeepgramSTTModels.NOVA_2,
    'plan': 'default',
    'audioDuration': 20 # audio duration in minutes
})

# 5. Track AI Processing (LLM Usage)
client.track_voice_llm(session_id, {
    'serviceProvider': ServiceProvider.OPENAI,
    'model': 'gpt-4o',
    'plan': 'default',
    'promptTokens': 1000,
    'completionTokens': 500
})

# 6. Track AI Speech (TTS Usage)
client.track_voice_tts(session_id, {
    'serviceProvider': ServiceProvider.AMAZON_POLLY,
    'model': AmazonPollyTTSModels.NEURAL,
    'plan': 'default',
    'character_count': 28000
})

# 7. Final Step: Set Indicator with Call Duration (Minutes)
client.set_voice_indicator(session_id, "voice_call_completed", 0.5)
```

## How it Works

### Step 1 — On-the-fly Onboarding

The `create_or_get_customer` method allows for frictionless user onboarding. If a customer with the `external_id` doesn't exist, Paygent creates one automatically.

### Step 2 — Session Context

`initialize_voice_session` sets up a billing context for the call. This ensures all subsequent STT, LLM, and TTS usage is grouped under this single call session.

### Step 3 — Detailed Analytics

Track every event within the voice pipeline. Paygent provides deep insights into which models are being used and the associated costs in real-time.

### Step 4 — Final Outcome

Finalize the session with `set_voice_indicator`. Use the duration or outcome-based indicators to accurately bill your customers.