Skip to content

Commit 1b8df6f

Browse files
committed
update bedrock claude usage
1 parent 58608a9 commit 1b8df6f

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

metagpt/provider/bedrock/base_provider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import json
22
from abc import ABC, abstractmethod
3-
from typing import Union
3+
from typing import Optional, Union
44

55

66
class BaseBedrockProvider(ABC):
77
# to handle different generation kwargs
88
max_tokens_field_name = "max_tokens"
9+
usage: Optional[dict] = None
910

1011
def __init__(self, reasoning: bool = False, reasoning_max_token: int = 4000):
1112
self.reasoning = reasoning

metagpt/provider/bedrock/bedrock_provider.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ def get_choice_text_from_stream(self, event) -> Union[bool, str]:
6868
elif delta_type == "signature_delta":
6969
completions = ""
7070
return reasoning, completions
71-
else:
72-
return False, ""
71+
elif rsp_dict["type"] == "message_stop":
72+
self.usage = {
73+
"prompt_tokens": rsp_dict.get("amazon-bedrock-invocationMetrics", {}).get("inputTokenCount", 0),
74+
"completion_tokens": rsp_dict.get("amazon-bedrock-invocationMetrics", {}).get("outputTokenCount", 0),
75+
}
76+
return False, ""
7377

7478

7579
class CohereProvider(BaseBedrockProvider):

metagpt/provider/bedrock_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ async def _achat_completion_stream(self, messages: list[dict], timeout=USE_CONFI
130130
collected_content = await self._get_stream_response_body(stream_response)
131131
log_llm_stream("\n")
132132
full_text = ("".join(collected_content)).lstrip()
133+
if self.__provider.usage:
134+
# if provider provide usage, update it
135+
self._update_costs(self.__provider.usage, self.config.model)
133136
return full_text
134137

135138
def _get_response_body(self, response) -> dict:

metagpt/utils/token_counter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@
373373
"anthropic.claude-3-haiku-20240307-v1:0:200k": {"prompt": 0.00025, "completion": 0.00125},
374374
# currently (2024-4-29) only available at US West (Oregon) AWS Region.
375375
"anthropic.claude-3-opus-20240229-v1:0": {"prompt": 0.015, "completion": 0.075},
376+
"anthropic.claude-3-5-sonnet-20241022-v2:0": {"prompt": 0.003, "completion": 0.015},
377+
"us.anthropic.claude-3-5-sonnet-20241022-v2:0": {"prompt": 0.003, "completion": 0.015},
378+
"anthropic.claude-3-7-sonnet-20250219-v1:0": {"prompt": 0.003, "completion": 0.015},
379+
"us.anthropic.claude-3-7-sonnet-20250219-v1:0": {"prompt": 0.003, "completion": 0.015},
376380
"cohere.command-text-v14": {"prompt": 0.0015, "completion": 0.0015},
377381
"cohere.command-text-v14:7:4k": {"prompt": 0.0015, "completion": 0.0015},
378382
"cohere.command-light-text-v14": {"prompt": 0.0003, "completion": 0.0003},

0 commit comments

Comments
 (0)