Skip to content

Build and run agents you can see, understand and trust.

fix(message): keep a copy of the usage appended to a message (#2805)

Grapette.L13a855be5cc1
diff --git a/src/agentscope/message/_base.py b/src/agentscope/message/_base.py
index 65205eb10..2fc189d03 100644
--- a/src/agentscope/message/_base.py
+++ b/src/agentscope/message/_base.py
@@ -519,12 +519,15 @@ class Msg(BaseModel):
def append_usage(self, usage: Usage) -> Self:
"""Accumulate the token usage of one model call into this message.
+ The message keeps a copy of ``usage``, so the object handed over by
+ the caller is never adopted and keeps its own value.
+
Args:
usage (`Usage`):
The token usage to be accumulated.
"""
if self.usage is None:
- self.usage = usage
+ self.usage = usage.model_copy()
else:
self.usage.input_tokens += usage.input_tokens
self.usage.output_tokens += usage.output_tokens
diff --git a/tests/message_test.py b/tests/message_test.py
index 6731909bf..f5187344b 100644
--- a/tests/message_test.py
+++ b/tests/message_test.py
@@ -16,6 +16,7 @@ from agentscope.message import (
ToolCallBlock,
ToolResultBlock,
ToolResultState,
+ Usage,
)
@@ -312,3 +313,81 @@ class MessageTest(IsolatedAsyncioTestCase):
),
],
)
+
+ async def test_append_usage_keeps_a_copy(self) -> None:
+ """The message accumulates a copy of the usage it is given."""
+ first = Usage(
+ input_tokens=10,
+ output_tokens=5,
+ cache_input_tokens=2,
+ cache_creation_input_tokens=1,
+ )
+ second = Usage(input_tokens=7, output_tokens=3, cache_input_tokens=4)
+ msg = AssistantMsg(name="agent", content=[])
+ msg.append_usage(first)
+ msg.append_usage(second)
+ accumulated = msg.usage
+ assert accumulated is not None
+
+ self.assertDictEqual(
+ {
+ "accumulated": accumulated.model_dump(),
+ "first": first.model_dump(),
+ "second": second.model_dump(),
+ },
+ {
+ "accumulated": {
+ "input_tokens": 17,
+ "output_tokens": 8,
+ "cache_input_tokens": 6,
+ "cache_creation_input_tokens": 1,
+ },
+ # Each model call keeps its own usage, so it can still be
+ # reported or accumulated into other messages afterwards.
+ "first": {
+ "input_tokens": 10,
+ "output_tokens": 5,
+ "cache_input_tokens": 2,
+ "cache_creation_input_tokens": 1,
+ },
+ "second": {
+ "input_tokens": 7,
+ "output_tokens": 3,
+ "cache_input_tokens": 4,
+ "cache_creation_input_tokens": 0,
+ },
+ },
+ )
+
+ async def test_append_usage_does_not_share_one_object(self) -> None:
+ """Messages given the same usage stay independent."""
+ shared = Usage(input_tokens=4, output_tokens=2)
+ first = AssistantMsg(name="agent_a", content=[])
+ second = AssistantMsg(name="agent_b", content=[])
+ first.append_usage(shared)
+ second.append_usage(shared)
+ first.append_usage(Usage(input_tokens=1, output_tokens=1))
+ first_usage = first.usage
+ second_usage = second.usage
+ assert first_usage is not None and second_usage is not None
+
+ self.assertDictEqual(
+ {
+ "first": first_usage.model_dump(),
+ "second": second_usage.model_dump(),
+ },
+ {
+ "first": {
+ "input_tokens": 5,
+ "output_tokens": 3,
+ "cache_input_tokens": 0,
+ "cache_creation_input_tokens": 0,
+ },
+ "second": {
+ "input_tokens": 4,
+ "output_tokens": 2,
+ "cache_input_tokens": 0,
+ "cache_creation_input_tokens": 0,
+ },
+ },
+ )