-
.envfile created with ANTHROPIC_API_KEY -
dotenvpackage installed - Extension rebuilt with
npm run rebuild-and-reload - VS Code completely restarted (CRITICAL!)
- MCP Server is running (check status bar)
Before running full test suite, try these quick validation tests:
count_tokens({
text: "Hello, world!"
})Expected: ~3-4 tokens
count_tokens({
filepath: "TOKEN_COUNTING_TESTING_GUIDE.md"
})Expected: Should return token count for this file
count_tokens({
filepath: "TOKEN_COUNTING_TESTING_GUIDE.md",
startLine: 1,
endLine: 10
})Expected: Should return token count for first 10 lines
Test Simple Text:
count_tokens({
text: "Hello, world! This is a test."
})Expected Output:
**Token Counting Results:**
**Tool Result Details:**
- tokenCount: <number>
- characterCount: 34
- model: claude-sonnet-4-5-20250929
- source: direct_text
Test with Test File:
count_tokens({
filepath: "token-counting-test.md"
})Expected Output:
**Token Counting Results:**
**Tool Result Details:**
- tokenCount: <number>
- characterCount: <number>
- model: claude-sonnet-4-5-20250929
- source: file
- filepath: token-counting-test.md
Test Lines 1-20:
count_tokens({
filepath: "token-counting-test.md",
startLine: 1,
endLine: 20
})Expected Output:
**Token Counting Results:**
**Tool Result Details:**
- tokenCount: <number>
- characterCount: <number>
- model: claude-sonnet-4-5-20250929
- source: file_lines
- filepath: token-counting-test.md
- linesProcessed: 1-20
Test with Claude Opus:
count_tokens({
text: "Testing with Claude Opus model.",
model: "claude-opus-4-20250514"
})Expected Output:
**Token Counting Results:**
**Tool Result Details:**
- tokenCount: <number>
- characterCount: 33
- model: claude-opus-4-20250514
- source: direct_text
Test with Large Source File:
count_tokens({
filepath: "src/tools/edit-tools.ts"
})Expected Output:
- Should successfully count tokens
- May show warning if file >16MB
Test Multiple Discontinuous Ranges:
count_tokens({
filepath: "src/tools/file-tools.ts",
lineRanges: [
{ startLine: 1, endLine: 50, description: "Imports and type definitions" },
{ startLine: 100, endLine: 200, description: "Core file operations" },
{ startLine: 400, endLine: 500, description: "Token counting functions" }
]
})Expected Output:
**Token Counting Results:**
**Tool Result Details:**
- tokenCount: <sum of all ranges>
- characterCount: <sum of all ranges>
- model: claude-sonnet-4-5-20250929
- source: file_multiple_ranges
- filepath: src/tools/file-tools.ts
- rangeCount: 3
**Range Breakdown:**
- Range 1 (lines 1-50): <tokens> tokens, <chars> characters
Description: Imports and type definitions
- Range 2 (lines 100-200): <tokens> tokens, <chars> characters
Description: Core file operations
- Range 3 (lines 400-500): <tokens> tokens, <chars> characters
Description: Token counting functions
Test Multiple Ranges (Minimal):
count_tokens({
filepath: "CLAUDE.md",
lineRanges: [
{ startLine: 1, endLine: 10 },
{ startLine: 50, endLine: 60 }
]
})Expected Output:
**Token Counting Results:**
**Tool Result Details:**
- tokenCount: <sum>
- characterCount: <sum>
- model: claude-sonnet-4-5-20250929
- source: file_multiple_ranges
- filepath: CLAUDE.md
- rangeCount: 2
**Range Breakdown:**
- Range 1 (lines 1-10): <tokens> tokens, <chars> characters
- Range 2 (lines 50-60): <tokens> tokens, <chars> characters
Remove or unset ANTHROPIC_API_KEY, then:
count_tokens({
text: "This should fail"
})Expected Error:
Error: ANTHROPIC_API_KEY environment variable is not set. Please set it to use the token counting feature.
count_tokens({
text: "Hello",
filepath: "test.md"
})Expected Error:
Error: Cannot provide both "text" and "filepath" parameters. Please use only one.
count_tokens({})Expected Error:
Error: Either "text" or "filepath" parameter must be provided
count_tokens({
text: "Hello",
startLine: 1,
endLine: 10
})Expected Error:
Error: Parameters "startLine" and "endLine" can only be used with "filepath"
count_tokens({
filepath: "token-counting-test.md",
startLine: 100,
endLine: 50
})Expected Error:
Error: "startLine" must be <= "endLine"
count_tokens({
filepath: "test.md",
startLine: 1,
endLine: 10,
lineRanges: [{ startLine: 20, endLine: 30 }]
})Expected Error:
Error: Cannot use both "startLine"/"endLine" and "lineRanges". Please use only one approach.
count_tokens({
text: "Hello",
lineRanges: [{ startLine: 1, endLine: 10 }]
})Expected Error:
Error: Parameter "lineRanges" can only be used with "filepath"
count_tokens({
filepath: "test.md",
lineRanges: []
})Expected Error:
Error: "lineRanges" must contain at least one range
count_tokens({
filepath: "test.md",
lineRanges: [
{ startLine: 0, endLine: 10 }
]
})Expected Error:
Error: lineRanges[0].startLine must be >= 1
count_tokens({
filepath: "nonexistent-file.txt"
})Expected Error:
Error: <file not found error from VS Code>
-
Open VS Code Output Panel
- View → Output
- Select "MCP Server" from dropdown
- This shows all logging from the tool
-
Start MCP Server
- Check status bar for MCP server status
- If not running, use command palette: "Toggle MCP Server"
-
Run Each Test Case
- Use your MCP client to call the
count_tokenstool - Verify output matches expected format
- Check Output panel for detailed logs
- Use your MCP client to call the
-
Verify Token Counts
- Token counts should be reasonable (roughly 1 token per 4 characters)
- Compare with Anthropic's web interface if needed
Solution:
- Check the
.envfile exists in workspace root - Check the API key is on the line starting with
ANTHROPIC_API_KEY= - Completely restart VS Code (close all windows, reopen)
- Check the Output panel (View → Output → "MCP Server") for loading confirmation
- Alternative: Verify env var is set:
echo $env:ANTHROPIC_API_KEY(PowerShell)
Solution:
- Verify extension is installed: Check Extensions panel
- Rebuild:
npm run rebuild-and-reload - Restart VS Code completely
- Check MCP server is running (status bar should show server status)
- Try toggling the MCP Server off and on again
Solution:
- Check which model is being used
- Different models may tokenize slightly differently
- Compare with Anthropic's official tokenizer if available
Solution:
- Verify API key is valid
- Check internet connection
- Review error message in Output panel
- Ensure model name is correct
✅ All basic test cases pass
✅ Error handling works correctly
✅ File size validation works
✅ Line range support works
✅ Multiple models supported
✅ Output format is correct
✅ Logging is comprehensive
- Document any issues found
- Consider adding automated tests
- Update user documentation
- Consider additional features:
- Batch token counting
- Token count caching
- Support for images/PDFs
- Cost estimation based on token count