Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

feat: use Gemini response metadata for token counting (#11743)

Shota Totsuka7d5a38581162
diff --git a/api/core/model_runtime/model_providers/google/llm/llm.py b/api/core/model_runtime/model_providers/google/llm/llm.py
index 9a1b13f96f4..86865757770 100644
--- a/api/core/model_runtime/model_providers/google/llm/llm.py
+++ b/api/core/model_runtime/model_providers/google/llm/llm.py
@@ -292,8 +292,12 @@ class GoogleLargeLanguageModel(LargeLanguageModel):
)
else:
# calculate num tokens
- prompt_tokens = self.get_num_tokens(model, credentials, prompt_messages)
- completion_tokens = self.get_num_tokens(model, credentials, [assistant_prompt_message])
+ if hasattr(response, "usage_metadata") and response.usage_metadata:
+ prompt_tokens = response.usage_metadata.prompt_token_count
+ completion_tokens = response.usage_metadata.candidates_token_count
+ else:
+ prompt_tokens = self.get_num_tokens(model, credentials, prompt_messages)
+ completion_tokens = self.get_num_tokens(model, credentials, [assistant_prompt_message])
# transform usage
usage = self._calc_response_usage(model, credentials, prompt_tokens, completion_tokens)