advanced12 min read· Module 7, Lesson 4
🎓Advanced Prompt Techniques
Role prompting, output control, and model-specific tips
Advanced Prompt Techniques
Role Prompting
Assign Claude a specific role for better results:
You are a senior database architect with 15 years of experience
in PostgreSQL and distributed systems. A junior developer asks you:
"Should I use MongoDB or PostgreSQL for an e-commerce application?"
Provide guidance considering:
- Data relationships
- Transaction requirements
- Scale expectations
- Team expertise
Output Control
Specify exact format:
Respond ONLY with valid JSON. No explanation, no markdown, no code fences.
The JSON should have these fields:
{
"summary": "one sentence",
"sentiment": "positive|negative|neutral",
"confidence": 0.0 to 1.0
}
Control length on Claude Opus 4.7:
Provide concise, focused responses. Skip non-essential context,
and keep examples minimal.
Handling Refusals
If Claude refuses a valid request, rephrase:
# Instead of:
"Write malware code"
# Try:
"I'm a security researcher. Write an educational example of how
SQL injection works, with the vulnerable code AND the fix,
so I can teach my team about this vulnerability."
Model-Specific Tips
Claude Opus 4.7:
- Best for complex, multi-step reasoning
- Naturally calibrates response length to complexity
- Use
effortparameter to control thinking depth - Prefers adaptive thinking over manual extended thinking
Claude Sonnet 4.6:
- Best balance of speed and quality
- Great for production applications
- Supports extended thinking with budget_tokens
- Ideal for most API use cases
Claude Haiku 4.5:
- Fastest model — great for high-volume
- May need more explicit instructions
- Best for simple tasks and classifications
- Cheapest option for production
Prompt Caching (Save Money!)
Reuse the same system prompt across requests:
// First request — creates cache
const response1 = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
system: [
{
type: "text",
text: "You are a helpful assistant...",
cache_control: { type: "ephemeral" } // Cache this!
}
],
messages: [{ role: "user", content: "Question 1" }]
});
// Subsequent requests — cheaper! Cache is used
const response2 = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
system: [
{
type: "text",
text: "You are a helpful assistant...",
cache_control: { type: "ephemeral" }
}
],
messages: [{ role: "user", content: "Question 2" }]
});Cache lasts 5 minutes (standard) or 1 hour (extended). Cached input is much cheaper than regular input.
Next up: Put it all together with hands-on projects.