Delimiters & Markdown Structuring: Using XML Tags to Eliminate Ambiguity
When prompts intermix instructions, reference documents, few-shot examples, and user questions into a continuous blob of English prose, the transformer's attention heads must expend representational capacity simply disambiguating where instructions end and where data begins. This ambiguity is the primary cause of prompt leakage, instruction omission, and format drift.
XML Delimiters provide a clean, human-readable, and machine-parseable syntax hierarchy. Claude 3.5 Sonnet and modern OpenAI models are explicitly trained to navigate and respect nested XML structures (e.g., <context>, <rules>, <instructions>).
Sanitizing Dynamic Input against Tag Breakouts
If user input contains the literal string </context>, it could prematurely close the context block. In production, always sanitize or entity-encode dangerous delimiters:
import html
def wrap_xml_context(tag_name: str, raw_content: str) -> str:
# Escape existing tag closers to prevent premature breakout
escaped_content = raw_content.replace(f"</{tag_name}>", f"</{tag_name}>")
return f"<{tag_name}>\n{escaped_content}\n</{tag_name}>"
