From 1bdb33245485c5ac55a3dc2a8b5404aea4849c67 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Mon, 5 Feb 2024 08:46:51 -0800 Subject: [PATCH] fix(utils.py): handle count response tokens false case token counting --- litellm/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/litellm/utils.py b/litellm/utils.py index 875ffd33feb..8df027b8745 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -3042,6 +3042,8 @@ def openai_token_counter( # This is the case where we need to count tokens for a streamed response. We should NOT add +3 tokens per message in this branch num_tokens = len(encoding.encode(text, disallowed_special=())) return num_tokens + elif text is not None: + num_tokens = len(encoding.encode(text, disallowed_special=())) num_tokens += 3 # every reply is primed with <|start|>assistant<|message|> return num_tokens