Switch agent output from JSON to TOON for ~19% token reduction#4940
Switch agent output from JSON to TOON for ~19% token reduction#4940pushpak1300 wants to merge 2 commits intophpstan:2.2.xfrom
Conversation
Adds helgesverre/toon and creates ToonErrorFormatter that outputs in TOON (Token-Oriented Object Notation) format. When an AI agent is detected, PHPStan now outputs in TOON instead of JSON, reducing token consumption while remaining machine-parseable. Follows up on phpstan#4938.
|
You've opened the pull request against the latest branch 2.2.x. PHPStan 2.2 is not going to be released for months. If your code is relevant on 2.1.x and you want it to be released sooner, please rebase your pull request and change its target to 2.1.x. |
| "helgesverre/toon": "^1.0", | ||
| "shipfastlabs/agent-detector": "^1.0", |
There was a problem hiding this comment.
I wonder what's your policy about added library in PHPStan @ondrejmirtes ?
Adding library with low number of star and/or low number of contributors/maintainers is always a risk (especially for security reason).
I always have this story in mind
|
I said you should try |
|
It could still be exposed as a standalone extension I'm not sure about it but I think PHPStan expose enough extension point to add custom formatter, no ? |
This pull request switches the AI agent output format from JSON to TOON (Token-Oriented Object Notation) — a compact, machine-parseable format optimized for LLM contexts.
Follows up on #4938.
Why:
TOON saves ~15% tokens compared to JSON while keeping the same data structure fully machine-parseable. It achieves this by declaring field names once as a header (
messages[9]{message,line,ignorable,identifier}:) and encoding each error as a single comma-separated line — no repeated keys, braces, or brackets per entry.Token comparison (9 real errors, measured with
o200k_basetokenizer):How detection works:
CLAUDECODE/CLAUDE_CODEenvironment variableCURSOR_TRACE_ID/CURSOR_AGENTGEMINI_CLI,CODEX_SANDBOX,AI_AGENT,AUGMENT_AGENT,OPENCODE,REPL_ID, etc.)shipfastlabs/agent-detectorand automatically switches to TOON format + suppresses progress bar animations — no flags neededWhat changed:
helgesverre/toondependency for TOON encodingToonErrorFormatter— builds the same error data structure asJsonErrorFormatterbut encodes as TOONtoonformat instead ofjson--error-format=toonOutput:
Explicit
--error-format=tableor configerrorFormat: tablealways takes priority over agent detection.related laravel/pint#415