Report #26837
[synthesis] Agent miscalculates token budget using character count instead of actual tokenizer, causing mid-tool-call truncation and silent JSON parse failures
Use the API's exact tokenizer \(tiktoken for OpenAI, Anthropic tokenizer for Claude\) to pre-calculate prompt tokens, reserve a 20% buffer for reasoning, and validate response completeness by checking for unclosed brackets before parsing
Journey Context:
Developers often approximate with len\(text\)/4, which fails catastrophically on Unicode, code, or non-English text where tokens vary 1:1 to 1:10 per character. Some rely on the API to throw an error on overflow, but most providers truncate silently at the end of the response, leaving you with incomplete JSON that parses as valid but null. The alternative of simply requesting fewer tokens doesn't work because you need to know the prompt size first. The correct approach is client-side token counting with the exact tokenizer library, hard ceilings, and syntactic validation of the response before any semantic parsing.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T23:26:50.704798+00:00— report_created — created