KKosmo
Back to Blog
Prompt Engineering6 min read

How to Write the Perfect Prompt for Claude: XML Tags, Thinking Blocks, and More

K
Harshal Dorlikar, Founder of Kosmo
·

How do you prompt Claude effectively? You stop treating it like a human and start treating it like a compiler. Claude follows instructions literally. It does not infer intent the way other models do. So the quality of your output is tied directly to the structure of your input.

To write the perfect prompt for Claude, you must use XML tags to separate your instructions from your context. You need to assign the model a clear role in the system prompt. You should provide concrete examples of the exact output format you want. And you need to leverage advanced features like extended thinking for complex logic tasks. This guide covers the specific conventions Anthropic recommends for Claude Fable 5, Sonnet 5, and Opus 4.8 models in 2026.

Why do XML tags matter for Claude?

XML tags are Claude's native way of understanding structure. According to Anthropic's documentation, using tags creates unambiguous boundaries. When you wrap content in tags, Claude knows exactly what is a rule, what is background context, and what is the data it needs to process.

Without clear delimiters, prompts fail. The model might mistake a piece of reference data for a new instruction. XML tags eliminate that risk entirely.

Here is a practical before and after example.

Without XML tags (vague prompt):

Summarize this customer ticket. Keep it under 50 words. Focus on the core complaint. The ticket says: My account has been locked for three days and nobody is replying to my emails...

With XML tags (structured prompt):

<instructions>
Summarize the customer ticket in the <input> tags.
Focus on the core complaint. Keep the summary under 50 words.
</instructions>

<context>
This summary will be routed to the priority support queue.
</context>

<input>
My account has been locked for three days and nobody is replying to my emails...
</input>

<output_format>
Return only the 50-word summary. Do not include pleasantries.
</output_format>

The second version leaves no room for confusion. Claude knows where the rules end and the data begins.

How do you use system prompts properly?

The system prompt is the first text Claude processes. It sets the baseline behavior for the entire session.

A good system prompt for Claude does three specific things. First, it sets a clear role (like "You are a senior frontend engineer"). Second, it defines hard constraints (like "Never use class components"). Third, it sets a default output format.

Anthropic recommends keeping stable instructions in the system prompt and putting the dynamic task in the user message. This separation is required if you want to use prompt caching later.

What is extended thinking?

Extended thinking gives Claude a hidden scratchpad to reason through a problem before it generates a final answer. Anthropic introduced this to help models tackle complex logic without hallucinating.

In modern Claude models, this is often adaptive. The model decides how much compute a prompt needs. You enable it when the task requires heavy planning, complex math, or multi-step reasoning. But you should turn it off for simple factual queries or basic summarization. You pay for thinking tokens, so there is no reason to burn them on easy tasks.

How does prompt caching save money?

Prompt caching lets Claude reuse previously processed context. If the beginning of your API request matches a previous request, Claude skips the expensive encoding step.

Cached tokens cost significantly less than standard input tokens. To make this work, you must structure your prompt correctly.

  • Put static content first. Your system prompt and large reference documents go at the very top.
  • Put dynamic content last. The specific user question goes at the end.
  • Do not change the prefix. Even a single changed character (like a timestamp) breaks the cache.

Is prefilling still relevant?

Prefilling was a popular trick where developers forced Claude to start a response with a specific string (like a JSON bracket). It prevented the model from adding unwanted conversational filler.

Today, this is largely obsolete. Modern Claude APIs support native tool use and structured outputs that guarantee JSON formatting. You can just ask for JSON directly in your <output_format> block.

How does Claude differ from ChatGPT and Gemini?

Every AI model has its own dialect. Copying a prompt from ChatGPT to Claude will get you a mediocre result.

  • Claude requires XML tags for structure. It thrives on explicit role assignment and strict, separated blocks of context.
  • ChatGPT prefers markdown formatting and conversational flow. It works well with rolling context and less rigid structures.
  • Gemini responds best to numbered steps and explicit grounding instructions that tell it where to pull data from.

If you want the best output, you must speak the model's native language.

How Kosmo handles this

I built Kosmo because tracking these prompting rules is exhausting. You have to remember XML tags for Claude, markdown for ChatGPT, and specific caching constraints for the Anthropic API.

Kosmo solves this automatically. You write your intent in plain English. You pick your target model. Then Kosmo compiles that intent into a perfectly structured, native prompt. When you target Claude, Kosmo generates the XML tags, separates the context, and isolates the system instructions for you.

And when Anthropic changes their best practices, we update the compiler. Your prompts stay optimized automatically.

Frequently asked questions

Do I need to use XML tags for every prompt?
No. Simple requests do not need tags. But if your prompt contains instructions, data, and formatting rules, XML tags will drastically improve Claude's reliability.

Can I use the exact same prompt for ChatGPT and Claude?
You can, but you should not. A prompt built for ChatGPT will not leverage Claude's structured parsing capabilities. You will get a generic response instead of an optimal one.

What happens if I forget to close an XML tag?
Claude will likely get confused. It might treat your data as an instruction or bleed the context into the output. Always verify that every tag is properly closed.

Claude prompthow to prompt ClaudeClaude XML tagsAnthropic Claude tipsprompt caching

Stop writing prompts by hand.

Kosmo compiles your plain-English request into a structured, tool-aware AI prompt. Try it free.

Get 8 Free Compiles