MCPcopy
hub / github.com/Skyvern-AI/skyvern / _get_encoding

Function _get_encoding

skyvern/utils/token_counter.py:14–27  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

12
13
14def _get_encoding() -> tiktoken.Encoding | None:
15 """Load the gpt-4o encoding lazily. tiktoken may download the encoding from a
16 remote host on first use; in environments without egress that raises, so a
17 failure is cached and callers fall back to a character-based approximation
18 rather than crashing."""
19 global _encoding, _encoding_load_failed
20 if _encoding is not None or _encoding_load_failed:
21 return _encoding
22 try:
23 _encoding = tiktoken.encoding_for_model("gpt-4o")
24 except Exception:
25 _encoding_load_failed = True
26 LOG.warning("Failed to load tiktoken encoding; falling back to approximate token counting", exc_info=True)
27 return _encoding
28
29
30def count_tokens(text: str) -> int:

Callers 3

count_tokensFunction · 0.85
encode_tokensFunction · 0.85
decode_tokensFunction · 0.85

Calls 1

warningMethod · 0.45

Tested by

no test coverage detected