diff --git a/litellm/main.py b/litellm/main.py index e70055a90a..8be923685a 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -7617,9 +7617,13 @@ async def acount_tokens( ) # Fallback to local tiktoken-based token counting + fallback_messages = messages or [] + if system and fallback_messages: + fallback_messages = [{"role": "system", "content": system}] + fallback_messages local_count = litellm.token_counter( model=model, - messages=messages or [], + messages=fallback_messages, + tools=tools, ) return TokenCountResponse( diff --git a/tests/test_litellm/test_count_tokens_public_api.py b/tests/test_litellm/test_count_tokens_public_api.py index 59a1092455..81ba244796 100644 --- a/tests/test_litellm/test_count_tokens_public_api.py +++ b/tests/test_litellm/test_count_tokens_public_api.py @@ -71,9 +71,11 @@ def test_acount_tokens_with_tools(): tools = [ { "type": "function", - "name": "get_weather", - "description": "Get weather info", - "parameters": {"type": "object", "properties": {"city": {"type": "string"}}}, + "function": { + "name": "get_weather", + "description": "Get weather info", + "parameters": {"type": "object", "properties": {"city": {"type": "string"}}}, + }, } ]