chore: Archive update-skills-lesson change

This commit is contained in:
Luong NGUYEN committed 2025-12-24 23:52:39 +01:00
1 parent e978c4949d
commit ecd3dba49a
10 files changed
+302

No files matched your search

@@ -0,0 +1,44 @@
# Change: Fix Context Usage Hook Token Calculation
## Why
The context-usage hook (Stop event) always reports 0 tokens because of a bug in line 68 of `context-usage.py`:
```python
estimated_tokens = estimate_tokens(str(total_chars))
```
This converts the integer `total_chars` (e.g., `156789`) to a string `"156789"` and then calculates tokens from that 6-character string, resulting in `6 // 4 = 1` token instead of `156789 // 4 = ~39,197` tokens.
**Current behavior:**
```
Stop says: Context: ~0/200,000 tokens (100.0% remaining)
```
**Expected behavior:**
```
Stop says: Context: ~39,197/200,000 tokens (80.4% remaining)
```
## What
Fix the token calculation bug in the context-usage.py example hook:
1. Remove the erroneous `str()` conversion
2. Calculate estimated tokens directly from character count
## Scope
- **Files affected:**
- `06-hooks/README.md` - Update the example code
- User's `~/.claude/hooks/context-usage.py` - Not managed by this repo, but fix will be documented
## Out of Scope
- Improving transcript parsing logic
- Adding more sophisticated token estimation
- Adding new hook features
## Risks
- **Low:** Simple bug fix with clear expected behavior
- Users who copied the broken example will need to update their hook manually
@@ -0,0 +1,24 @@
# hooks-documentation Specification Delta
## MODIFIED Requirements
### Requirement: Context Usage Reporting Hook Example
The hooks lesson SHALL include a correct, working example showing how to create a hook that reports context/token usage after each Claude response.
#### Scenario: Token calculation is correct
- **WHEN** a user copies the context-usage.py example
- **AND** runs it as a Stop hook
- **THEN** the hook correctly calculates estimated tokens from total character count
- **AND** displays a non-zero token count proportional to conversation length
#### Scenario: User learns to create context monitoring hook
- **WHEN** a user reads the context usage reporter example
- **THEN** they find a complete Python script that reads the transcript file
- **AND** they understand how to estimate token usage from conversation history
- **AND** they see the configuration for Stop hooks
- **AND** they understand the limitations of token estimation
#### Scenario: Hook output format is documented
- **WHEN** a user implements the context usage hook
- **THEN** they can generate a one-line report showing used tokens and remaining capacity
- **AND** the output shows realistic token counts based on conversation size
@@ -0,0 +1,20 @@
# 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