Your question is Rolling Latency Percentiles and Alerts. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Anthropic monitors Claude token-generation latency from an ordered stream of completed requests. Given latency values in arrival order, compute rolling p50 and p95 values and identify requests whose latency is more than 1.5 times the previous window's p95.
Return one result for every complete window. The first window cannot be classified as anomalous because it has no preceding window.
Implement rolling_latency_anomalies(latencies, window_size), where latencies is a list of non-negative integers and window_size is a positive integer. Return a list of dictionaries in chronological order. Each dictionary must contain:
index: the ending index of the current windowp50: the current window's 50th percentilep95: the current window's 95th percentileanomalous: whether the current window's newest latency is greater than 1.5 * the preceding window's p95Use the nearest-rank percentile: rank ceil(percentile * window length / 100), counting ranks from 1. For example, with four values, p50 is the second-smallest value and p95 is the largest value.
def rolling_latency_anomalies(latencies, window_size):