Claude Skills Guide

Claude Code for Few-Shot Prompting Best Practices Workflow

Few-shot prompting is one of the most powerful techniques for getting high-quality, consistent outputs from Claude Code. By providing carefully chosen examples within your prompts, you teach the model exactly what format, tone, and approach you expect. This guide walks you through a practical workflow for implementing few-shot prompting with Claude Code, complete with real examples and actionable advice you can apply immediately to your projects.

Understanding Few-Shot Prompting Fundamentals

Few-shot prompting works by showing Claude Code a small number of input-output examples (typically 2-5) that demonstrate your desired behavior. The model learns from these examples and applies the same pattern to new inputs. This approach dramatically outperforms zero-shot prompting (no examples) for tasks requiring specific formats, specialized terminology, or consistent decision-making patterns.

The key insight is that examples act as implicit instructions. Rather than explaining every detail of what you want, you show it. This is particularly valuable when working with Claude Code because it reduces the need for lengthy, detailed prompts while producing more predictable results.

Consider the difference between these two approaches for generating commit messages:

Zero-shot approach (less effective):

Write a commit message for this git diff. Make it follow conventional commits format.

Few-shot approach (more effective):

Write commit messages for these git diffs following conventional commits format:

feat: add user authentication flow
- Added login/logout functionality
- Implemented JWT token handling
- Created auth middleware

feat: implement API rate limiting
- Added rate limit middleware
- Configured Redis for distributed rate limiting
- Added rate limit headers to responses

{{NEW_DIFF_HERE}}

The second example demonstrates both format and content expectations. Claude Code immediately understands you want conventional commits with type, scope, and description—without explicitly stating those requirements.

Building Your Few-Shot Prompting Workflow

A robust few-shot prompting workflow with Claude Code involves four stages: task analysis, example selection, prompt construction, and iteration. Let’s walk through each stage with practical guidance.

Stage 1: Task Analysis

Before writing any prompt, identify what makes your task challenging. Ask yourself three questions:

For instance, if you’re prompting Claude Code to review pull requests, your analysis might reveal: you need structured JSON output with severity ratings, the model must identify security issues and code smells, and good responses include specific line numbers and remediation suggestions.

Stage 2: Example Selection

Choose 3-5 representative examples that demonstrate the full range of your task’s complexity. Quality matters more than quantity. Each example should:

Avoid examples that are too simple or perfectly straightforward. Real-world usage will present complications, so your examples should prepare Claude Code to handle them.

Stage 3: Prompt Construction

Structure your prompt with clear sections:

[Task description - what you want]
[Input format explanation]
[Output format explanation]
[Examples - 3-5 input/output pairs]
[Your actual input]

This structure keeps instructions separate from examples, making it easy to update either component independently.

Stage 4: Iteration and Refinement

Test your prompt with real inputs, not just hypothetical ones. Track which inputs produce unexpected outputs and adjust your examples accordingly. Few-shot prompting is an iterative process—your first version will rarely be perfect.

Practical Code Examples for Common Tasks

Example 1: Generating Database Schemas

Here’s a few-shot prompt for generating SQLAlchemy models:

Generate SQLAlchemy ORM models for the following database schemas.
Use declarative base, proper relationships, and appropriate column types.

Example 1:
Table: users
- id (integer, primary key)
- email (string, unique, not null)
- username (string, not null)
- password_hash (string, not null)
- created_at (datetime, default now)

Output:
```python
from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base

class User(Base):
    __tablename__ = 'users'
    
    id = Column(Integer, primary_key=True)
    email = Column(String, unique=True, nullable=False)
    username = Column(String, nullable=False)
    password_hash = Column(String, nullable=False)
    created_at = Column(DateTime, server_default=func.now())
    
    posts = relationship("Post", back_populates="author")

Example 2: Table: posts

Now generate models for: Table: comments

Example 2: Creating API Response Validators

Few-shot prompting excels at generating consistent validation code:

Create Pydantic models for API response validation.
Include proper type hints, validation decorators, and error messages.

Example Input:
Response: {"status": "success", "data": {"user_id": 123, "name": "John"}, "timestamp": "2024-01-15T10:30:00Z"}

Output:
```python
from pydantic import BaseModel, Field, field_validator
from typing import Optional
from datetime import datetime

