Skip to content

Commit 294e247

Browse files
yiranwu0qingyun-wusonichi
authored andcommitted
Add usage summary for agents (#1269)
* update * update * Update notebook/oai_client_cost.ipynb Co-authored-by: Chi Wang <[email protected]> * update doc and test --------- Co-authored-by: Qingyun Wu <[email protected]> Co-authored-by: Chi Wang <[email protected]>
1 parent f40afb4 commit 294e247

File tree

6 files changed

+442
-12
lines changed

6 files changed

+442
-12
lines changed

autogen/agent_utils.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from typing import List, Dict, Tuple
2+
from autogen import Agent
3+
4+
5+
def gather_usage_summary(agents: List[Agent]) -> Tuple[Dict[str, any], Dict[str, any]]:
6+
"""Gather usage summary from all agents.
7+
8+
Args:
9+
agents: (list): List of agents.
10+
11+
Returns:
12+
tuple: (total_usage_summary, actual_usage_summary)
13+
14+
Example return:
15+
total_usage_summary = {
16+
'total_cost': 0.0006090000000000001,
17+
'gpt-35-turbo':
18+
{
19+
'cost': 0.0006090000000000001,
20+
'prompt_tokens': 242,
21+
'completion_tokens': 123,
22+
'total_tokens': 365
23+
}
24+
}
25+
`actual_usage_summary` follows the same format.
26+
If none of the agents incurred any cost (not having a client), then the total_usage_summary and actual_usage_summary will be {'total_cost': 0}.
27+
"""
28+
29+
def aggregate_summary(usage_summary: Dict[str, any], agent_summary: Dict[str, any]) -> None:
30+
if agent_summary is None:
31+
return
32+
usage_summary["total_cost"] += agent_summary.get("total_cost", 0)
33+
for model, data in agent_summary.items():
34+
if model != "total_cost":
35+
if model not in usage_summary:
36+
usage_summary[model] = data.copy()
37+
else:
38+
usage_summary[model]["cost"] += data.get("cost", 0)
39+
usage_summary[model]["prompt_tokens"] += data.get("prompt_tokens", 0)
40+
usage_summary[model]["completion_tokens"] += data.get("completion_tokens", 0)
41+
usage_summary[model]["total_tokens"] += data.get("total_tokens", 0)
42+
43+
total_usage_summary = {"total_cost": 0}
44+
actual_usage_summary = {"total_cost": 0}
45+
46+
for agent in agents:
47+
if agent.client:
48+
aggregate_summary(total_usage_summary, agent.client.total_usage_summary)
49+
aggregate_summary(actual_usage_summary, agent.client.actual_usage_summary)
50+
51+
return total_usage_summary, actual_usage_summary

autogen/agentchat/conversable_agent.py

+24
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,8 @@ def reset(self):
699699
self.clear_history()
700700
self.reset_consecutive_auto_reply_counter()
701701
self.stop_reply_at_receive()
702+
if self.client is not None:
703+
self.client.clear_usage_summary()
702704
for reply_func_tuple in self._reply_func_list:
703705
if reply_func_tuple["reset_config"] is not None:
704706
reply_func_tuple["reset_config"](reply_func_tuple["config"])
@@ -1890,3 +1892,25 @@ def process_last_message(self, messages):
18901892
messages = messages.copy()
18911893
messages[-1]["content"] = processed_user_text
18921894
return messages
1895+
1896+
def print_usage_summary(self, mode: Union[str, List[str]] = ["actual", "total"]) -> None:
1897+
"""Print the usage summary."""
1898+
if self.client is None:
1899+
print(f"No cost incurred from agent '{self.name}'.")
1900+
else:
1901+
print(f"Agent '{self.name}':")
1902+
self.client.print_usage_summary(mode)
1903+
1904+
def get_actual_usage(self) -> Union[None, Dict[str, int]]:
1905+
"""Get the actual usage summary."""
1906+
if self.client is None:
1907+
return None
1908+
else:
1909+
return self.client.actual_usage_summary
1910+
1911+
def get_total_usage(self) -> Union[None, Dict[str, int]]:
1912+
"""Get the total usage summary."""
1913+
if self.client is None:
1914+
return None
1915+
else:
1916+
return self.client.total_usage_summary

autogen/oai/openai_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"gpt-4-0613": (0.03, 0.06),
4343
"gpt-4-32k-0613": (0.06, 0.12),
4444
# 11-06
45-
"gpt-3.5-turbo": (0.001, 0.002),
45+
"gpt-3.5-turbo": (0.0015, 0.002), # default is still 0613
4646
"gpt-3.5-turbo-1106": (0.001, 0.002),
4747
"gpt-35-turbo-1106": (0.001, 0.002),
4848
"gpt-4-1106-preview": (0.01, 0.03),

0 commit comments

Comments
 (0)