YAML vs JSON for LLM prompts: size, tokens and reliable output

Why YAML is usually shorter than JSON in LLM prompts, how to measure the token difference yourself, and why JSON is still the right format for model output.

Guides By Convert JSON to YAML Team

YAML vs JSON for LLM prompts: size, tokens and reliable output

Structured data shows up in prompts all the time: a customer record, a product catalogue, tool results, few-shot examples. It is usually JSON, because that is what the API returned. Converting it to YAML before it goes into the prompt is a small change that often makes the prompt shorter. Here is what that buys you, how to measure it, and where JSON should stay.

The same data, two formats

JSON, pretty-printed (467 bytes)
{
  "customer": {
    "id": "C-1042",
    "name": "Ada Lopez",
    "plan": "pro",
    "country": "ES"
  },
  "orders": [
    {
      "id": "O-1",
      "status": "shipped",
      "total": 129.9,
      "items": [
        "keyboard",
        "mouse"
      ]
    },
    {
      "id": "O-2",
      "status": "processing",
      "total": 49.5,
      "items": [
        "usb-c cable"
      ]
    }
  ],
  "preferences": {
    "language": "es",
    "newsletter": false
  }
}
YAML (296 bytes)
customer:
  id: C-1042
  name: Ada Lopez
  plan: pro
  country: ES
orders:
  - id: O-1
    status: shipped
    total: 129.9
    items:
      - keyboard
      - mouse
  - id: O-2
    status: processing
    total: 49.5
    items:
      - usb-c cable
preferences:
  language: es
  newsletter: false

For this record, the YAML is 37% smaller than the pretty-printed JSON. The difference is almost all punctuation: the JSON version contains 88 quotes, braces, brackets and commas, the YAML version none at all. Minified JSON (284 bytes) is smaller still, but it is also the hardest version for a person, or a model, to scan.

Does fewer characters mean fewer tokens?

Usually, but not by a fixed ratio. Tokenizers split text into chunks learned from training data, and a run of punctuation such as "},{" may be one token or several depending on the model. Indentation spaces in YAML also cost tokens. So the honest answer is: measure with your model’s tokenizer.

  • For OpenAI models, the open-source tiktoken library counts tokens locally.
  • Several providers, including Anthropic, offer a token-counting endpoint in their API.
  • Most API responses report the input token count, so you can also compare two real requests.

In practice the saving is largest for data with many short keys and values, where quotes and braces make up a big share of the text, and smallest for data dominated by long free-text strings.

Readability matters as much as size

Models read prompts the way people do: the less noise around the data, the easier it is to pick out. YAML’s nesting by indentation and its lack of quotes make records and lists of options easy to follow, and you can add # comments to explain a field. For few-shot examples that the model should imitate, YAML is often the clearest way to show structure.

Keep JSON for what the model writes

The picture flips for output that your code will parse:

  • Most major model APIs can constrain output to a JSON Schema (often called structured outputs or tool calling). That guarantee only exists for JSON.
  • JSON has one way to write each value. Model-written YAML can drift in indentation, and a YAML 1.1 parser will read an unquoted no as false or 1.10 as 1.1.
  • If you do accept YAML from a model, parse it with a safe loader such as yaml.safe_load in Python, and validate the result before you use it.

A practical recipe

  1. Convert JSON context to YAML before putting it in the prompt, with the JSON to YAML converter or a one-liner from the Python guide.
  2. Keep the YAML 1.1 safe option on, so ambiguous values such as "NO" or "007" stay quoted.
  3. Ask for output as JSON, ideally with a JSON Schema, and validate what comes back, for example with the JSON formatter and validator.
  4. Measure tokens for your own data and model before and after; keep the change if it pays off.

← Blog