spring-projects / spring-projects/spring-ai
Model tokens usage in ChatMemory
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.9k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 6
Description
ChatMemory contract does not provide message size in tokens making context management quite dificult.
Entire ChatMemory (interface) contract should be focused around context size in tokens while that is how models function.
Yet we have no size of the message. Usually the message size is available in model response and this can be seen for example in ChatResponseMetadata where we do have entire message size.
My proposal would be simple:
- Add getSize() method to org.springframework.ai.chat.messages.Message interface
- Modify org.springframework.ai.chat.memory.ChatMemory.get(String conversationId, int lastN); so that uses real model context size and actually is called with lastTokens (how many tokens chat memory should retrieve from persistence layer). ChatModel when calling ChatMemory already has its max context size and question hence he can compute how many tokens will be requested from persistence layer
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the org.springframework.ai.chat.messages.Message interface and the ChatMemory.get(String conversationId, int lastN) contract, then trace how ChatModel calls ChatMemory and how ChatResponseMetadata exposes message size. Define how token-based retrieval and message sizing fit the existing contracts; done means the API and its callers consistently use context size in tokens with corresponding tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- ai, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100