@@ -78,7 +78,8 @@ def __call__(
7878 top_k : Optional [Union [float , str ]] = None ,
7979 top_p : Optional [Union [float , str ]] = None ,
8080 max_tokens : Optional [Union [int , str ]] = None ,
81- max_retries : Optional [int ] = None
81+ max_retries : Optional [int ] = None ,
82+ context : str = "Unspecified"
8283 ) -> Dict [str , Any ]:
8384 """
8485 Make the instance callable with the same signature as the original function.
@@ -109,7 +110,8 @@ def __call__(
109110 top_k = top_k ,
110111 top_p = top_p ,
111112 max_tokens = max_tokens ,
112- max_retries = effective_max_retries
113+ max_retries = effective_max_retries ,
114+ context = context
113115 )
114116
115117 def _preprocess_content_for_cachepoint (self , content : List [Dict [str , Any ]]) -> List [Dict [str , Any ]]:
@@ -177,7 +179,8 @@ def invoke_model(
177179 top_k : Optional [Union [float , str ]] = 5 ,
178180 top_p : Optional [Union [float , str ]] = 0.1 ,
179181 max_tokens : Optional [Union [int , str ]] = None ,
180- max_retries : Optional [int ] = None
182+ max_retries : Optional [int ] = None ,
183+ context : str = "Unspecified"
181184 ) -> Dict [str , Any ]:
182185 """
183186 Invoke a Bedrock model with retry logic.
@@ -335,7 +338,8 @@ def invoke_model(
335338 converse_params = converse_params ,
336339 retry_count = 0 ,
337340 max_retries = effective_max_retries ,
338- request_start_time = request_start_time
341+ request_start_time = request_start_time ,
342+ context = context
339343 )
340344
341345 return result
@@ -346,7 +350,8 @@ def _invoke_with_retry(
346350 retry_count : int ,
347351 max_retries : int ,
348352 request_start_time : float ,
349- last_exception : Exception = None
353+ last_exception : Exception = None ,
354+ context : str = "Unspecified"
350355 ) -> Dict [str , Any ]:
351356 """
352357 Recursive helper method to handle retries for Bedrock invocation.
@@ -424,7 +429,7 @@ def _invoke_with_retry(
424429 response_with_metering = {
425430 "response" : response ,
426431 "metering" : {
427- f"bedrock/{ converse_params ['modelId' ]} " : {
432+ f"{ context } / bedrock/{ converse_params ['modelId' ]} " : {
428433 ** usage
429434 }
430435 }
@@ -470,7 +475,8 @@ def _invoke_with_retry(
470475 retry_count = retry_count + 1 ,
471476 max_retries = max_retries ,
472477 request_start_time = request_start_time ,
473- last_exception = e
478+ last_exception = e ,
479+ context = context
474480 )
475481 else :
476482 logger .error (f"Non-retryable Bedrock error: { error_code } - { error_message } " )
@@ -838,6 +844,7 @@ def _sanitize_response_for_logging(self, response: Dict[str, Any]) -> Dict[str,
838844 top_p: Optional top_p parameter (float or string)
839845 max_tokens: Optional max_tokens parameter (int or string)
840846 max_retries: Optional override for the instance's max_retries setting
847+ context: Context prefix for metering key (default: "Unspecified")
841848
842849Returns:
843850 Bedrock response object with metering information
0 commit comments