(sum by (type)(rate(aiplatform_googleapis_com:publisher_online_serving_token_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}[${__interval}])))
I want to divide it by this vector: aiplatform_googleapis_com:publisher_online_serving_model_invocation_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"} to get a per-request rate of token counts.
For some extra context, this is within Google Stackdriver.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Of course. This is a very common and important pattern in PromQL: calculating a ratio between two CUMULATIVE or COUNTER metrics.
Your intuition to divide the two is correct, but the structure needs a slight adjustment to handle how PromQL matches and divides vectors. You cannot divide a pre-aggregated rate by a non-aggregated rate because the labels won't match.
Here is the correct and most robust query to get the average number of tokens per request.
The Correct Query
This query calculates the ratio of the total increase in tokens to the total increase in requests over the given interval, which gives you the average tokens per request.
promql
sum by (type) (
increase(aiplatform_googleapis_com:publisher_online_serving_token_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}[${__interval}])
/ ignoring(type) group_left
increase(aiplatform_googleapis_com:publisher_online_serving_model_invocation_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}[${__interval}])
)
Explanation Step-by-Step
Let's break down why this query works and why your original approach wouldn't.
Use increase() instead of rate():
rate() calculates the per-second rate of change (e.g., tokens/sec).
increase() calculates the total increase over the time window (e.g., total tokens in the last 5 minutes).
When you want a ratio like "tokens per request", it's more intuitive and often more numerically stable to divide the total increase in tokens by the total increase in requests. The math works out the same: (tokens/sec) / (requests/sec) = tokens/request. But increase() is generally preferred for this use case.
The Vector Matching Problem (The Core Issue):
The publisher_online_serving_token_count metric has a type label (e.g., type="input" and type="output").
The publisher_online_serving_model_invocation_count metric does not have a type label, as an invocation is a single event.
PromQL division requires that for every time series on the left side, it finds a single time series on the right side with the exact same set of labels.
Your original approach sum by (type)(rate(...)) first removes all labels excepttype. The result is a vector like {type="input"} and {type="output"}. The denominator vector still has all its original labels (project_id, model_id, etc.), so there is no match, and the division would return an empty result.
The Solution: ignoring and group_left
We need to perform the division before the final aggregation.
... / ignoring(type) ...: This tells PromQL to perform the division by matching all labels on both sides, except for the type label. This is exactly what we need, as type only exists on the left.
... group_left: This modifier is crucial. It tells PromQL that for this many-to-one matching (many token types to one invocation count), the resulting time series should keep the labels from the "left" side of the division—including the type label that we want to preserve.
Final Aggregation: sum by (type) (...)
The division inside the parentheses gives you the tokens-per-request for every unique combination of project_id, model_id, location, etc., while preserving the type label.
Wrapping the entire expression in sum by (type) (...) then aggregates these individual ratios, giving you the overall average tokens-per-request, broken down only by type, which is what you originally wanted.
Alternative using rate()
If you prefer to stick with rate(), the same logic applies. The result will be identical.
promql
sum by (type) (
rate(aiplatform_googleapis_com:publisher_online_serving_token_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}[${__interval}])
/ ignoring(type) group_left
rate(aiplatform_googleapis_com:publisher_online_serving_model_invocation_count{monitored_resource="aiplatform.googleapis.com/PublisherModel"}[${__interval}])
)
This calculates the per-second token rate divided by the per-second request rate, yielding the same "tokens per request" unit. For this specific ratio calculation, increase() is often considered more idiomatic.