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

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

> **Warning**
>
> **Prerequisite:** Complete the [Voice Agent Setup](/documentation/voice-tracking/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`**

```python title="openai_realtime_sts.py"
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)
```