Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix: Token usage tracking fails in streaming mode (#3603)

Wendong-Fan5daac34f9adb
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index d97b6df6d..f61818fdd 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -3357,9 +3357,11 @@ class ChatAgent(BaseAgent):
tracker (Dict[str, int]): The token usage tracker to update.
usage_dict (Dict[str, int]): The usage dictionary with new values.
"""
- tracker["prompt_tokens"] += usage_dict.get("prompt_tokens", 0)
- tracker["completion_tokens"] += usage_dict.get("completion_tokens", 0)
- tracker["total_tokens"] += usage_dict.get("total_tokens", 0)
+ tracker["prompt_tokens"] += usage_dict.get("prompt_tokens") or 0
+ tracker["completion_tokens"] += (
+ usage_dict.get("completion_tokens") or 0
+ )
+ tracker["total_tokens"] += usage_dict.get("total_tokens") or 0
def _convert_to_chatagent_response(
self,