> ## 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.models.base model

<a id="camel.models.base_model" />

<a id="camel.models.base_model._StreamLogger" />

## \_StreamLogger

```python theme={"system"}
class _StreamLogger:
```

Base for stream logging wrappers.

<a id="camel.models.base_model._StreamLogger.__init__" />

### **init**

```python theme={"system"}
def __init__(self, log_path: Optional[str], log_enabled: bool):
```

<a id="camel.models.base_model._StreamLogger._collect" />

### \_collect

```python theme={"system"}
def _collect(self, chunk: ChatCompletionChunk):
```

<a id="camel.models.base_model._StreamLogger._log" />

### \_log

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

<a id="camel.models.base_model._SyncStreamWrapper" />

## \_SyncStreamWrapper

```python theme={"system"}
class _SyncStreamWrapper(_StreamLogger):
```

Sync stream wrapper with logging.

<a id="camel.models.base_model._SyncStreamWrapper.__init__" />

### **init**

```python theme={"system"}
def __init__(
    self,
    stream: Union[Stream[ChatCompletionChunk], Generator[ChatCompletionChunk, None, None]],
    log_path: Optional[str],
    log_enabled: bool
):
```

<a id="camel.models.base_model._SyncStreamWrapper.__iter__" />

### **iter**

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

<a id="camel.models.base_model._SyncStreamWrapper.__next__" />

### **next**

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

<a id="camel.models.base_model._SyncStreamWrapper.__enter__" />

### **enter**

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

<a id="camel.models.base_model._SyncStreamWrapper.__exit__" />

### **exit**

```python theme={"system"}
def __exit__(self, *_):
```

<a id="camel.models.base_model._SyncStreamWrapper.__del__" />

### **del**

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

<a id="camel.models.base_model._AsyncStreamWrapper" />

## \_AsyncStreamWrapper

```python theme={"system"}
class _AsyncStreamWrapper(_StreamLogger):
```

Async stream wrapper with logging.

<a id="camel.models.base_model._AsyncStreamWrapper.__init__" />

### **init**

```python theme={"system"}
def __init__(
    self,
    stream: Union[AsyncStream[ChatCompletionChunk], AsyncGenerator[ChatCompletionChunk, None]],
    log_path: Optional[str],
    log_enabled: bool
):
```

<a id="camel.models.base_model._AsyncStreamWrapper.__aiter__" />

### **aiter**

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

<a id="camel.models.base_model._AsyncStreamWrapper.__del__" />

### **del**

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

<a id="camel.models.base_model.ModelBackendMeta" />

## ModelBackendMeta

```python theme={"system"}
class ModelBackendMeta(ABCMeta):
```

Metaclass that automatically preprocesses messages in run method.

Automatically wraps the run method of any class inheriting from
BaseModelBackend to preprocess messages (remove `<think>` tags) before they
are sent to the model.

<a id="camel.models.base_model.ModelBackendMeta.__new__" />

### **new**

```python theme={"system"}
def __new__(
    mcs,
    name,
    bases,
    namespace
):
```

Wraps run method with preprocessing if it exists in the class.

<a id="camel.models.base_model.BaseModelBackend" />

## BaseModelBackend

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

Base class for different model backends.
It may be OpenAI API, a local LLM, a stub for unit tests, etc.

**Parameters:**

* **model\_type** (Union\[ModelType, str]): Model for which a backend is created.
* **model\_config\_dict** (Optional\[Dict\[str, Any]], optional): A config dictionary. (default: :obj:`{}`)
* **api\_key** (Optional\[str], optional): The API key for authenticating with the model service. (default: :obj:`None`)
* **url** (Optional\[str], optional): The url to the model service. (default: :obj:`None`)
* **token\_counter** (Optional\[BaseTokenCounter], optional): Token counter to use for the model. If not provided, :obj:`OpenAITokenCounter` will be used. (default: :obj:`None`)
* **timeout** (Optional\[float], optional): The timeout value in seconds for API calls. (default: :obj:`None`)
* **max\_retries** (int, optional): Maximum number of retries for API calls. (default: :obj:`3`)

<a id="camel.models.base_model.BaseModelBackend.__init__" />

### **init**

```python theme={"system"}
def __init__(
    self,
    model_type: Union[ModelType, str],
    model_config_dict: Optional[Dict[str, Any]] = None,
    api_key: Optional[str] = None,
    url: Optional[str] = None,
    token_counter: Optional[BaseTokenCounter] = None,
    timeout: Optional[float] = Constants.TIMEOUT_THRESHOLD,
    max_retries: int = 3
):
```

<a id="camel.models.base_model.BaseModelBackend.token_counter" />

### token\_counter

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

**Returns:**

