A conversation grows with every turn, but the model's context window is finite and every token costs money and latency. The simplest form of agent memory is a sliding window: keep the most recent messages that fit in the budget and drop the oldest. Two rules keep it from breaking things. The system message holds the agent's instructions and must never be dropped. And the kept messages must be a contiguous suffix of the history: skipping a short recent message to keep a longer older one would leave the model with a conversation that has holes in it.
Implement trim_messages.
messages is a list of dicts with role and content. A message's cost is len(content.split()) tokens.'system' are always kept. The remaining messages are considered from newest to oldest and added while the running total stays within max_tokens (system messages count toward the total); stop at the first message that does not fit, so the kept non-system messages form a contiguous suffix.max_tokens, raise ValueError.Pay for the system messages first, then walk the other messages backwards.
Remember each kept message's original index to restore the order.
No results yet
Run your code to check it against the examples, or submit to run every test.