Blog
Wild & Free Tools

JSON vs YAML for LLM Prompts — Token Costs and Model Preferences

Last updated: March 2026 7 min read
Quick Answer

Table of Contents

  1. Token counts — YAML usually wins
  2. Model accuracy — format usually matters less than clarity
  3. When JSON is the right call
  4. Claude's XML preference and how it interacts with YAML
  5. The switching cost — convert once
  6. Frequently Asked Questions

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:

ContentJSON tokensYAML tokensSavings
Simple object (5 keys)~35~2820%
Nested config (20 keys)~180~13028%
Array of 10 objects~420~32024%

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:

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 Shipping

When JSON Is the Right Call

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 Converter

Frequently 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.

Carlos Mendez
Carlos Mendez Photo Editing & Image Writer

Carlos has been a freelance photographer and photo editor for a decade, working with clients from local businesses to regional magazines.

More articles by Carlos →
Launch Your Own Clothing Brand — No Inventory, No Risk