Skip to content

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

fix(model): refresh anthropic streaming usage time (#2927)

ANIRUDDHA ADAKa7df48448d45
diff --git a/src/agentscope/model/_anthropic/_model.py b/src/agentscope/model/_anthropic/_model.py
index 96f37f579..b28d723b9 100644
--- a/src/agentscope/model/_anthropic/_model.py
+++ b/src/agentscope/model/_anthropic/_model.py
@@ -564,6 +564,14 @@ class AnthropicChatModel(ChatModelBase):
elif event.type == "message_delta":
if event.usage and usage:
usage.output_tokens = event.usage.output_tokens
+ # ``usage`` was stamped when ``message_start``
+ # arrived, i.e. at time-to-first-token. Refresh it
+ # here so ``time`` spans the whole generation, as
+ # every other provider (and this model's
+ # non-streaming path) already does.
+ usage.time = (
+ datetime.now() - start_datetime
+ ).total_seconds()
if delta_res.content:
delta_res.usage = usage