717. Trim an agent's chat history to fit a token budget
Agent loops grow the conversation every turn and eventually overflow the context window. The simplest guard keeps the system prompt plus the most recent turns that fit. The kept turns must be a contiguous tail, otherwise a tool result can end up without the call that produced it.
Write trim_history(messages, budget). Each message is a dict with keys "role" and "tokens" (int).
- If messages[0]["role"] == "system", it is always kept and its tokens count toward the budget.
- Then walk from the newest message backwards, keeping messages while the running total stays <= budget. Stop at the first message that does not fit (do not skip it and keep older ones).
Return the sorted list of kept indices. If even the system message alone exceeds the budget, return [0] anyway.
def trim_history(messages, budget)Example 1
tokens [system 10, user 50, assistant 30, user 20], budget 65: keep 0, then 3 (30 total), then 2 (60), then 1 would make 110, stop. Result [0, 2, 3].
Example 2
Input: messages = [{'role': 'system', 'tokens': 100}, {'role': 'user', 'tokens': 5}, {'role': 'tool', 'tokens': 400}, {'role': 'assistant', 'tokens': 40}, {'role': 'user', 'tokens': 30}], budget = 200
Output: [0, 3, 4]
Example 3
Input: messages = [{'role': 'user', 'tokens': 40}, {'role': 'assistant', 'tokens': 40}, {'role': 'user', 'tokens': 40}], budget = 100
Output: [1, 2]
Constraints
- Python 3 standard library. Code runs in your browser.
- Your code must pass all 5 tests to be accepted.
Hint 1
Keep the system prompt, then take the newest messages going backwards until the next one would not fit.
Hint 2
If the first message is a system message, keep index 0 and start the total at its tokens; loop from the last index down to the first non-system index, add while the total stays within budget, break on the first overflow, then return the sorted indices.
Hint 3
Stop with break at the first message that does not fit rather than skipping it, so the kept turns stay a contiguous tail; the system message stays even if it alone exceeds the budget.
Common mistakes
- The buggy version skips the message that does not fit and keeps older small ones, which leaves holes in the conversation.
Understand the idea
An agent's chat history grows every turn until it no longer fits the model's context window. A simple fix keeps the system prompt and then as many of the newest messages as fit. The kept messages must form one unbroken run at the end; if you skip a big message and keep older small ones, a tool result can lose the call that produced it.
tokens [system 5, user 40, assistant 10, tool 30, user 15], budget 60. Keep the system message: 5. Walk from the newest: user 15 gives 20, tool 30 gives 50, assistant 10 gives 60 (allowed, since 60 <= 60), user 40 would give 100, so stop. Kept: [0, 2, 3, 4]. With budget 55, adding the assistant message would reach 60, so stop there with [0, 3, 4], even though nothing older is tried.
if message 0 is a system message, always keep it and count its tokens; then walk backwards while the running total stays within budget, stop at the first miss; return the kept indices sorted
If the system message alone is too big, return [0]. Used in every agent framework's memory manager.