@@ -661,6 +661,7 @@ def extract_usage_from_chunk(chunk: Any) -> Dict[str, Optional[int]]:
661661def extract_litellm_metadata (response : Any , model_name : str ) -> Dict [str , Any ]:
662662 """Extract LiteLLM-specific metadata from response."""
663663 metadata = {}
664+ response_headers = {}
664665
665666 try :
666667 # Extract hidden parameters
@@ -689,6 +690,7 @@ def extract_litellm_metadata(response: Any, model_name: str) -> Dict[str, Any]:
689690 if 'additional_headers' in hidden_params :
690691 headers = hidden_params ['additional_headers' ]
691692 if headers :
693+ response_headers = headers
692694 metadata ['response_headers' ] = headers
693695
694696 # Extract system fingerprint if available
@@ -697,9 +699,48 @@ def extract_litellm_metadata(response: Any, model_name: str) -> Dict[str, Any]:
697699
698700 # Extract response headers if available
699701 if hasattr (response , '_response_headers' ):
700- metadata ['response_headers' ] = dict (response ._response_headers )
702+ response_headers = dict (response ._response_headers )
703+ metadata ['response_headers' ] = response_headers
704+
705+ # Fallback: Extract cost from x-litellm-response-cost header if cost is missing or zero
706+ if not metadata .get ('cost' ) and response_headers :
707+ cost_from_header = _extract_cost_from_headers (response_headers )
708+ if cost_from_header is not None :
709+ metadata ['cost' ] = cost_from_header
701710
702711 except Exception as e :
703712 logger .debug ("Error extracting LiteLLM metadata: %s" , e )
704713
705714 return metadata
715+
716+
717+ def _extract_cost_from_headers (headers : Dict [str , Any ]) -> Optional [float ]:
718+ """Extract cost from LiteLLM response headers."""
719+ try :
720+ # Try to get cost from x-litellm-response-cost header
721+ cost_str = headers .get ('x-litellm-response-cost' )
722+ if cost_str is not None :
723+ # Handle string values (headers are often strings)
724+ if isinstance (cost_str , str ):
725+ cost = float (cost_str )
726+ else :
727+ cost = float (cost_str )
728+
729+ if cost > 0 :
730+ return cost
731+
732+ # Fallback to x-litellm-response-cost-original if primary is zero/missing
733+ cost_original_str = headers .get ('x-litellm-response-cost-original' )
734+ if cost_original_str is not None :
735+ if isinstance (cost_original_str , str ):
736+ cost = float (cost_original_str )
737+ else :
738+ cost = float (cost_original_str )
739+
740+ if cost > 0 :
741+ return cost
742+
743+ except (ValueError , TypeError ) as e :
744+ logger .debug ("Error parsing cost from headers: %s" , e )
745+
746+ return None
0 commit comments