BaseTokenCounter: The token counter following the model's
tokenization style.

<a id="camel.models.base_model.BaseModelBackend._prepare_request_config" />

### \_prepare\_request\_config

```python theme={"system"}
def _prepare_request_config(self, tools: Optional[List[Dict[str, Any]]] = None):
```

Prepare the request configuration dictionary.

Creates a deep copy of the model config and handles tool-related
parameters. If no tools are specified, removes parallel\_tool\_calls
as OpenAI API only allows it when tools are present.

**Parameters:**

* **tools** (Optional\[List\[Dict\[str, Any]]]): The tools to include in the request. (default: :obj:`None`)

**Returns:**

Dict\[str, Any]: The prepared request configuration.

<a id="camel.models.base_model.BaseModelBackend.preprocess_messages" />

### preprocess\_messages

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

Preprocess messages before sending to model API.
Removes thinking content from assistant and user messages.
Automatically formats messages for parallel tool calls if tools are
detected.

**Parameters:**

* **messages** (List\[OpenAIMessage]): Original messages.

**Returns:**

List\[OpenAIMessage]: Preprocessed messages

<a id="camel.models.base_model.BaseModelBackend._log_request" />

### \_log\_request

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

Log the request messages to a JSON file if logging is enabled.

**Parameters:**

* **messages** (List\[OpenAIMessage]): The messages to log.

**Returns:**

Optional\[str]: The path to the log file if logging is enabled,
None otherwise.

<a id="camel.models.base_model.BaseModelBackend._log_response" />

### \_log\_response

```python theme={"system"}
def _log_response(self, log_path: str, response: Any):
```

Log the response to the existing log file.

**Parameters:**

* **log\_path** (str): The path to the log file.
* **response** (Any): The response to log.

<a id="camel.models.base_model.BaseModelBackend._log_and_trace" />

### \_log\_and\_trace

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

Update Langfuse trace with session metadata.

This method updates the current Langfuse trace with agent session
information and model metadata. Called at the start of \_run() and
\_arun() methods before API execution.

<a id="camel.models.base_model.BaseModelBackend._run" />

### \_run

```python theme={"system"}
def _run(
    self,
    messages: List[OpenAIMessage],
    response_format: Optional[Type[BaseModel]] = None,
    tools: Optional[List[Dict[str, Any]]] = None
):
```

Runs the query to the backend model in a non-stream mode.

**Parameters:**

* **messages** (List\[OpenAIMessage]): Message list with the chat history in OpenAI API format.
* **response\_format** (Optional\[Type\[BaseModel]]): The format of the response.
* **tools** (Optional\[List\[Dict\[str, Any]]]): The schema of the tools to use for the request.

**Returns:**

Union\[ChatCompletion, Stream\[ChatCompletionChunk], Any]:
`ChatCompletion` in the non-stream mode, or
`Stream[ChatCompletionChunk]` in the stream mode,
or `ChatCompletionStreamManager[BaseModel]` in the structured
stream mode.

<a id="camel.models.base_model.BaseModelBackend.run" />

### run

```python theme={"system"}
def run(
    self,
    messages: List[OpenAIMessage],
    response_format: Optional[Type[BaseModel]] = None,
    tools: Optional[List[Dict[str, Any]]] = None
):
```

Runs the query to the backend model.

**Parameters:**

* **messages** (List\[OpenAIMessage]): Message list with the chat history in OpenAI API format.
* **response\_format** (Optional\[Type\[BaseModel]]): The response format to use for the model. (default: :obj:`None`)
* **tools** (Optional\[List\[Tool]]): The schema of tools to use for the model for this request. Will override the tools specified in the model configuration (but not change the configuration). (default: :obj:`None`)

**Returns:**

Union\[ChatCompletion, Stream\[ChatCompletionChunk], Any]:
`ChatCompletion` in the non-stream mode,
`Stream[ChatCompletionChunk]` in the stream mode, or
`ChatCompletionStreamManager[BaseModel]` in the structured
stream mode.

<a id="camel.models.base_model.BaseModelBackend.count_tokens_from_messages" />

### count\_tokens\_from\_messages

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

Count the number of tokens in the messages using the specific
tokenizer.

**Parameters:**

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

**Returns:**

int: Number of tokens in the messages.

<a id="camel.models.base_model.BaseModelBackend._to_chat_completion" />

### \_to\_chat\_completion

```python theme={"system"}
def _to_chat_completion(self, response: ParsedChatCompletion):
```

<a id="camel.models.base_model.BaseModelBackend.token_limit" />

### token\_limit

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

**Returns:**

int: The maximum token limit for the given model.

<a id="camel.models.base_model.BaseModelBackend.stream" />

### stream

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

**Returns:**

bool: Whether the model is in stream mode.
