Trigger Summarization at a Budget Fraction
Problem Statement
An agent summarizes old turns once its running context grows past a fraction of the model's window. Given cumulative message token sizes, find the first message index at which summarization should trigger.
Background
Let budget be the context window and threshold a fraction (e.g. 0.75). Walking messages in order and accumulating tokens, summarization triggers at the first message whose inclusion makes the running total strictly exceed threshold * budget. If the total never exceeds it, no trigger fires.
Your Task
Implement:
def summarize_trigger(sizes, budget, threshold):
- sizes: list of per-message token counts, in order.
- Return the 0-based index of the first message at which the running sum > threshold * budget, or -1 if it never happens.
Input Format
- sizes (list of ints), budget (int), threshold (float).
Output Format
- An int index or -1.
Sample
print(summarize_trigger([100, 100, 100, 100], 500, 0.75))
Output:
3
Example
print(summarize_trigger([100, 100, 100, 100], 500, 0.75))
3
- Calculate the token limit by multiplying the budget by the threshold: . This defines the strict upper bound for the running sum before summarization triggers.
- Process the first message (index 0) with 100 tokens. The running sum becomes . Since , the trigger condition is not met.
- Process the second message (index 1) with 100 tokens. The running sum becomes . Since , the trigger condition is not met.
- Process the third message (index 2) with 100 tokens. The running sum becomes . Since , the trigger condition is not met.
- Process the fourth message (index 3) with 100 tokens. The running sum becomes . Since , the threshold is exceeded, and the function returns the current index.
- The final output is 3
Constraints
0 <= len(sizes) <= 100000,budget >= 1,0 < threshold <= 1.- Trigger is the first index where the running sum strictly exceeds
threshold*budget. - Return
-1if never exceeded.