Voice Tracking

Paygent provides a robust Session-based flow for tracking complex Voice AI interactions, including STT, LLM, and TTS usage.

Prerequisite: Complete the Voice Agent Setup before implementing voice tracking.

Complete Voice Session Flow

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

voice_session_flow.py
1import time
2from paygent_sdk import Client
3from paygent_sdk.models import CustomerCreateOrGetRequest
4from paygent_sdk.constants import ServiceProvider, DeepgramSTTModels, AmazonPollyTTSModels
5
6client = Client(api_key="your-paygent-api-key")
7
8customer_id = "customer-123"
9agent_id = "voice-agent-id"
10
11# 1. Create or Get Customer (Automated)
12client.create_or_get_customer(CustomerCreateOrGetRequest(
13 name="Customer Name",
14 external_id=customer_id
15))
16
17# 2. Define a unique session ID
18session_id = f"voice_call_{int(time.time() * 1000)}"
19
20# 3. Initialize Voice Session
21client.initialize_voice_session(session_id, agent_id, customer_id)
22
23# 4. Track User Speech (STT Usage)
24client.track_voice_stt(session_id, {
25 'serviceProvider': ServiceProvider.DEEPGRAM,
26 'model': DeepgramSTTModels.NOVA_2,
27 'plan': 'default',
28 'audioDuration': 20 # audio duration in seconds
29})
30
31# 5. Track AI Processing (LLM Usage)
32client.track_voice_llm(session_id, {
33 'serviceProvider': ServiceProvider.OPENAI,
34 'model': 'gpt-4o',
35 'plan': 'default',
36 'promptTokens': 1000,
37 'completionTokens': 500
38})
39
40# 6. Track AI Speech (TTS Usage)
41client.track_voice_tts(session_id, {
42 'serviceProvider': ServiceProvider.AMAZON_POLLY,
43 'model': AmazonPollyTTSModels.NEURAL,
44 'plan': 'default',
45 'character_count': 28000
46})
47
48# 7. Final Step: Set Indicator with Call Duration (Minutes)
49client.set_voice_indicator(session_id, "voice_call_completed", 0.5)

How it Works

Step 1 — Customer Onboarding

The CreateOrGetCustomer call ensures the customer exists in Paygent. If they don’t, we’ll create them instantly using the provided externalId.

Step 2 — Session Context

Initializing a 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 — Granular Tracking

Track every step of the voice pipeline independently. Paygent calculates precise costs for each model used during the conversation.

Step 4 — Final Outcome

Finalize the session with setVoiceIndicator. Use the duration or outcome-based indicators to accurately bill your customers based on per-minute connectivity or success outcomes.