diff --git a/common/token_utils.py b/common/token_utils.py index 29f10f7eb..4d5220fc9 100644 --- a/common/token_utils.py +++ b/common/token_utils.py @@ -35,6 +35,12 @@ def num_tokens_from_string(string: str) -> int: return 0 def total_token_count_from_response(resp): + """ + Extract token count from LLM response in various formats. + + Handles None responses and different response structures from various LLM providers. + Returns 0 if token count cannot be determined. + """ if resp is None: return 0 @@ -50,19 +56,19 @@ def total_token_count_from_response(resp): except Exception: pass - if 'usage' in resp and 'total_tokens' in resp['usage']: + if isinstance(resp, dict) and 'usage' in resp and 'total_tokens' in resp['usage']: try: return resp["usage"]["total_tokens"] except Exception: pass - if 'usage' in resp and 'input_tokens' in resp['usage'] and 'output_tokens' in resp['usage']: + if isinstance(resp, dict) and 'usage' in resp and 'input_tokens' in resp['usage'] and 'output_tokens' in resp['usage']: try: return resp["usage"]["input_tokens"] + resp["usage"]["output_tokens"] except Exception: pass - if 'meta' in resp and 'tokens' in resp['meta'] and 'input_tokens' in resp['meta']['tokens'] and 'output_tokens' in resp['meta']['tokens']: + if isinstance(resp, dict) and 'meta' in resp and 'tokens' in resp['meta'] and 'input_tokens' in resp['meta']['tokens'] and 'output_tokens' in resp['meta']['tokens']: try: return resp["meta"]["tokens"]["input_tokens"] + resp["meta"]["tokens"]["output_tokens"] except Exception: