InferenceAI

Enter at least two characters to search.

Start free

Conversation Token Trim

mediumLLM Engineering20 min25 XP

Messages are [text,tokens]. Keep the newest suffix whose total tokens fit the budget, preserving chronological order.

Examples

EXAMPLE WALKTHROUGHnewest suffix

A guided view of the published example, not a trace of your code.

INPUT[[["a",3],["b",4],["c",2]],6]
TASKtrim_messages()
EXPECTED OUTPUT[["b",4],["c",2]]

Start with the input values shown in this example.

Example 1 of 2

Input: messages = [["a",3],["b",4],["c",2]], budget = 6

Output: [["b",4],["c",2]]

Explanation: newest suffix

Input: messages = [["a",7]], budget = 3

Output: []

Explanation: none fit

Hint 1

Walk backward, then reverse the selected messages.

Topics

PythonLLM Engineering