> ## 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.utils.token counting

<a id="camel.utils.token_counting" />

<a id="camel.utils.token_counting.get_model_encoding" />

## get\_model\_encoding

```python theme={"system"}
def get_model_encoding(value_for_tiktoken: str):
```

Get model encoding from tiktoken.

**Parameters:**

* **value\_for\_tiktoken**: Model value for tiktoken.

**Returns:**

tiktoken.Encoding: Model encoding.

<a id="camel.utils.token_counting.BaseTokenCounter" />

## BaseTokenCounter

```python theme={"system"}
class BaseTokenCounter(ABC):
```

Base class for token counters of different kinds of models.

<a id="camel.utils.token_counting.BaseTokenCounter.count_tokens_from_messages" />

### count\_tokens\_from\_messages

```python theme={"system"}
def count_tokens_from_messages(self, messages: List[OpenAIMessage]):
```

Count number of tokens in the provided message list.

**Parameters:**

* **messages** (List\[OpenAIMessage]): Message list with the chat history in OpenAI API format.

**Returns:**

int: Number of tokens in the messages.

<a id="camel.utils.token_counting.BaseTokenCounter.encode" />

### encode

```python theme={"system"}
def encode(self, text: str):
```

Encode text into token IDs.

**Parameters:**

* **text** (str): The text to encode.

**Returns:**

List\[int]: List of token IDs.

<a id="camel.utils.token_counting.BaseTokenCounter.decode" />

### decode

```python theme={"system"}
def decode(self, token_ids: List[int]):
```

Decode token IDs back to text.

**Parameters:**

* **token\_ids** (List\[int]): List of token IDs to decode.

**Returns:**

str: Decoded text.

<a id="camel.utils.token_counting.OpenAITokenCounter" />

## OpenAITokenCounter

```python theme={"system"}
class OpenAITokenCounter(BaseTokenCounter):
```

<a id="camel.utils.token_counting.OpenAITokenCounter.__init__" />

### **init**

```python theme={"system"}
def __init__(self, model: UnifiedModelType):
```

Constructor for the token counter for OpenAI models.

**Parameters:**

* **model** (UnifiedModelType): Model type for which tokens will be counted.

<a id="camel.utils.token_counting.OpenAITokenCounter.count_tokens_from_messages" />

### count\_tokens\_from\_messages

```python theme={"system"}
def count_tokens_from_messages(self, messages: List[OpenAIMessage]):
```

Count number of tokens in the provided message list with the
help of package tiktoken.

**Parameters:**

* **messages** (List\[OpenAIMessage]): Message list with the chat history in OpenAI API format.

**Returns:**

int: Number of tokens in the messages.

<a id="camel.utils.token_counting.OpenAITokenCounter._count_tokens_from_image" />

### \_count\_tokens\_from\_image

```python theme={"system"}
def _count_tokens_from_image(self, image: Image.Image, detail: OpenAIVisionDetailType):
```

