File size: 1,860 Bytes
ddebc3e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | """Unified token estimation utilities.
All API surfaces share the same tokenizer-backed approximation so usage
reporting stays consistent across OpenAI-compatible and Anthropic-compatible
responses.
"""
from functools import lru_cache
from typing import Any
import orjson
import tiktoken
PROMPT_OVERHEAD = 4
_ENCODING_NAME = "o200k_base"
@lru_cache(maxsize=1)
def _get_encoding() -> tiktoken.Encoding:
return tiktoken.get_encoding(_ENCODING_NAME)
def _coerce_text(value: Any) -> str:
if value is None:
return ""
if isinstance(value, str):
return value
try:
return orjson.dumps(value).decode()
except (TypeError, ValueError):
return str(value)
def estimate_tokens(value: Any) -> int:
text = _coerce_text(value).strip()
if not text:
return 0
return len(_get_encoding().encode(text, disallowed_special=()))
def estimate_prompt_tokens(value: Any, *, overhead: int = PROMPT_OVERHEAD) -> int:
base = estimate_tokens(value)
if base <= 0:
return 0
return base + max(0, overhead)
def estimate_tool_call_tokens(tool_calls: list[Any]) -> int:
normalized: list[Any] = []
for call in tool_calls:
if isinstance(call, dict):
normalized.append(call)
continue
name = getattr(call, "name", None)
arguments = getattr(call, "arguments", None)
call_id = getattr(call, "call_id", None)
if name is not None and arguments is not None:
normalized.append({
"id": call_id or "",
"name": name,
"arguments": arguments,
})
continue
normalized.append(call)
return estimate_tokens(normalized)
__all__ = [
"PROMPT_OVERHEAD",
"estimate_tokens",
"estimate_prompt_tokens",
"estimate_tool_call_tokens",
]
|