From fb5414393e63c065ee6b00ed8a4a14fa77c3b5a6 Mon Sep 17 00:00:00 2001 From: John Karam <168653085+johnkaram02@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:19:34 +0000 Subject: [PATCH] refactor(token_counter): read function and custom tool call text through one lookup --- litellm/litellm_core_utils/token_counter.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.py index 5784e91f98c..6c85a6606c6 100644 --- a/litellm/litellm_core_utils/token_counter.py +++ b/litellm/litellm_core_utils/token_counter.py @@ -571,11 +571,12 @@ def _count_function_call_tokens( total = 0 for tool_call in value: if "function" in tool_call: - total += count_function(str(tool_call["function"].get("arguments", ""))) + kind, field = "function", "arguments" elif "custom" in tool_call: - total += count_function(str(tool_call["custom"].get("input", ""))) + kind, field = "custom", "input" else: raise ValueError(f"Unsupported tool call {tool_call} must contain a function or custom key") + total += count_function(str(tool_call[kind].get(field, ""))) return total if key == "function_call": if not isinstance(value, Mapping):