Concept #95Mediumextended-ai-concepts

How to increase accuracy while prompting AI?

#gen-ai#prompt-engineering

Answer

How to Increase Accuracy While Prompting AI

Accuracy in AI prompts depends on prompt structure, specificity, context quality, and the techniques you apply. Here are proven strategies.

1. Be Specific and Clear

text
āŒ Vague: "Explain machine learning"
āœ… Specific: "Explain supervised machine learning to a Python developer who knows
   statistics but has never worked with ML libraries. Use a concrete classification
   example and include scikit-learn code."

2. Provide Context

python
# Include relevant context the model needs
prompt = '''
Context: I'm working on a FastAPI application using Python 3.11, PostgreSQL 15,
and SQLAlchemy 2.0. The app handles 10K requests/day.

Task: Write an efficient database connection pool configuration.

Requirements:
- Use async SQLAlchemy
- Pool size: 5-10 connections
- Handle connection timeouts gracefully
'''

3. Chain-of-Thought Prompting

Ask the model to reason step by step:

python
prompt = '''
Problem: A customer's order shows 'shipped' but they haven't received it after
10 days. The tracking shows it arrived at a regional warehouse 8 days ago.

Think through this carefully:
1. What could cause this delay?
2. What information do we need to investigate?
3. What should we tell the customer?

Work through each step before giving your final recommendation.
'''

4. Few-Shot Examples

Show the model exactly what format/quality you expect:

python
prompt = '''
Convert user feedback to structured JSON.

Example 1:
Input: "The app crashes when I upload images larger than 5MB"
Output: {"type": "bug", "severity": "high", "component": "image_upload", "description": "crash on upload >5MB"}

Example 2:
Input: "Would love dark mode support"
Output: {"type": "feature_request", "priority": "medium", "component": "ui", "description": "dark mode"}

Now convert:
Input: "Login sometimes fails on mobile but works on desktop"
Output:'''

5. Role Assignment

python
system_prompt = '''You are a senior security engineer with 10 years of experience
in web application security. You specialize in identifying OWASP Top 10 vulnerabilities.
When reviewing code, you:
- Point out specific line numbers
- Explain the vulnerability clearly
- Provide a concrete fix
- Rate severity as Critical/High/Medium/Low'''

6. Structured Output Format

python
prompt = '''Analyze this API response and return ONLY a JSON object with this structure:
{
  "status": "success|failure|partial",
  "issues": [{"field": "...", "error": "...", "severity": "..."}],
  "recommendation": "..."
}

API Response to analyze:
{response_to_analyze}'''

7. Verification and Self-Checking

python
prompt = '''
Write a Python function to calculate compound interest.

After writing the function:
1. Trace through it with: principal=1000, rate=0.05, years=3
2. Verify your answer equals $1157.63
3. If it doesn't match, find and fix the error'''

Accuracy Techniques Summary

TechniqueWhen to UseImpact
SpecificityAlwaysHigh
ContextDomain-specific tasksHigh
Chain-of-thoughtMath, logic, debuggingHigh
Few-shot examplesFormat/style requirementsVery High
Role assignmentTechnical domainsMedium-High
Structured outputData extraction, APIsHigh
Self-verificationCritical computationsMedium
Temperature=0Deterministic tasksMedium

Temperature Setting

python
# For factual, deterministic tasks — use low temperature
response = client.messages.create(
    model="claude-opus-4-6",
    max_tokens=1024,
    temperature=0,  # Most deterministic
    messages=[{"role": "user", "content": "What is 2^10?"}]
)

# For creative tasks — use higher temperature
response = client.messages.create(
    model="claude-opus-4-6",
    max_tokens=1024,
    temperature=0.9,  # More creative
    messages=[{"role": "user", "content": "Write a creative story opening"}]
)