Back to Litellm

LiteLLM Completion Cost

cookbook/LiteLLM_Completion_Cost.ipynb

1.84.0-dev.21.7 KB
Original Source

Use LiteLLM to calculate costs for all your completion calls

In this notebook we'll use litellm.completion_cost to get completion costs

python
!pip install litellm==0.1.549 # use 0.1.549  or later

Calculating costs for gpt-3.5 turbo completion()

python
from litellm import completion, completion_cost
import os
os.environ['OPENAI_API_KEY'] = ""

messages = [{ "content": "Hello, how are you?","role": "user"}]
response = completion(
            model="gpt-3.5-turbo",
            messages=messages,
)

print(response)

cost = completion_cost(completion_response=response)
formatted_string = f"Cost for completion call: ${float(cost):.10f}"
print(formatted_string)

Calculating costs for Together Computer completion()

python
from litellm import completion, completion_cost
import os
os.environ['TOGETHERAI_API_KEY'] = ""

messages = [{ "content": "Hello, how are you?","role": "user"}]
response = completion(
            model="togethercomputer/llama-2-70b-chat",
            messages=messages,
)

print(response)

cost = completion_cost(completion_response=response)
formatted_string = f"Cost for completion call: ${float(cost):.10f}"
print(formatted_string)

Calculating costs for Replicate Llama2 completion()

python
from litellm import completion, completion_cost
import os
os.environ['REPLICATE_API_KEY'] = ""

messages = [{ "content": "Hello, how are you?","role": "user"}]
response = completion(
            model="replicate/llama-2-70b-chat:2796ee9483c3fd7aa2e171d38f4ca12251a30609463dcfd4cd76703f22e96cdf",
            messages=messages,
)

print(response)

cost = completion_cost(completion_response=response)
formatted_string = f"Cost for completion call: ${float(cost):.10f}"
print(formatted_string)