CommencerCommencer gratuitement

Calculating the cost

Before deploying AI features at scale, it's essential to estimate costs. The cost is dependent on the number of input and output tokens used and the model chosen.

Your task is to calculate the cost of summarizing customer chat transcripts.

The OpenAI client, along with a prompt and max_tokens, are preloaded for you.

Cet exercice fait partie du cours

Working with DeepSeek in Python

Afficher le cours

Instructions

  • Extract the input token usage from the response.
  • Complete the cost calculation to add the output token cost.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3",
    messages=[{"role": "user", "content": prompt}],
    max_tokens=max_tokens
)

input_token_price = 1.25 / 1_000_000
output_token_price = 1.25 / 1_000_000

# Extract token usage
input_tokens = response.____.____
output_tokens = max_tokens
# Calculate cost
cost = (input_tokens * input_token_price + ____ * ____)
print(f"Estimated cost: ${cost}")
Modifier et exécuter le code