Skip to content

Automate browser based workflows with AI

SKY-10703: make tiktoken encoding load non-blocking in token_counter (#6405)

Shuchang Zhengf4314b2bbaad
diff --git a/skyvern/utils/token_counter.py b/skyvern/utils/token_counter.py
index ed23ddf69..08352f1db 100644
--- a/skyvern/utils/token_counter.py
+++ b/skyvern/utils/token_counter.py
@@ -1,15 +1,48 @@
+import math
+
+import structlog
import tiktoken
-_ENCODING = tiktoken.encoding_for_model("gpt-4o")
+LOG = structlog.get_logger()
+
+_APPROX_CHARS_PER_TOKEN = 4
+
+_encoding: tiktoken.Encoding | None = None
+_encoding_load_failed = False
+
+
+def _get_encoding() -> tiktoken.Encoding | None:
+ """Load the gpt-4o encoding lazily. tiktoken may download the encoding from a
+ remote host on first use; in environments without egress that raises, so a
+ failure is cached and callers fall back to a character-based approximation
+ rather than crashing."""
+ global _encoding, _encoding_load_failed
+ if _encoding is not None or _encoding_load_failed:
+ return _encoding
+ try:
+ _encoding = tiktoken.encoding_for_model("gpt-4o")
+ except Exception:
+ _encoding_load_failed = True
+ LOG.warning("Failed to load tiktoken encoding; falling back to approximate token counting", exc_info=True)
+ return _encoding
def count_tokens(text: str) -> int:
- return len(_ENCODING.encode(text))
+ encoding = _get_encoding()
+ if encoding is None:
+ return math.ceil(len(text) / _APPROX_CHARS_PER_TOKEN)
+ return len(encoding.encode(text))
def encode_tokens(text: str) -> list[int]:
- return _ENCODING.encode(text)
+ encoding = _get_encoding()
+ if encoding is None:
+ return [ord(char) for char in text]
+ return encoding.encode(text)
def decode_tokens(tokens: list[int]) -> str:
- return _ENCODING.decode(tokens)
+ encoding = _get_encoding()
+ if encoding is None:
+ return "".join(chr(token) for token in tokens)
+ return encoding.decode(tokens)