Skip to main content

ScoreBasedContextCreator

A context creation strategy that orders records chronologically. This class supports token count estimation to reduce expensive repeated token counting. When a cached token count is available, it estimates new message tokens using character-based approximation instead of calling the token counter for every message. Parameters:
  • token_counter (BaseTokenCounter): Token counter instance used to compute the combined token count of the returned messages.
  • token_limit (int): Retained for API compatibility. No longer used to filter records.

init

token_counter

token_limit

set_cached_token_count

Set the cached token count from LLM response usage. Parameters:
  • token_count (int): The total token count (prompt + completion) from LLM response usage.
  • message_count (int): The number of messages including the assistant response that will be added to memory.

clear_cache

Clear the cached token count.

_estimate_message_tokens

Estimate token count for a single message. Uses ~2 chars/token as a conservative approximation to handle both ASCII (~4 chars/token) and CJK text (~1-2 chars/token). Parameters:
  • message: The OpenAI message to estimate.
Returns: Estimated token count (intentionally conservative).

create_context

Returns messages sorted by timestamp and their total token count.