Completion Token Usage & Cost
By default LiteLLM returns token usage in all completion requests (See here)
LiteLLM returns response_cost in all calls.
from litellm import completion
response = litellm.completion(
model="gpt-6-luna",
messages=[{"role": "user", "content": "Hey, how's it going?"}],
mock_response="Hello world",
)
print(response._hidden_params["response_cost"])
LiteLLM also exposes some helper functions:
-
encode: This encodes the text passed in, using the model-specific tokenizer. Jump to code -
decode: This decodes the tokens passed in, using the model-specific tokenizer. Jump to code -
token_counter: This returns the number of tokens for a given input - it uses the tokenizer based on the model, and defaults to tiktoken if no model-specific tokenizer is available. Jump to code -
create_pretrained_tokenizerandcreate_tokenizer: LiteLLM provides default tokenizer support for OpenAI, Cohere, Anthropic, Llama2, and Llama3 models. If you are using a different model, you can create a custom tokenizer and pass it ascustom_tokenizerto theencode,decode, andtoken_countermethods. Jump to code -
cost_per_token: This returns the cost (in USD) for prompt (input) and completion (output) tokens. Uses the live list fromapi.litellm.ai. Jump to code -
completion_cost: This returns the overall cost (in USD) for a given LLM API Call. It combinestoken_counterandcost_per_tokento return the cost for that query (counting both cost of input and output). Jump to code -
get_max_tokens: This returns the maximum number of tokens allowed for the given model. Jump to code -
model_cost: This returns a dictionary for all models, with their max_tokens, input_cost_per_token and output_cost_per_token. It uses theapi.litellm.aicall shown below. Jump to code -
register_model: This registers new / overrides existing models (and their pricing details) in the model cost dictionary. Jump to code -
api.litellm.ai: Live token + price count across all supported models. Jump to code
This is a community maintained list. Contributions are welcome!
Example Usage
1. encode
Encoding has model-specific tokenizers for anthropic, cohere, llama2 and openai. If an unsupported model is passed in, it'll default to using tiktoken (openai's tokenizer).
from litellm import encode, decode
sample_text = "Hellö World, this is my input string!"
# openai encoding + decoding
openai_tokens = encode(model="gpt-6-luna", text=sample_text)
print(openai_tokens)
2. decode
Decoding is supported for anthropic, cohere, llama2 and openai.
from litellm import encode, decode
sample_text = "Hellö World, this is my input string!"
# openai encoding + decoding
openai_tokens = encode(model="gpt-6-luna", text=sample_text)
openai_text = decode(model="gpt-6-luna", tokens=openai_tokens)
print(openai_text)
3. token_counter
from litellm import token_counter
messages = [{"user": "role", "content": "Hey, how's it going"}]
print(token_counter(model="gpt-6-luna", messages=messages))
4. create_pretrained_tokenizer and create_tokenizer
from litellm import create_pretrained_tokenizer, create_tokenizer
# get tokenizer from huggingface repo
custom_tokenizer_1 = create_pretrained_tokenizer("Xenova/llama-3-tokenizer")
# use tokenizer from json file
with open("tokenizer.json") as f:
json_data = json.load(f)
json_str = json.dumps(json_data)
custom_tokenizer_2 = create_tokenizer(json_str)