CloudCodeTree LogoCloudCodeTree
AI NewsTutorialsAbout
CloudCodeTree Logo
CloudCodeTree
  • AI News
  • Tutorials
  • About
← Back to AI News
Delimit Your Claude System Prompts With XML Tags — It Reduces Drift in Long Agent Sessions

Delimit Your Claude System Prompts With XML Tags — It Reduces Drift in Long Agent Sessions

Chris Harper

3 min read

Aug 2, 2026 · 20:02 UTC

AI
Workflow
Best Practices
Agents

Wrapping Claude system-prompt sections in separate role, instructions, context, and output_format XML tags reduces drift in long agent sessions — a five-minute change to any existing prompt.

Most Claude agent system prompts are plain prose paragraphs. That works for short sessions, but as a conversation extends past 10–15 turns, Claude starts blending constraints with context. "You have access to a PostgreSQL database" ends up weighted the same as "never delete rows without confirmation." Structured tags fix this by giving Claude clear semantic buckets.

Anthropic's prompt engineering docs specifically recommend this pattern: Claude's training reinforces XML-delimited structure (it's the same format Claude uses internally for tool call results and chain-of-thought tags).

The before and after

Before — one blob of prose:

You are a senior backend engineer. You have access to the codebase in /workspace.
Work on one file at a time. Never modify tests. Output only changed code with no
explanation. If a task requires creating new files, ask first.

After — four semantic buckets:

<role>
You are a senior backend engineer working on a production Python codebase.
</role>

<context>
The codebase is mounted at /workspace. Framework: FastAPI + PostgreSQL.
No internet access. Available tools: read_file, write_file, run_tests.
</context>

<instructions>
- Work on one file at a time.
- Never modify files under /workspace/tests/.
- If a task requires creating new files, confirm with the user before proceeding.
- Output only the changed code. No preamble, no explanation.
</instructions>

<output_format>
One fenced code block per changed file, annotated with the file path as the info string:
```python /workspace/app/auth.py
</output_format>

Why four tags?

TagWhat goes in itWhy it's separate
<role>Who Claude is in this sessionSets persona without competing with rules
<context>Facts about the environmentInformation to reason from, not rules to follow
<instructions>Rules and constraintsHighest-priority section — followed most reliably
<output_format>Response shapeIsolated so format drift doesn't corrupt rules

Handle examples carefully

If your system prompt includes examples, put them last and use <good_example> / <bad_example> subtags. Claude generalizes from examples — a plain "bad example" can become a learned pattern if it's the last thing in the prompt. Explicit tags make Claude treat them as classification examples, not demonstrations to emulate.

This costs nothing to add and compounds in value across longer sessions where context windows shrink and reliable recall of earlier instructions matters more.

Sources: Use XML Tags to Structure Your Prompts — Anthropic Prompt Engineering · System Prompts — Anthropic Docs