Files
llm-connect/llm_connect/_token_estimator.py
tegwick e499edba90 feat: initial llm-connect package scaffold
Copy markitect.llm module into standalone llm_connect package.
All markitect.* imports replaced with llm_connect.* equivalents.
LLMError base class inlined (no markitect.exceptions dependency).
Verified: from llm_connect import create_adapter works.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-27 07:54:42 +01:00

17 lines
467 B
Python

"""
Rough token estimation for backends that don't return usage data.
Uses the ~4 characters per token heuristic common across English LLM tokenizers.
"""
def estimate_tokens(text: str) -> int:
"""Estimate the number of tokens in *text*.
This is intentionally coarse — it is only used by the Claude Code CLI
adapter where real token counts are unavailable.
"""
if not text:
return 0
return max(1, len(text) // 4)