19  Effective Prompting

How you communicate with Claude Code significantly affects the quality of results. This chapter covers prompting strategies.

19.1 The Basics of Good Prompts

19.1.1 Be Specific

Vague:

> Help me with my data

Specific:

> Read the CSV file data/expression.csv and show me the first 5 rows,
> column names, and data types

19.1.2 Provide Context

Without context:

> Fix the error

With context:

> I'm running main.py and getting a KeyError on line 45.
> Here's the error message: [paste error]
> The code is trying to access a dictionary with sample IDs.

19.1.3 State the Goal

Task-focused:

> Add a try-except block

Goal-focused:

> This script crashes when the input file is missing.
> Make it handle that case gracefully with a helpful error message.

19.2 Prompt Patterns

19.2.1 The Explore-Then-Act Pattern

First understand, then modify:

> Explain what the process_data function does

(Claude explains)

> Good. Now modify it to also handle missing values

19.2.2 The Step-by-Step Pattern

For complex tasks:

> I want to build an analysis pipeline. Let's break this into steps:
> 1. First, let's load and inspect the data

Complete step 1, then:

> Good. Step 2: Clean and preprocess

19.2.3 The Show-Me-Options Pattern

When you’re unsure of the approach:

> What are the different ways to handle outliers in this dataset?
> Explain the trade-offs of each.

19.2.4 The Rubber Duck Pattern

Think out loud:

> I'm trying to figure out why the correlation is negative.
> Walk me through what could cause this given the data we have.

19.3 Refining Results

19.3.1 When Output Isn’t Quite Right

Don’t start over—refine:

> That's close, but the plot needs a legend
> Change the bar colors to blue instead of the default
> Actually, make it a horizontal bar chart instead

19.3.2 Being More Specific About Changes

Vague refinement:

> Make it better

Specific refinement:

> The code works but is hard to read. Specifically:
> - Break the long function into smaller functions
> - Add docstrings
> - Use more descriptive variable names

19.3.3 Providing Examples

> The output format isn't quite right. I want it like this:
> Gene: BRCA1
> Expression: 5.2
> Not like this:
> BRCA1, 5.2

19.4 Domain-Specific Prompting

19.4.1 For Code

> Write a Python function that [description]
> - Input: [describe inputs]
> - Output: [describe outputs]
> - Handle edge cases: [list them]

19.4.2 For Analysis

> Analyze this data to answer: [specific question]
> Use appropriate statistical methods
> Generate visualizations that support the conclusions

19.4.3 For Writing

> Write a methods paragraph about [topic]
> - Tone: formal academic
> - Length: 150-200 words
> - Include: specific details about [X]

19.5 What to Include in Prompts

19.5.1 Always Helpful

  • What you want to achieve
  • Relevant file names/paths
  • Error messages (if debugging)
  • Constraints or requirements

19.5.2 Sometimes Helpful

  • Background context (if unusual)
  • Examples of desired output
  • Prior attempts that didn’t work

19.5.3 Usually Not Needed

  • General explanations Claude already knows
  • Excessive politeness (Claude responds the same either way)
  • Long preambles before the actual request

19.6 Common Mistakes

19.6.1 Too Many Things at Once

Overloaded:

> Load the data, clean it, run the analysis, create figures, and write a summary

Better:

> Let's start by loading and exploring the data

Then proceed step by step.

19.6.2 Assumed Knowledge

Assumes Claude knows your setup:

> Run the test

Provides context:

> Run pytest on the tests/ directory

19.6.3 Vague Feedback

Unclear:

> That's wrong

Helpful:

> The function returns a list but I need a dictionary with gene names as keys

19.7 Advanced Techniques

19.7.1 Asking Claude to Plan

> Before writing any code, outline how you would approach this problem

This helps catch issues before implementation.

19.7.2 Requesting Explanations

> After making the changes, explain why you chose that approach

Helps you learn and verify the reasoning.

19.7.3 Setting Constraints

> Solve this but:
> - Don't use any external libraries
> - Keep it under 50 lines
> - Make it work with Python 3.8+

19.7.4 Asking for Alternatives

> Show me two different ways to implement this
> with pros and cons of each

19.8 Iteration is Normal

Nobody gets perfect results on the first try:

  1. First prompt: Get something working
  2. Refinement: Fix obvious issues
  3. Polish: Improve style, efficiency, readability
  4. Verify: Test and validate

Each step uses the conversation history—Claude remembers what came before.

19.9 Practice Exercises

Try these prompts to practice:

  1. Vague → Specific: Take “help me with my code” and rewrite it to be specific

  2. Add Context: You have an error. Write a prompt that gives Claude everything needed to help

  3. Step by Step: You want to create a data pipeline. Write prompts for a step-by-step approach

19.10 Summary

Good prompts are: - Specific: Say exactly what you want - Contextual: Provide relevant information - Goal-oriented: Explain why, not just what - Iterative: Build up through refinement

19.11 Next Steps

Continue to Safety and Review.