import time
from paygent_sdk import Client
from paygent_sdk.models import CustomerCreateOrGetRequest
paygent = Client("your-paygent-api-key")
session_id = f"voice_call_{int(time.time() * 1000)}"
customer_id = "your-customer-id"
# 1. Ensure Customer exists (Automatic Onboarding)
paygent.create_or_get_customer(CustomerCreateOrGetRequest(
name="Customer Name",
external_id=customer_id
))
# 2. Initialize Voice Session
paygent.initialize_voice_session(
session_id=session_id,
agent_id="your-external-agent-id",
customer_id=customer_id
)
# 3. Handle Realtime Events
async def _on_event(self, event: RealtimeSessionEvent) -> None:
if event.type == "audio":
...
elif event.type == "audio_interrupted":
...
elif event.type == "raw_model_event":
# Direct tracking from raw server events
# The SDK automatically extracts usage from 'response.done'
paygent.track_openai_sts(
usage_metadata=event.data,
session_id=self.session_id,
model="gpt-realtime-2025-08-28"
)
# 4. On Session End
duration_minutes = (time.time() - start_time) / 60.0
paygent.set_voice_indicator(session_id, indicator_name, duration_minutes)