JSON vs YAML for LLM Prompts — Token Costs and Model Preferences
- YAML is typically 15-30% fewer tokens than equivalent JSON — meaningful cost savings at scale.
- GPT-4o, Claude, and Gemini all parse both; model accuracy usually tracks clarity more than format.
- JSON still wins for structured output (function calling) where the API response needs strict typing.
Table of Contents
Feeding data to an LLM? YAML costs fewer tokens than equivalent JSON — typically 15-30% fewer, which compounds at production scale. All major models (GPT, Claude, Gemini) parse both formats accurately. This guide covers token cost differences, which models prefer which format, and when JSON still wins (function calling, structured output).
Token Counts — YAML Usually Wins
Compare token counts for equivalent data:
| Content | JSON tokens | YAML tokens | Savings |
|---|---|---|---|
| Simple object (5 keys) | ~35 | ~28 | 20% |
| Nested config (20 keys) | ~180 | ~130 | 28% |
| Array of 10 objects | ~420 | ~320 | 24% |
The savings come from skipping braces, quotes, and commas. YAML's minimal syntax uses fewer tokens. At 10 million prompts a day with a 20% token savings on the input, that's real money — GPT-4o input is ~$2.50/million tokens, Claude Sonnet input is ~$3/million.
Model Accuracy — Format Usually Matters Less Than Clarity
For most structured data tasks, LLMs parse JSON and YAML with equivalent accuracy. What moves accuracy more:
- Clear field names (use
customer_emailnotce). - Consistent structure across examples.
- Examples that show the expected output shape.
Format choice is usually a wash on accuracy but meaningful on cost. Pick YAML to save tokens unless your specific use case requires JSON.
Sell Custom Apparel — We Handle Printing & Free ShippingWhen JSON Is the Right Call
- Function calling / structured output. OpenAI and Anthropic both accept JSON schemas to constrain responses. The API returns JSON. Keep the input JSON too for consistency.
- Tool use loops. Agents that invoke tools and receive JSON responses benefit from JSON-native formatting throughout.
- Strict validation requirements. If your post-processing uses a JSON schema validator, keep everything JSON.
- Training / fine-tuning data. Most training datasets are JSON. Match the format.
For prompt context that's purely read by the model, YAML saves tokens with no downside.
Claude's XML Preference and How It Interacts With YAML
Anthropic recommends XML-like tags for Claude prompt structure (<instructions>, <examples>). But within those tags, structured data can be JSON, YAML, or prose — Claude parses all three.
Common pattern: XML tags for prompt structure, YAML inside for the structured data:
<instructions> Classify the sentiment of each review. </instructions> <reviews> - id: 1 text: The service was great. - id: 2 text: Never again. </reviews>
Best of both worlds: Anthropic-recommended tag structure, YAML savings for the data. Works equally well on GPT and Gemini.
The Switching Cost — Convert Once
If your prompt-building pipeline emits JSON and you want to switch to YAML, convert once with our browser tool to see the token-count savings, then update the pipeline.
For some teams, the token savings don't justify updating mature prompt infrastructure. That's fine — JSON is still a reasonable default. For any new prompt, consider YAML first.
Save Tokens on Every Prompt
Paste JSON prompt data, click Convert, feed YAML to your LLM. Smaller, cheaper, same accuracy.
Open Free JSON to YAML ConverterFrequently Asked Questions
Is YAML actually fewer tokens than JSON?
Yes, for equivalent data — typically 15-30% fewer. Savings come from no braces, fewer quotes, no commas. The exact ratio depends on structure; deep nesting saves more than flat objects.
Does Claude really prefer XML tags?
Yes, for prompt structure. Anthropic published that explicitly. But within a tagged section, JSON or YAML both work — Claude parses both. Pair XML tags for structure with YAML for data to get both benefits.
What about tool calling / function calling?
Tool calling APIs return JSON. If your prompts use tools, JSON consistency through the loop is easier. YAML-in, JSON-out works too but adds a small mental-model cost.
How much do the token savings matter in practice?
At 10M prompts/day with 28% savings on a ~1000-token input, you save ~$7,000/month on GPT-4o input costs. At low volume, it's negligible. Decide based on scale.