Count image tokens for OpenAI vision model. An :obj:`"auto"`
resolution model will be treated as :obj:`"high"`. All images with
:obj:`"low"` detail cost 85 tokens each. Images with :obj:`"high"` detail
are first scaled to fit within a 2048 x 2048 square, maintaining their
aspect ratio. Then, they are scaled such that the shortest side of the
image is 768px long. Finally, we count how many 512px squares the image
consists of. Each of those squares costs 170 tokens. Another 85 tokens are
always added to the final total. For more details please refer to [OpenAI
vision docs](https://platform.openai.com/docs/guides/vision)

**Parameters:**

* **image** (PIL.Image.Image): Image to count number of tokens.
* **detail** (OpenAIVisionDetailType): Image detail type to count number of tokens.

**Returns:**

int: Number of tokens for the image given a detail type.

<a id="camel.utils.token_counting.OpenAITokenCounter.encode" />

### encode

```python theme={"system"}
def encode(self, text: str):
```

Encode text into token IDs.

**Parameters:**

* **text** (str): The text to encode.

**Returns:**

List\[int]: List of token IDs.

<a id="camel.utils.token_counting.OpenAITokenCounter.decode" />

### decode

```python theme={"system"}
def decode(self, token_ids: List[int]):
```

Decode token IDs back to text.

**Parameters:**

* **token\_ids** (List\[int]): List of token IDs to decode.

**Returns:**

str: Decoded text.

<a id="camel.utils.token_counting.AnthropicTokenCounter" />

## AnthropicTokenCounter

```python theme={"system"}
class AnthropicTokenCounter(BaseTokenCounter):
```

<a id="camel.utils.token_counting.AnthropicTokenCounter.__init__" />

### **init**

```python theme={"system"}
def __init__(
    self,
    model: str,
    api_key: Optional[str] = None,
    base_url: Optional[str] = None
):
```

Constructor for the token counter for Anthropic models.

**Parameters:**

* **model** (str): The name of the Anthropic model being used.
* **api\_key** (Optional\[str], optional): The API key for authenticating with the Anthropic service. If not provided, it will use the ANTHROPIC\_API\_KEY environment variable. (default: :obj:`None`)
* **base\_url** (Optional\[str], optional): The URL of the Anthropic service. If not provided, it will use the default Anthropic URL. (default: :obj:`None`)

<a id="camel.utils.token_counting.AnthropicTokenCounter.count_tokens_from_messages" />

### count\_tokens\_from\_messages

```python theme={"system"}
def count_tokens_from_messages(self, messages: List[OpenAIMessage]):
```

Count number of tokens in the provided message list using
loaded tokenizer specific for this type of model.

**Parameters:**

* **messages** (List\[OpenAIMessage]): Message list with the chat history in OpenAI API format.

**Returns:**

int: Number of tokens in the messages.

<a id="camel.utils.token_counting.AnthropicTokenCounter.encode" />

### encode

```python theme={"system"}
def encode(self, text: str):
```

Encode text into token IDs.

**Parameters:**

* **text** (str): The text to encode.

**Returns:**

List\[int]: List of token IDs.

<a id="camel.utils.token_counting.AnthropicTokenCounter.decode" />

### decode

```python theme={"system"}
def decode(self, token_ids: List[int]):
```

Decode token IDs back to text.

**Parameters:**

* **token\_ids** (List\[int]): List of token IDs to decode.

**Returns:**

str: Decoded text.

<a id="camel.utils.token_counting.LiteLLMTokenCounter" />

## LiteLLMTokenCounter

```python theme={"system"}
class LiteLLMTokenCounter(BaseTokenCounter):
```

<a id="camel.utils.token_counting.LiteLLMTokenCounter.__init__" />

### **init**

```python theme={"system"}
def __init__(self, model_type: UnifiedModelType):
```

Constructor for the token counter for LiteLLM models.

**Parameters:**

* **model\_type** (UnifiedModelType): Model type for which tokens will be counted.

<a id="camel.utils.token_counting.LiteLLMTokenCounter.token_counter" />

### token\_counter

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

<a id="camel.utils.token_counting.LiteLLMTokenCounter.completion_cost" />

### completion\_cost

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

<a id="camel.utils.token_counting.LiteLLMTokenCounter.count_tokens_from_messages" />

### count\_tokens\_from\_messages

```python theme={"system"}
def count_tokens_from_messages(self, messages: List[OpenAIMessage]):
```

Count number of tokens in the provided message list using
the tokenizer specific to this type of model.

**Parameters:**

* **messages** (List\[OpenAIMessage]): Message list with the chat history in LiteLLM API format.

**Returns:**

int: Number of tokens in the messages.

<a id="camel.utils.token_counting.LiteLLMTokenCounter.calculate_cost_from_response" />

### calculate\_cost\_from\_response

```python theme={"system"}
def calculate_cost_from_response(self, response: dict):
```

Calculate the cost of the given completion response.

**Parameters:**

* **response** (dict): The completion response from LiteLLM.

**Returns:**

float: The cost of the completion call in USD.

<a id="camel.utils.token_counting.LiteLLMTokenCounter.encode" />

### encode

```python theme={"system"}
def encode(self, text: str):
```

Encode text into token IDs.

**Parameters:**

* **text** (str): The text to encode.

**Returns:**

List\[int]: List of token IDs.

<a id="camel.utils.token_counting.LiteLLMTokenCounter.decode" />

### decode

```python theme={"system"}
def decode(self, token_ids: List[int]):
```

Decode token IDs back to text.

**Parameters:**

* **token\_ids** (List\[int]): List of token IDs to decode.

**Returns:**

str: Decoded text.

<a id="camel.utils.token_counting.MistralTokenCounter" />

## MistralTokenCounter

```python theme={"system"}
class MistralTokenCounter(BaseTokenCounter):
```

<a id="camel.utils.token_counting.MistralTokenCounter.__init__" />

### **init**

```python theme={"system"}
def __init__(self, model_type: ModelType):
```

Constructor for the token counter for Mistral models.

**Parameters:**

* **model\_type** (ModelType): Model type for which tokens will be counted.

<a id="camel.utils.token_counting.MistralTokenCounter.count_tokens_from_messages" />

### count\_tokens\_from\_messages

```python theme={"system"}
def count_tokens_from_messages(self, messages: List[OpenAIMessage]):
```

Count number of tokens in the provided message list using
loaded tokenizer specific for this type of model.

**Parameters:**

* **messages** (List\[OpenAIMessage]): Message list with the chat history in OpenAI API format.

**Returns:**

int: Total number of tokens in the messages.

<a id="camel.utils.token_counting.MistralTokenCounter._convert_response_from_openai_to_mistral" />

### \_convert\_response\_from\_openai\_to\_mistral

```python theme={"system"}
def _convert_response_from_openai_to_mistral(self, openai_msg: OpenAIMessage):
```

Convert an OpenAI message to a Mistral ChatCompletionRequest.

**Parameters:**

* **openai\_msg** (OpenAIMessage): An individual message with OpenAI format.

**Returns:**

ChatCompletionRequest: The converted message in Mistral's request
format.

<a id="camel.utils.token_counting.MistralTokenCounter.encode" />

### encode

```python theme={"system"}
def encode(self, text: str):
```

Encode text into token IDs.

**Parameters:**

* **text** (str): The text to encode.

**Returns:**

List\[int]: List of token IDs.

<a id="camel.utils.token_counting.MistralTokenCounter.decode" />

### decode

```python theme={"system"}
def decode(self, token_ids: List[int]):
```

Decode token IDs back to text.

**Parameters:**

* **token\_ids** (List\[int]): List of token IDs to decode.

**Returns:**

str: Decoded text.
