Intermediate

Advanced Prompting Techniques

Take your prompting to the next level with chain-of-thought reasoning, system prompts, XML tags, prompt chaining, and parameter controls.

Chain of Thought Reasoning

Chain of thought (CoT) prompting asks Claude to show its reasoning step by step before giving a final answer. This dramatically improves accuracy on complex problems like math, logic, and analysis.

Without Chain of Thought
User:
A store sells apples for $2 each. If you buy 5 or
more, you get a 20% discount. How much do 7 apples
cost?

Claude (might rush to answer):
$11.20
With Chain of Thought
User:
A store sells apples for $2 each. If you buy 5 or
more, you get a 20% discount. How much do 7 apples
cost?

Think through this step by step.

Claude:
Let me work through this step by step:

1. Price per apple: $2.00
2. Number of apples: 7
3. Full price: 7 x $2.00 = $14.00
4. Discount applies? 7 >= 5, so yes, 20% off
5. Discount amount: $14.00 x 0.20 = $2.80
6. Final price: $14.00 - $2.80 = $11.20

The 7 apples cost $11.20.

Phrases that trigger chain-of-thought reasoning:

  • "Think through this step by step"
  • "Show your reasoning"
  • "Walk me through your thought process"
  • "Before answering, consider the problem carefully"
  • "Explain your reasoning before giving the final answer"
When to use CoT: Chain of thought is most beneficial for math problems, logical reasoning, code debugging, complex analysis, and any task where accuracy matters more than speed. For simple factual questions, it is unnecessary.

Role Prompting / System Prompts

A system prompt sets Claude's behavior and persona for the entire conversation. It goes before the user's first message and acts as standing instructions.

System Prompt Example
System prompt:
You are a senior Python developer with 15 years of
experience. You write clean, well-documented code
following PEP 8 conventions. When reviewing code,
you focus on:
- Performance optimization
- Security best practices
- Readability and maintainability
- Type hints and documentation

Always explain your reasoning. If multiple approaches
exist, present the trade-offs.

User:
Review this function:

def get_users(db, active=True):
    q = "SELECT * FROM users WHERE active=" + str(active)
    return db.execute(q)

With that system prompt, Claude would immediately flag the SQL injection vulnerability, suggest parameterized queries, recommend type hints, and offer a production-quality rewrite.

Effective System Prompt Patterns

Technical Writer System Prompt
You are a technical writer creating documentation
for a developer audience. Your writing style:
- Clear and concise, no unnecessary jargon
- Use active voice
- Include code examples for every concept
- Structure content with headers and bullet points
- Assume the reader knows basic programming
  but not the specific technology being documented
Data Analyst System Prompt
You are a data analyst. When given data:
1. Start with a brief summary of what the data shows
2. Identify key patterns, trends, and outliers
3. Provide specific numbers and percentages
4. Suggest visualizations that would be helpful
5. End with actionable recommendations

Always caveat statistical claims appropriately and
note when sample sizes are too small for conclusions.

Multi-Turn Conversations

Claude maintains context across messages in a conversation. You can build on previous exchanges to refine results, explore topics deeper, or iterate on a task.

Multi-Turn Refinement
Turn 1 - Initial request:
Write a landing page headline for a project
management tool aimed at small teams.

Claude:
"Manage Projects, Not Chaos. Built for Small Teams."

Turn 2 - Refine:
Good start. Make it shorter and more punchy.
Emphasize simplicity over features.

Claude:
"Simple Projects. Happy Teams."

Turn 3 - Explore alternatives:
Give me 5 variations. Mix serious and playful tones.

Claude:
1. "Simple Projects. Happy Teams."
2. "Finally, project management that doesn't suck."
3. "Your team's work, organized."
4. "Less process. More progress."
5. "Ditch the spreadsheets. Ship the work."
💡
Context management: Each message in a conversation adds to the context window. In very long conversations, earlier messages may get pushed out. For important context, repeat key instructions periodically or start a new conversation.

