fix: correct token calculation in context-usage hook example

The hook was converting total_chars to string before calculating tokens,
resulting in ~0 tokens reported. Fixed to calculate directly from char count.

- Remove unused estimate_tokens() function
- Calculate tokens as total_chars // 4 directly
- Archive fix-context-usage-hook change
This commit is contained in:
Luong NGUYEN
2025-12-24 23:54:35 +01:00
parent ecd3dba49a
commit 513171332e
10 changed files with 132 additions and 28 deletions

View File

@@ -516,10 +516,6 @@ MODEL_LIMITS = {
"haiku": 200000,
}
def estimate_tokens(text: str) -> int:
"""Estimate token count from text. ~4 characters per token on average."""
return len(text) // 4
def read_transcript(transcript_path: str) -> list:
"""Read JSONL transcript file and return list of messages."""
messages = []
@@ -561,7 +557,7 @@ def calculate_usage(messages: list) -> tuple[int, int]:
if tool_input:
total_chars += len(json.dumps(tool_input))
estimated_tokens = estimate_tokens(str(total_chars))
estimated_tokens = total_chars // 4 # ~4 characters per token
return total_chars, estimated_tokens
def main():