class UserData(BaseModel):
    user_id: int = Field(gt=0)
    name: str = Field(min_length=1, max_length=100)
    
    @field_validator('name')
    @classmethod
    def name_not_empty(cls, v: str) -> str:
        if not v.strip():
            raise ValueError('Name cannot be empty')
        return v

class ApiResponse(BaseModel):
    status: str = Field(pattern='^(success|error)$')
    data: Optional[UserData] = None
    timestamp: datetime
    
    @field_validator('timestamp', mode='before')
    @classmethod
    def parse_timestamp(cls, v):
        if isinstance(v, str):
            return datetime.fromisoformat(v.replace('Z', '+00:00'))
        return v

Now create validators for this response: {“order_id”: “ORD-456”, “items”: [{“product_id”: 1, “quantity”: 2, “price”: 29.99}], “total”: 59.98, “status”: “pending”}


## Advanced Techniques for Better Results

### Variable Placeholders

Use consistent placeholder patterns in your examples so Claude Code recognizes where to apply the pattern:

- `{{INPUT}}` or `{{YOUR_INPUT_HERE}}` marks where your actual input goes
- `...` indicates omitted content in longer examples
- `[YOUR_CONTEXT]` marks optional context variables

### Chain-of-Thought Examples

For complex reasoning tasks, include examples that show the thinking process:

Solve the following debugging problem by explaining your reasoning step by step.

Example: Input: “Users report that file uploads fail for files larger than 5MB” Thought: First, I need to identify where file size limits could be set. Common locations include the frontend, backend, server configuration, and database. Let me check each systematically. Action: Examine the frontend upload component for size validation. Finding: Frontend shows no limit, but uploads hang at exactly 5MB. Thought: The hang suggests a server-side timeout or limit. Let me check nginx and application settings. Conclusion: Nginx client_max_body_size is set to 5M, causing the hang.

Now solve: Input: “API returns 504 errors intermittently under high load”


### Negative Examples

Sometimes showing what you don't want is as valuable as showing what you do want:

Write clean, well-documented Python functions. Avoid these patterns:

Bad example:

def get_data(d):
    return [x for x in d if x['active']==True]

Good example:

def get_active_users(users: list[dict]) -> list[dict]:
    """Filter and return only active users from the provided list.
    
    Args:
        users: List of user dictionaries containing 'active' key
        
    Returns:
        List of user dictionaries where 'active' is True
    """
    return [user for user in users if user.get('active', False)]

Now write a function that processes order data and calculates totals.


## Common Pitfalls to Avoid

**Too many examples:** More than 5 examples rarely improves performance and increases token usage. Quality trumps quantity.

**Inconsistent examples:** If your examples contradict each other in format or approach, Claude Code will produce inconsistent results. Keep all examples aligned.

**Vague examples:** Each example should clearly demonstrate your expectations. Ambiguous examples lead to ambiguous outputs.

**Ignoring edge cases:** Include at least one example showing how to handle unusual or difficult inputs. This prepares Claude Code for real-world complexity.

**Not testing with real data:** Always validate your prompts with actual inputs, not just hypothetical scenarios. What works in theory often fails in practice.

## Integrating Few-Shot Prompts with Claude Code Skills

For maximum effectiveness, embed your few-shot prompts within Claude Code skills. This creates reusable, version-controlled prompt templates that your entire team can use:

.claude/skills/review-skill.yaml

name: “Code Review” description: “Perform thorough code reviews using few-shot learning” prompts:

review-prompt.md

Review the following code diff using these examples as reference: [3-5 example reviews with specific patterns] ```

This approach combines the consistency of few-shot prompting with Claude Code’s skill-based workflow system, giving you reproducible results across your entire development process.

Start implementing few-shot prompting in your Claude Code workflows today—the improvements in output quality and consistency will be immediately noticeable.