XML Tags for Structure

Claude responds particularly well to XML-style tags in prompts. XML tags clearly delineate different parts of your prompt, making it easy for Claude to understand the structure.

XML Tags in Prompts
Analyze the following document and provide a
summary, key takeaways, and action items.

<document>
Q3 2024 revenue increased 23% YoY to $4.2M.
Customer acquisition cost decreased from $150
to $120. Churn rate remains at 5.2%, above our
target of 4%. The engineering team shipped 3
major features: real-time collaboration, API
v2, and mobile app redesign. Customer NPS
improved from 42 to 58.
</document>

Format your response as:
<summary>2-3 sentence overview</summary>
<takeaways>bullet list</takeaways>
<actions>prioritized next steps</actions>

Common XML tag patterns used with Claude:

Useful XML Tag Patterns
# Separating input data from instructions
<instructions>What to do</instructions>
<data>The data to process</data>

# Multiple documents
<doc1>First document</doc1>
<doc2>Second document</doc2>

# Examples section
<examples>
  <example>
    <input>example input</input>
    <output>expected output</output>
  </example>
</examples>

# Constraining output format
<answer>Claude puts answer here</answer>

Handling Long Documents

Claude's 200K token context window can handle long documents, but how you structure your prompt matters for quality.

Long Document Pattern
I'm going to give you a long document. Please read
the entire document first, then answer my questions.

<document>
[paste your full document here - up to ~150K tokens]
</document>

Questions:
1. What are the main arguments presented?
2. What evidence supports each argument?
3. Are there any logical gaps or weaknesses?
4. Summarize the document in 200 words.
Best practice: Put the document before your questions. Claude processes text sequentially, so having the full document in context before it encounters the questions leads to better, more comprehensive answers.

Prompt Chaining

Prompt chaining breaks a complex task into a series of simpler prompts, where the output of one becomes the input of the next. This produces higher quality results than trying to do everything in one prompt.

Prompt Chain Example
# Step 1: Research/Analysis
Prompt 1:
"Analyze this customer feedback data and identify
the top 5 pain points, ranked by frequency."

# Step 2: Strategy (uses Step 1 output)
Prompt 2:
"Based on these 5 pain points:
[paste Step 1 output]
Propose a solution for each one. Include effort
estimate (low/medium/high) and expected impact."

# Step 3: Action Plan (uses Step 2 output)
Prompt 3:
"Create a 90-day roadmap to implement these
solutions: [paste Step 2 output]
Prioritize by impact-to-effort ratio.
Format as a week-by-week timeline."

Temperature and Other Parameters

When using the API, you can control Claude's behavior with several parameters:

Temperature

Temperature controls how random or deterministic Claude's responses are. It ranges from 0 to 1.

Temperature Behavior Best For
0.0 Most deterministic, consistent Classification, data extraction, math
0.3 Slightly varied, mostly focused Code generation, factual Q&A
0.7 Balanced creativity and coherence General conversation, writing
1.0 Most creative and varied Brainstorming, creative writing, poetry
Python - Temperature Example
import anthropic

client = anthropic.Anthropic()

# Low temperature for consistent code
response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    temperature=0.0,  # Deterministic
    messages=[{
        "role": "user",
        "content": "Write a SQL query to find duplicate emails"
    }]
)

# High temperature for creative writing
response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    temperature=1.0,  # Creative
    messages=[{
        "role": "user",
        "content": "Write a short poem about debugging code"
    }]
)

Max Tokens

Controls the maximum length of Claude's response. Set this to avoid overly long outputs or to enforce brevity.

Top-P (Nucleus Sampling)

An alternative to temperature. Controls the cumulative probability threshold for token selection. Usually you adjust either temperature or top-p, not both.

💡
Default recommendation: For most tasks, the default temperature (1.0 on the API) works well. Lower it for tasks requiring precision and consistency. The claude.ai web interface handles this automatically.