{"slug": "xml-tagging-in-prompts-the-secret-to-getting-better-output-from-claude-and-gpt", "title": "XML Tagging in Prompts: The Secret to Getting Better Output from Claude and GPT", "summary": "A developer shares a simple prompt-engineering trick: wrapping prompt sections in XML tags like <instructions> and <context> measurably improves output consistency from large language models such as Claude and GPT. The technique leverages models' familiarity with markup from training data, reducing ambiguity in multi-part prompts and simplifying programmatic parsing of responses.", "body_md": "*A simple structuring trick that turns messy, unpredictable LLM outputs into clean, reliable ones.*\n\nIf you've spent any time writing prompts for Claude, GPT, or any other large language model, you've probably hit this wall: your prompt works fine for a simple ask, but the moment you pack in multiple instructions — some context, a few examples, formatting rules, and the actual task — the model starts mixing things up. It answers the wrong part of the question. It ignores your formatting instructions. It treats your example output as part of the actual task.\n\nThe fix is almost embarrassingly simple: **wrap your prompt sections in XML tags.**\n\nLLMs are trained on enormous amounts of code, documentation, and markup. XML (and HTML) syntax is deeply embedded in that training data, which means models are very good at recognizing where one tagged section ends and another begins. Unlike plain paragraphs — where the boundary between \"here's my context\" and \"here's my instruction\" is fuzzy — a tag creates an unambiguous boundary.\n\nAnthropic actually recommends this explicitly for Claude: wrapping distinct parts of a prompt (instructions, context, examples, output format) in tags like `<instructions>`\n\n, `<context>`\n\n, `<example>`\n\n, and `<output_format>`\n\nmeasurably improves consistency, especially in longer or more complex prompts.\n\nThink of it like the difference between handing someone a wall of text versus handing them a form with labeled fields. Both contain the same information, but one is far easier to parse correctly — for a human, and for a model.\n\n**Without tags:**\n\n```\nSummarize the article below in 3 bullet points. Keep it under 50 words.\nUse a neutral tone. Here's an example of the style I want:\n\"- Company X raised $10M in Series A funding.\"\nNow here's the article: [long article text]\n```\n\nThe model has to guess where the instructions end and the article begins — and with a long article, it sometimes starts summarizing the example instead of the real content.\n\n**With tags:**\n\n```\n<instructions>\nSummarize the article in 3 bullet points, under 50 words total.\nUse a neutral tone.\n</instructions>\n\n<example_style>\n- Company X raised $10M in Series A funding.\n</example_style>\n\n<article>\n[long article text]\n</article>\n```\n\nNow there's zero ambiguity. The model knows exactly what's an instruction, what's a style reference, and what's the raw content to work on.\n\nYou don't need a formal schema — these are just semantic containers the model can recognize. Some of the most useful:\n\n`<instructions>`\n\n— the actual task`<context>`\n\n— background info the model needs but shouldn't act on directly`<example>`\n\n/ `<examples>`\n\n— sample inputs/outputs (few-shot prompting)`<document>`\n\nor `<article>`\n\n— source text to analyze/transform`<output_format>`\n\n— exactly how you want the response structured`<thinking>`\n\n— for models that support step-by-step reasoning before the final answerYou can also nest them, e.g., multiple `<document index=\"1\">`\n\n, `<document index=\"2\">`\n\nblocks when feeding several sources at once — attributes work too, and models parse them correctly.\n\nThis trick isn't just for input — you can ask the model to *return* its answer in tags, which makes parsing the response programmatically trivial:\n\n```\n<answer>\nYour final response here.\n</answer>\n<confidence>high</confidence>\n```\n\nIf you're building this into an app (say, a Next.js API route calling the AI SDK), this turns a fragile \"hope the model formats it right\" problem into a simple string-extraction problem — no need for a full JSON schema when you just need one or two fields.\n\nFor a single, simple instruction (\"Translate this to French\"), tags are overkill — they add noise for no benefit. Reach for XML tagging when your prompt has **multiple distinct components** that could be confused with each other: instructions + context + examples + a document to process, all in one message.\n\nXML tagging isn't a hack — it's closer to good API design applied to prompts. You're giving the model an explicit contract instead of hoping it infers your intent from prose. The next time a prompt feels like it's \"almost working,\" try wrapping its parts in tags before you start rewriting the wording. Often, structure — not phrasing — was the actual problem.", "url": "https://wpnews.pro/news/xml-tagging-in-prompts-the-secret-to-getting-better-output-from-claude-and-gpt", "canonical_source": "https://dev.to/maryam_saba_792cdbcdb46fd/xml-tagging-in-prompts-the-secret-to-getting-better-output-from-claude-and-gpt-54e1", "published_at": "2026-08-03 20:39:43+00:00", "updated_at": "2026-08-03 21:11:38.176975+00:00", "lang": "en", "topics": ["large-language-models", "generative-ai", "ai-tools", "developer-tools"], "entities": ["Claude", "GPT", "Anthropic", "Next.js", "AI SDK"], "alternates": {"html": "https://wpnews.pro/news/xml-tagging-in-prompts-the-secret-to-getting-better-output-from-claude-and-gpt", "markdown": "https://wpnews.pro/news/xml-tagging-in-prompts-the-secret-to-getting-better-output-from-claude-and-gpt.md", "text": "https://wpnews.pro/news/xml-tagging-in-prompts-the-secret-to-getting-better-output-from-claude-and-gpt.txt", "jsonld": "https://wpnews.pro/news/xml-tagging-in-prompts-the-secret-to-getting-better-output-from-claude-and-gpt.jsonld"}}