P99 Token Latency
0:00:00
Let’s say at OpenAI we log every API call in a requests table, and streamed responses also log each chunk in stream_chunks. Write a query to get the daily 99th percentile (P99) of generation latency, in milliseconds per output token, for the gpt-4 model by organization, ordered by day and organization.
Note: For a regular request, end_timestamp is when the full response was sent. For a streamed request (is_streamed is TRUE), end_timestamp only records when the first chunk was sent. The send time of every chunk is in stream_chunks, a huge table whose rows can arrive late and out of order.
Rules
- Latency runs from start_timestamp to when the response finished.
- The day is the date of start_timestamp.
- The P99 is the smallest latency that at least 99% of an organization’s requests that day are at or below.
- Skip requests with 0 output tokens, regular requests with no end_timestamp, and streamed requests with no chunks.
- Round to 2 decimal places.
Example:
Input:
requests table
| Column | Type |
|---|---|
request_id |
INTEGER |
organization_id |
INTEGER |
model |
VARCHAR |
is_streamed |
BOOLEAN |
start_timestamp |
DATETIME |
end_timestamp |
DATETIME |
output_tokens |
INTEGER |
stream_chunks table
| Column | Type |
|---|---|
request_id |
INTEGER |
chunk_index |
INTEGER |
chunk_time |
DATETIME |
Output:
| Column | Type |
|---|---|
day |
DATE |
organization_id |
INTEGER |
p99_ms_per_token |
FLOAT |
.
.
.
.
Comments