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
@@ -0,0 +1,20 @@
# Tasks: Fix Context Usage Hook Token Calculation
## Implementation Tasks
### 1. Fix the bug in 06-hooks/README.md example
- [x] Update line 564 in the context-usage.py example: change `estimate_tokens(str(total_chars))` to `total_chars // 4`
- [x] Remove the now-unused `estimate_tokens()` function from the example
### 2. Update user's local hook file
- [x] Fix `~/.claude/hooks/context-usage.py` with the same correction
### 3. Verification
- [x] Test the hook by running Claude Code and confirming non-zero token count is displayed
- [x] Verify the percentage calculation is reasonable (should show usage increasing during conversation)
## Acceptance Criteria
- [x] After Claude responds, the Stop hook displays a non-zero token estimate
- [x] The displayed percentage decreases as conversation grows
- [x] Example in README.md matches the corrected implementation
@@ -1,20 +0,0 @@
# Tasks: Fix Context Usage Hook Token Calculation
## Implementation Tasks
### 1. Fix the bug in 06-hooks/README.md example
- [ ] Update line 564 in the context-usage.py example: change `estimate_tokens(str(total_chars))` to `total_chars // 4`
- [ ] Remove the now-unused `estimate_tokens()` function from the example
### 2. Update user's local hook file
- [ ] Fix `~/.claude/hooks/context-usage.py` with the same correction
### 3. Verification
- [ ] Test the hook by running Claude Code and confirming non-zero token count is displayed
- [ ] Verify the percentage calculation is reasonable (should show usage increasing during conversation)
## Acceptance Criteria
- [ ] After Claude responds, the Stop hook displays a non-zero token estimate
- [ ] The displayed percentage decreases as conversation grows
- [ ] Example in README.md matches the corrected implementation