bench/tokens.py: ANTHROPIC_API_KEY presence selects the anthropic counter, but the SDK was removed in v7.5 — 6 bench tests fail with ModuleNotFoundError
Summary
bench/tokens.py selects the Anthropic token counter whenever ANTHROPIC_API_KEY is present in the environment, but the anthropic SDK was removed from project dependencies in v7.5 (per the comment in pyproject.toml). The branch has no ImportError fallback, so the 7-test module tests/test_bench_ops_12_total_session_cost.py fails with ModuleNotFoundError: No module named 'anthropic' on any machine where that env var is set — including every shell spawned inside a Claude Code session, which exports ANTHROPIC_API_KEY by default. That makes the default correctness gate red for exactly the audience most likely to run it.
Environment
- Release 3.0.3 (
bc28bd9), macOS (Apple Silicon), Python 3.12.13, editable install viascripts/install.sh - Reproduced identically on a second machine profile (Python 3.11.15, uv venv) once the env var was introduced
Reproduction
# fails — 6 of 7 tests error at import
ANTHROPIC_API_KEY=dummy .venv/bin/python -m pytest tests/test_bench_ops_12_total_session_cost.py
# passes — 7 of 7
env -u ANTHROPIC_API_KEY .venv/bin/python -m pytest tests/test_bench_ops_12_total_session_cost.py
The key's value is never used before the crash: selection at bench/tokens.py:113 only checks presence, and _anthropic_count_tokens fails at import anthropic (bench/tokens.py:26) before any API call.
bench/total_session_cost.py:150: in run_total_session_cost
t = _simulate_turn(turn, counter)
bench/total_session_cost.py:133: in _simulate_turn
return int(counter("\n".join(p for p in parts if p)))
bench/tokens.py:26: in _anthropic_count_tokens
import anthropic
E ModuleNotFoundError: No module named 'anthropic'
Suggested fix
Gate the branch on the SDK actually being importable, falling through to the existing tiktoken/char4 chain:
elif os.environ.get("ANTHROPIC_API_KEY") and importlib.util.find_spec("anthropic"):
counter = _anthropic_count_tokens
mode = "anthropic-count-tokens"
or wrap the counter call in try/except ImportError with fallthrough. Either keeps the documented v7.5 posture (anthropic SDK optional, tiktoken cl100k as the proxy) while preserving the opt-in exact counter for users who do have the SDK installed.
Possibly related (minor, can split out)
Under full-suite memory pressure (~1.3 GB RSS), tests/test_step_memory_relief.py::test_relief_returns_six_key_numeric_telemetry_dict failed once by ~0.001 MB on the unclamped-delta epsilon (assert 0.066 <= (1286.619 - 1286.554) + 0.001); 10/10 isolated runs pass. The two RSS samples race under load, and the 0.001 MB tolerance is tighter than psutil RSS jitter at that scale. Happy to open separately if preferred.
Fixed in v3.0.5: both counter-selection sites now gate on the anthropic SDK being importable and fall back to the tiktoken proxy. Thanks for the report.