What did Andrej Karpathy say about JSON versus YAML token efficiency?

Andrej Karpathy says YAML can be more token-efficient than JSON, which matters because token count affects both context length and cost. In his example, the same data takes 116 tokens in JSON and 99 in YAML. His advice is to pay attention to token density and choose encodings that are compact when you can.

“More efficient in tokens. Um, so for example, these are the same in JSON and in YAML. The JSON is 116 and the YAML is 99, so quite a bit of an improvement. And so in the token economy, where we are paying per token in many ways, and you are paying in the context length, and you're paying in, um, dollar amount for the cost of processing all this kind of structured data, when you have to, um, so prefer to use the YAML over JSON. And in general, kind of like, the tokenization density is something that you have to, um, sort of care about and worry about at all times and try to find efficient encoding schemes, and spend a lot of time in the tokenizer, and measure the different token efficiencies of different formats and settings and so on.”
— Andrej Karpathy, Talk / tutorial presentation, 2019 · at 2:09:36
Andrej Karpathy: Andrej Karpathy on Tokenization: Building GPT's Tokenizer

Key takeaways

  • Choose more token-dense formats when cost or context matters.
  • Measure token counts for your data formats, not just readability.
  • Small serialization choices can meaningfully affect LLM usage.

Context

Near the end of his 2019 tokenization talk, Karpathy gives practical advice on format efficiency and token costs. The quote above is taken word for word from the recording's transcript.

In the transcript

…I wanted to just briefly mention, point out, although I think a lot of people are quite aware of this, is that different kinds of formats and different representations and different languages and so on might be more or less efficient with GPT tokenizers, uh, or any tokenizers for any other L for that matter. So, for example, JSON is actually really dense in tokens, and YAML is a lot.

More efficient in tokens. Um, so for example, these are the same in JSON and in YAML. The JSON is 116 and the YAML is 99, so quite a bit of an improvement. And so in the token economy, where we are paying per token in many ways, and you are paying in the context length, and you're paying in, um, dollar amount for the cost of processing all this kind of structured data, when you have to, um, so prefer to use the YAML over JSON. And in general, kind of like, the tokenization density is something that you have to, um, sort of care about and worry about at all times and try to find efficient encoding schemes, and spend a lot of time in the tokenizer, and measure the different token efficiencies of different formats and settings and so on.

Concludes my fairly long video on tokenization. I know it's a try, I know it's annoying, I know it's irritating. I personally really dislike this stage. What I do have to say at this point is don't brush it off. There's a lot of foot guns, sharp edges here, security issues, AI safety issues. As we saw, plugging in unallocated memory into language models. So, it's worth understanding this stage.…

Read the full transcript of Talk / tutorial presentation

Topics

More questions about Andrej Karpathy

All Andrej Karpathy answers · All Andrej Karpathy recordings

How this answer was made: the quote is checked word for word against the transcript of the recording and confirmed as the speaker's own words; the summary above it was drafted with AI from that transcript and quality-checked. Methodology and corrections.

Search every transcript
Open the full search page →