> ## Documentation Index
> Fetch the complete documentation index at: https://docs.camel-ai.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Camel.memories.context creators.score based

<a id="camel.memories.context_creators.score_based" />

<a id="camel.memories.context_creators.score_based.ScoreBasedContextCreator" />

## ScoreBasedContextCreator

```python theme={"system"}
class ScoreBasedContextCreator(BaseContextCreator):
```

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.

<a id="camel.memories.context_creators.score_based.ScoreBasedContextCreator.__init__" />

### **init**

```python theme={"system"}
def __init__(self, token_counter: BaseTokenCounter, token_limit: int):
```

<a id="camel.memories.context_creators.score_based.ScoreBasedContextCreator.token_counter" />

### token\_counter

```python theme={"system"}
def token_counter(self):
```

<a id="camel.memories.context_creators.score_based.ScoreBasedContextCreator.token_limit" />

### token\_limit

```python theme={"system"}
def token_limit(self):
```

<a id="camel.memories.context_creators.score_based.ScoreBasedContextCreator.set_cached_token_count" />

### set\_cached\_token\_count

```python theme={"system"}
def set_cached_token_count(self, token_count: int, message_count: int):
```

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.

<a id="camel.memories.context_creators.score_based.ScoreBasedContextCreator.clear_cache" />

### clear\_cache

```python theme={"system"}
def clear_cache(self):
```

Clear the cached token count.

<a id="camel.memories.context_creators.score_based.ScoreBasedContextCreator._estimate_message_tokens" />

### \_estimate\_message\_tokens

```python theme={"system"}
def _estimate_message_tokens(self, message: OpenAIMessage):
```

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).

<a id="camel.memories.context_creators.score_based.ScoreBasedContextCreator.create_context" />

### create\_context

```python theme={"system"}
def create_context(self, records: List[ContextRecord]):
```

Returns messages sorted by timestamp and their total token count.
