Skip to navigation

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
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
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': 0.333 # audio duration in minutes (e.g. 20 seconds → 20/60)
})
# 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 — 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.