OpenAI Realtime (STS)

Track OpenAI’s Realtime API usage using the explicit track_openai_sts method. This supports the production-style SDK pattern with full multimodal tracking.

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

Core Workflow

  1. Initialize: Create a Paygent Client instance.
  2. Session Setup: Call initialize_voice_session at the start of the call.
  3. Event Tracking: Feed raw server events to track_openai_sts.
  4. Finalize: Set the voice indicator with the total session duration.
openai_realtime_sts.py
1import time
2from paygent_sdk import Client
3from paygent_sdk.models import CustomerCreateOrGetRequest
4
5paygent = Client("your-paygent-api-key")
6session_id = f"voice_call_{int(time.time() * 1000)}"
7customer_id = "your-customer-id"
8
9# 1. Ensure Customer exists (Automatic Onboarding)
10paygent.create_or_get_customer(CustomerCreateOrGetRequest(
11 name="Customer Name",
12 external_id=customer_id
13))
14
15# 2. Initialize Voice Session
16paygent.initialize_voice_session(
17 session_id=session_id,
18 agent_id="your-external-agent-id",
19 customer_id=customer_id
20)
21
22# 3. Handle Realtime Events
23async def _on_event(self, event: RealtimeSessionEvent) -> None:
24 if event.type == "audio":
25 ...
26 elif event.type == "audio_interrupted":
27 ...
28 elif event.type == "raw_model_event":
29 # Direct tracking from raw server events
30 # The SDK automatically extracts usage from 'response.done'
31 paygent.track_openai_sts(
32 usage_metadata=event.data,
33 session_id=self.session_id,
34 model="gpt-realtime-2025-08-28"
35 )
36
37# 4. On Session End
38duration_minutes = (time.time() - start_time) / 60.0
39paygent.set_voice_indicator(session_id, indicator_name, duration_minutes)