spring-projects / spring-projects/spring-ai

Model tokens usage in ChatMemory

Open
#2,923 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
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:

  1. Add getSize() method to org.springframework.ai.chat.messages.Message interface
  2. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.