Skip to content

Commit

Permalink
Prevent duplicate TokenCalcHandler callbacks on Agent (crewAIInc#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackhayter authored Apr 16, 2024
1 parent 2ee6ab6 commit 575f1f9
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/crewai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,14 @@ def set_agent_executor(self) -> "Agent":
"""set agent executor is set."""
if hasattr(self.llm, "model_name"):
token_handler = TokenCalcHandler(self.llm.model_name, self._token_process)
if isinstance(self.llm.callbacks, list):

# Ensure self.llm.callbacks is a list
if not isinstance(self.llm.callbacks, list):
self.llm.callbacks = []

# Check if an instance of TokenCalcHandler already exists in the list
if not any(isinstance(handler, TokenCalcHandler) for handler in self.llm.callbacks):
self.llm.callbacks.append(token_handler)
else:
self.llm.callbacks = [token_handler]

if not self.agent_executor:
if not self.cache_handler:
Expand Down

0 comments on commit 575f1f9

Please sign in to comment.