docs: Add context usage reporter hook example

Add Example 6 showing how to create a hook that reports context/token
usage after each Claude response:

- Python script reads transcript_path to access conversation history
- Estimates tokens using ~4 chars/token heuristic
- Outputs one-line report: "Context: ~45k/200k tokens (77% remaining)"
- Documents both Stop and UserPromptSubmit hook configurations
- Explains limitations (estimate vs exact /context command)
This commit is contained in:
Luong NGUYEN
2025-12-24 23:21:38 +01:00
parent e144e42b20
commit 9c5c7c5049
4 changed files with 230 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
# Change: Add Context Usage Reporting Hook Example
## Why
Users want to monitor their context window usage during Claude Code sessions. The hooks documentation currently lacks a practical example showing how to create a hook that reports context/token usage after each user request. This is valuable for understanding when context is getting full and when auto-compaction might occur.
## What Changes
- Add a detailed example hook that reports context usage after each user prompt
- The hook will read the transcript file and estimate token usage
- Include step-by-step explanation of how the hook works
- Document the limitations (estimation vs exact token counts)
## Impact
- **Affected specs**: hooks-documentation (add new example)
- **Affected code**: `06-hooks/README.md` (add new example section)
- **User impact**: Users gain a practical example for monitoring context usage

View File

@@ -0,0 +1,18 @@
# Hooks Documentation Specification
## ADDED Requirements
### Requirement: Context Usage Reporting Hook Example
The hooks lesson SHALL include a detailed example showing how to create a hook that reports context/token usage after each user request.
#### 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 UserPromptSubmit or 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** they understand the report is an estimate based on transcript analysis

View File

@@ -0,0 +1,9 @@
# Tasks: Add Context Usage Reporting Hook Example
## 1. Documentation Update
- [x] 1.1 Add new example section "Context Usage Reporter" to 06-hooks/README.md
- [x] 1.2 Write Python hook script that reads transcript and estimates tokens
- [x] 1.3 Add configuration example for UserPromptSubmit hook
- [x] 1.4 Document how transcript_path provides access to conversation history
- [x] 1.5 Explain token estimation approach and limitations
- [x] 1.6 Show sample output format for the one-line report