XML tag token overhead calculator
Claude follows structured prompts more reliably when you wrap sections in
XML tags — but every <tag> and </tag> costs tokens on every call. This
tool counts your prompt with tags, strips them to get the tagless
equivalent, and reports the exact overhead so you can judge whether the
structure pays for itself.
How it works
The calculator counts tokens for your prompt as written, then removes all XML tags with a markup-stripping pass and counts the plain-text version. The difference is your XML overhead, shown in both absolute tokens and as a percent of the tagged total. Token counts use the ≈ 4-characters-per-token heuristic that tracks Claude’s tokenizer closely for English text.
A single tag pair like <instructions>...</instructions> adds the opening and
closing tags plus their angle brackets — usually a handful of tokens. The cost
scales with how many sections you tag, not with the content inside them.
Why XML tags are used in Claude prompts
Anthropic’s guidance recommends XML tags for multi-part prompts because they give Claude unambiguous delimiters. Plain prose prompts rely on Claude inferring where one section ends and the next begins; with XML tags the boundary is syntactically explicit. This matters most when you combine multiple distinct pieces — a system instruction, a reference document, several examples, and an output format rule — into one long prompt.
The tags also make it easier to refer back to a section. A prompt that says
“based on the contents of the <document> tag above” is clearer than “based
on the text I provided earlier.”
Worked example
Consider a structured prompt like this:
<instructions>
Classify the sentiment of each review. Return JSON.
</instructions>
<examples>
Review: "Great product" -> {"sentiment": "positive"}
Review: "Broken on arrival" -> {"sentiment": "negative"}
</examples>
<reviews>
Review 1: "Absolutely love it."
Review 2: "Packaging was damaged."
</reviews>
The four XML tag pairs add approximately 16 tokens (four opening tags, four closing tags, each a few tokens). The entire prompt might be around 60 tokens tagged. Without tags — just the raw prose instructions and content with line breaks — it comes to about 44 tokens. That is roughly 27% overhead, which for a repeating classification pipeline that runs thousands of times, amounts to real cost. But for a one-off task the reliability gain of explicit structure is almost always worth it.
Tips for cost-effective tagging
- Tag structure, not every sentence. Wrap the big sections —
<document>,<instructions>,<examples>,<format>— not individual lines. - Reuse short tag names.
<doc>costs fewer tokens than<reference_document>and Claude handles both equally well. - Skip tags on tiny prompts. A one-line instruction does not need a wrapper; the overhead is pure waste there.
- Keep tags for long inputs. When you paste a large document, the few tokens of structure are negligible and the reliability gain is large.
- Measure before optimising. Use this tool to get the actual overhead number. Tag costs often feel significant but measure at 2–5% for real production prompts, making them a poor target for optimisation compared with trimming verbose instructions or unnecessary examples.