I don't see any document references to it and there are no tests so I'm not 100% but looking at LastMaxTokenSizeContentPurger it appears to return all content that will not fit vs the content that does fit

Shouldn't L61 be newList.addAll(datum.subList(0, index)); ? https://github.com/spring-projects/spring-ai/blob/ff52859b2d56029dce8f18d47b4fecb9cc8720c4/spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/LastMaxTokenSizeContentPurger.java#L61C24-L61C31

Comment From: jhl221123

Hi @jasonparallel,

From what I understand, the loop while (index < datum.size() && totalSize > this.maxTokenSize) at line 50 determines the starting point for retaining content, trimming off elements from the beginning of datum up to datum[index - 1] to stay within maxTokenSize.

Given that, datum.subList(index, datum.size()) at line 61 seems to correctly return the remaining content.

I agree that adding some tests would help confirm this behavior.

If you're not already planning to work on this, would it be alright if I gave it a try?

Comment From: jasonparallel

@jhl221123 You are correct. It is trimming off the front. I must have been thinking about using it in the opposite manor.
It seems odd that this.doEstimateTokenCount would have to be run on each item in List datum before purgeExcess is called to supply totalSize and then it would run doEstimateTokenCount on the content again, but I guess totalSize might include additional content beyond the size of the datum content.

I'm going to close the ticket. Feel free to add documentation and or tests.