Functional Response Token Accounting
Agents Remember 0.9.5
Makes the response token counter functional: every MCP tool response now reports an accurate token count instead of placeholder zeros.
Highlights
- The token-accounting engine (
models/tokens.py) is now wired into the single response choke point (_tool_payload), so every public tool response carries a realtokens,tokenizer, andtokenCountExact. Previously these shipped as Pydantic defaults (0/""/false). - Default tokenizer is tiktoken
o200k_base(tokenCountExact: true), with a deterministic approximate counter retained as a fallback. - Widened
response_payload/dump_with_token_countto accept anyResponseModel, so operation-less responses such aspingare counted too. - Added
test_tokens.py— first direct coverage of the counters, serializers, and the count fixpoint.
Verification
Full quality wrapper green (ruff clean, Pyright 0 errors, 289 passed / 3 skipped). ping now reports tokens: 44, tokenizer: "tiktoken:o200k_base", tokenCountExact: true.