Testing Token Counter Implementation in llama_index
This test suite validates the TokenCountingHandler functionality in llama_index, focusing on accurate token counting for both LLM and embedding operations. It ensures proper event handling and token count tracking across different callback events.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
run-llama/llama_index
llama-index-core/tests/callbacks/test_token_counter.py
"""Embeddings."""
from llama_index.core.callbacks.schema import CBEventType
from llama_index.core.callbacks.token_counting import TokenCountingHandler
TEST_PAYLOAD = {"chunks": ["one"], "formatted_prompt": "two", "response": "three"}
TEST_ID = "my id"
def test_on_event_start() -> None:
"""Test event start."""
handler = TokenCountingHandler()
event_id = handler.on_event_start(
CBEventType.LLM, payload=TEST_PAYLOAD, event_id=TEST_ID
)
assert event_id == TEST_ID
event_id = handler.on_event_start(
CBEventType.EMBEDDING, payload=TEST_PAYLOAD, event_id=TEST_ID
)
assert event_id == TEST_ID
assert len(handler.llm_token_counts) == 0
assert len(handler.embedding_token_counts) == 0
def test_on_event_end() -> None:
"""Test event end."""
handler = TokenCountingHandler()
handler.on_event_end(CBEventType.LLM, payload=TEST_PAYLOAD, event_id=TEST_ID)
assert len(handler.llm_token_counts) == 1
assert len(handler.embedding_token_counts) == 0
handler.on_event_end(CBEventType.EMBEDDING, payload=TEST_PAYLOAD, event_id=TEST_ID)
assert len(handler.llm_token_counts) == 1
assert len(handler.embedding_token_counts) == 1
assert handler.embedding_token_counts[0].total_token_count == 1
assert handler.llm_token_counts[0].total_token_count == 2
# test actual counts
# LLM should be two (prompt plus response)
# Embedding should be one (single token chunk)
assert handler.total_llm_token_count == 2
assert handler.total_embedding_token_count == 1