Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Python function to calculate the cost of each conversation with Gemini

Hello,

I'm trying to write a function that calculates the cost of each message sent to Gemini, but I don't understand what figures to base my calculation on.

Billing is done on input and output tokens. I'm using the Gemini 1.5 Pro model, in text mode only.

I have the following Python code:

 

 

# model: GenerativeModel (from vertexai.generative_models)
# response: list[Content] (from vertexai.generative_models)

print(f"Prompt Token Count: {model.count_tokens(contents).total_tokens}")
print(f"Prompt Character Count: {model.count_tokens(contents).total_billable_characters}")

print(f"Prompt Token Count: {response.usage_metadata.prompt_token_count}")
print(f"Candidates Token Count: {response.usage_metadata.candidates_token_count}")
print(f"Total Token Count: {response.usage_metadata.total_token_count}")

 

 

This show me the following result (in my case):

 

 

Prompt Token Count: 255758
Prompt Character Count: 818
Prompt Token Count: 247
Candidates Token Count: 378
Total Token Count: 625

 

 

I don't unsdertand:
1. Why both "Prompt Token Count" are not the same value
2. What input token value should I divide by 1000 and then multiply by the cost in dollars ( which is for 1000 tokens)?
3. What output token value should I divide by 1000 and then multiply by the cost in dollars ( which is for 1000 tokens)?

Could you help me with these questions?

 
1 REPLY 1

You use caching, token count includes both cached token and prompt token.