Module core.token_counter

Functions

def deduplicate_events(events: List[Any]) ‑> List[Any]

Helper function to deduplicate events if they are logged twice (temporary fix for #444)

def triage_events(events: List[Any]) ‑> Tuple[List[Any], List[Any]]

Split events into world model and action engine events based on prompt start

Classes

class TokenCounter (log=False)

Token counter for each of MM LLM and embedding models. It relies on TokenCountingHandler from llama_index. Only one token counter can be used at a time.

Methods

def calculate_embedding_pricing(self, token_count: int, model: str) ‑> int

Computes token costs for Embedding according to the pricing data available in pricing_config.yaml

def calculate_llm_pricing(self, input_token_count: int, output_token_count: int, model: str) ‑> Tuple[int, int, int]

Computes token costs for LLM according to the pricing data available in pricing_config.yaml

def count_tokens(self, events: List[Any], model) ‑> Tuple[int, int, int]

Helper function to sum token counts for a list of events that belong together

def get_token_count(self, event_type)
def process_token_usage(self,
world_model: lavague.core.world_model.WorldModel,
action_engine: lavague.core.action_engine.ActionEngine,
result_to_update: lavague.core.base_engine.ActionResult | None = None) ‑> Tuple[dict, dict]

Computes token counts and costs for the current step