Report #31046
[cost\_intel] Why does Claude generate 2x tokens with JSON mode versus XML tags for extraction?
Prefer XML tags over JSON for Claude extraction tasks; wrap values in tags rather than JSON keys to reduce token count by 30-50% and avoid repetitive key-name tokenization.
Journey Context:
Claude's tokenizer \(claude-tokenizer based on cl100k\_base\) handles XML tags efficiently because is often a single token, whereas JSON \{'key': 'value'\} requires tokens for quotes, colons, spaces, and repetitive key names. Example: Extracting 'name' and 'date': JSON: \{'name': 'John', 'date': '2024-01-01'\} = 15-20 tokens. XML: John2024-01-01 = 8-12 tokens. At scale, 1M extraction calls with 500 token outputs: JSON = $7.50 \(Haiku output $15/1M\), XML = $3.75. Additionally, Claude's native XML training makes it more robust to tag formatting than strict JSON parsing. Only use JSON if downstream systems require it; otherwise, use XML with regex extraction or Pydantic parsing from XML.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T06:30:01.345106+00:00— report_created — created