Tracking customer transcription usage
This guide explains how to track individual customer usage within your product for billing purposes.
This is the recommended approach for tracking customer usage. Creating separate API keys for each of your customers is not an optimal strategy for usage tracking, as it adds unnecessary complexity and makes it harder to manage your account.
There are two separate methods depending on which transcription approach you use:
-
Async transcription: Use webhooks with custom query parameters to associate transcriptions with customers, then retrieve the
audio_durationfrom the transcript response. -
Streaming transcription: Manage customer IDs in your application state and capture the
session_duration_secondsfrom the WebSocket Termination event.
This guide covers both methods in detail.
Async transcription usage tracking
By combining webhooks with custom metadata, you can track audio duration per customer and monitor their usage of your transcription service.
Step 1: Set up webhooks with customer metadata
When submitting a transcription request, include your webhook URL with the customer ID as a query parameter. This allows you to associate each transcription with a specific customer.
Python SDK
Python
JavaScript SDK
JavaScript
You can add multiple query parameters to track additional information:
This allows you to track usage across multiple dimensions (customer, project, order, etc.).
Step 2: Handle the webhook delivery
When the transcription completes, AssemblyAI sends a POST request to your webhook URL with the following payload:
Extract both the transcript_id from the payload and the customer_id from your URL query parameters.
Step 3: Retrieve the transcript with audio duration
Use the transcript ID to fetch the complete transcript details, which includes the audio_duration field (in seconds).
Python SDK
Python
JavaScript SDK
JavaScript
Step 4: Track usage per customer
In your webhook handler, combine the customer ID from your webhook URL query parameters with the audio duration from the transcript to record usage:
- Extract the
customer_idfrom the webhook URL query parameters - Extract the
transcript_idfrom the webhook payload - If the status is
completed, fetch the transcript using the SDK to get theaudio_duration - Store the usage record in your database with the customer ID, transcript ID, audio duration, and timestamp
This allows you to aggregate usage per customer for billing purposes.
Streaming transcription usage tracking
Unlike async transcription which uses webhooks, streaming transcription requires a different approach. You’ll track usage by managing customer IDs in your own application state/session management, capturing the session_duration_seconds from the Termination event, and associating the duration with the customer ID for billing/tracking. AssemblyAI bills streaming based on session duration, so this is the metric you should track.
Step 1: Set up your WebSocket connection
Connect to AssemblyAI’s streaming service. The customer ID is managed entirely in your application and is never sent to AssemblyAI.
Step 2: Capture audio duration from the Termination event
The key to tracking usage is capturing the audio_duration_seconds field from the Termination message. This is sent when the streaming session ends.
Step 3: Log customer usage
When you receive the Termination event, store the session duration for billing/tracking:
- Retrieve the customer ID from your session management system (authentication tokens, session cookies, etc.)
- Extract the
session_duration_secondsfrom the Termination event - Store the usage record in your database with the customer ID, session duration, and timestamp
Since AssemblyAI bills streaming based on session_duration_seconds, this is the metric you should track for accurate billing.
Session duration vs audio duration
From the Termination event, you receive two fields:
Streaming transcription is billed based on session_duration_seconds, not
audio_duration_seconds. Make sure you track the correct metric for accurate
billing.
Session management
You need to implement your own session management to associate WebSocket connections with customer IDs. This could be through user authentication tokens, session cookies, database lookups, or in-memory session stores. Track the customer ID throughout the WebSocket lifecycle so you can associate it with the session duration when the Termination event arrives.
Proper session termination
Always close sessions properly to ensure you receive the Termination event and avoid unexpected costs:
Best practices
When implementing billing tracking, consider the following best practices:
-
Store the transcript/session ID: Always store the identifier alongside usage records. This allows you to audit and verify billing data.
-
Handle errors gracefully: If a transcription fails (
status: "error"), don’t bill the customer for that request. You may want to log failed transcriptions for debugging. -
Secure your webhooks: Use the
webhook_auth_header_nameandwebhook_auth_header_valueparameters to verify that webhook requests are from AssemblyAI. -
Consider time zones: Store timestamps in UTC to avoid confusion when generating billing reports.
Next steps
- Learn more about webhooks and their configuration options
- Explore the Submit Transcript API for async transcription
- Explore the Get Transcript API for retrieving transcript details
- Review the Streaming API for real-time transcription