Usage Pattern
Source: llama_index

# Usage Pattern## Estimating LLM and Embedding Token CountsIn order to measure LLM and Embedding token counts, you'll need to1. Setup `MockLLM` and `MockEmbedding` objects```pythonfrom llama_index.core.llms import MockLLMfrom llama_index.core import MockEmbeddingllm = MockLLM(max_tokens=256)embed_model = MockEmbedding(embed_dim=1536)```2. Setup the `TokenCountingCallback` handler```pythonimport tiktokenfrom llama_index.core.callbacks import CallbackManager, TokenCountingHandlertoken_counter = TokenCountingHandler(    tokenizer=tiktoken.encoding_for_model("gpt-3.5-turbo").encode)callback_manager = CallbackManager([token_counter])```3. Add them to the global `Settings````pythonfrom llama_index.core import SettingsSettings.llm = llmSettings.embed_model = embed_modelSettings.callback_manager = callback_manager```4. Construct an Index```pythonfrom llama_index.core import VectorStoreIndex, SimpleDirectoryReaderdocuments = SimpleDirectoryReader(    "./docs/examples/data/paul_graham").load_data()index = VectorStoreIndex.from_documents(documents)```5. Measure the counts!```pythonprint(    "Embedding Tokens: ",    token_counter.total_embedding_token_count,    "\n",    "LLM Prompt Tokens: ",    token_counter.prompt_llm_token_count,    "\n",    "LLM Completion Tokens: ",    token_counter.completion_llm_token_count,    "\n",    "Total LLM Token Count: ",    token_counter.total_llm_token_count,    "\n",)# reset countstoken_counter.reset_counts()```6. Run a query, measure again```pythonquery_engine = index.as_query_engine()response = query_engine.query("query")print(    "Embedding Tokens: ",    token_counter.total_embedding_token_count,    "\n",    "LLM Prompt Tokens: ",    token_counter.prompt_llm_token_count,    "\n",    "LLM Completion Tokens: ",    token_counter.completion_llm_token_count,    "\n",    "Total LLM Token Count: ",    token_counter.total_llm_token_count,    "\n",)```