Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 4 min read

Python's json.dumps made my Russian prompt 3.6x more expensive with one default

Here is a pattern almost every LLM app has somewhere. You load some records from a database, turn them into JSON with json.dumps, and paste that string into the prompt so the model can answer questions about them. It wor

Here is a pattern almost every LLM app has somewhere. You load some records from a database, turn them into JSON with json.dumps, and paste that string into the prompt so the model can answer questions about them. It works and the answers look fine. If your records are in English, nothing else happens. If they contain Russian, Chinese, accented names or emoji, you are probably paying several times more for that part of the prompt than you need to.

The cause is one default argument in Python's standard library.

A few words first

A token is a chunk of text the model reads as one piece; APIs bill per million of them. Input tokens are what you send, output tokens are what the model writes back. A tokenizer is the program that splits text into tokens; I used tiktoken with the o200k_base encoding, the one the GPT-5.x models use. ASCII is the basic 128-character set: English letters, digits, punctuation. An escape is a way to write a character using only ASCII, like ΠΏ for the Cyrillic letter "ΠΏ".

What json.dumps does by default

json.dumps has a parameter called ensure_ascii, and it defaults to True. With that default, every character outside ASCII is replaced by its \uXXXX escape. The JSON is still valid and any JSON parser will read it back correctly. But the model doesn't parse your prompt. It reads the characters you actually sent.

import json
import tiktoken

enc = tiktoken.get_encoding("o200k_base")
for text in ["ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ΠΌΠΈΡ€", "naΓ―ve rΓ©sumΓ©", "東京", "πŸ‘"]:
    escaped = json.dumps(text)[1:-1]  # drop the quotes
    print(f"{text!r}: {len(enc.encode(text))} tokens as text, "
          f"{len(enc.encode(escaped))} as {escaped}")

"ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ΠΌΠΈΡ€" ("Hello, world") is 4 tokens as normal text and 29 tokens once escaped. "naΓ―ve rΓ©sumΓ©" goes from 4 to 15. Two Japanese characters, one token as text, take 7. A single thumbs-up emoji takes 9, because it becomes two escapes. A tokenizer has learned common words in many languages, so normal text packs several letters into one token. Escaped text is a string of hex digits it has seen far less often, so it ends up as many short pieces.

Measured on real text

Short strings are a demo, so I wanted real data. The catalog of the LLM gateway I work on has a short public description of each of its 50 chat models, in Russian and in English. I built the kind of payload an app would paste into a prompt: a list of 50 records with id, name and description, and counted tokens three ways.

Payload (50 records) Default json.dumps ensure_ascii=False Multiplier
Russian descriptions 10,014 tokens 2,753 tokens 3.64x
Only the Russian description strings 8,679 tokens 1,417 tokens 6.12x
English descriptions 2,492 tokens 2,492 tokens 1.00x

The English row is the control. Those descriptions contain no characters outside ASCII, so the default changes nothing, which is exactly why many developers never notice. The Russian records cost 200 tokens each instead of 55. The full payload is 3.64x rather than 6x because the IDs, model names and JSON keys are ASCII and cost the same either way.

For comparison, pretty-printing the same Russian JSON with indent=2 adds about 11% (2,753 to 3,054 tokens). The escapes add 264%. If you were going to optimize one thing in that JSON, the indentation is not it.

What it costs per month

Take a support bot that sends 300 tokens of instructions plus 20 of those Russian records on every call and gets back a 250-token answer. With the default, the input is 4,306 tokens. With ensure_ascii=False, it's 1,401.

At the official GPT-5.6 list price of $2 per 1M input tokens and $12 per 1M output, the call costs $0.0116 instead of $0.0058, exactly twice as much. At 10,000 calls a day, that's $3,483 a month instead of $1,741: about $1,740 a month for a default argument. On GPT-5.6 mini the multiplier is the same and the amounts are ten times smaller, $348 versus $174.

The fix

Pass ensure_ascii=False wherever JSON goes into a prompt:

context = json.dumps(records, ensure_ascii=False)

Two details matter. First, this only applies to JSON you put inside the text of a message. When an SDK sends your request to the API, it may escape the request body, and that's fine: the server decodes it before the tokenizer ever sees it. The problem is a JSON string that becomes part of the prompt itself. Second, other languages differ. JavaScript's JSON.stringify doesn't escape non-ASCII by default, so this mostly bites Python code and any library that wraps json.dumps with its defaults.

To find it in an existing project, search for json.dumps( near wherever you build prompts, or count tokens of your final prompt and look for \u in it. A prompt with Russian text should not contain the string \u04.

What this doesn't solve

I didn't measure answer quality. Models can usually read escaped text, but nothing promises they read it as well as plain text, and a model may copy the escapes back into its answer, where you pay output prices. Removing them should help or do nothing, but I haven't tested it.

The token counts come from one tokenizer. Claude and Gemini use their own, so the exact multiplier on their models will differ, even though escapes are long hex strings for any tokenizer.

The bot scenario is my assumption: 20 records, 300 tokens of instructions and 250 tokens of output. If most of your prompt is English instructions and the non-English data is small, the saving is small too.

And this fixes the escapes, not the language. Russian text still costs more tokens than the same meaning in English. Unescaping removes the extra cost the default added on top of that, nothing more.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.