Lesson 5 of 21 ~15 min
Course progress
0%

Response Formatting

Learn how to get Claude to format responses exactly how you need them.

Response Formatting

Get Claude to structure responses in exactly the format you need—whether it’s code, documentation, data, or explanations.

Specifying Output Format

Be Explicit About Format

❌ Vague:

Explain how API authentication works.

✅ Specific:

Explain API authentication in this format:
1. Executive summary (2 sentences)
2. Step-by-step process (numbered list)
3. Code example (JavaScript)
4. Security considerations (bullet points)

Format Types

1. Structured Text

Create a project README with these sections:
# Project Title
## Description
## Installation
## Usage
## API Reference
## Contributing
## License

2. Code with Comments

Write a Python function to validate email addresses.
Format:
- Comprehensive docstring
- Inline comments explaining logic
- Type hints
- Example usage in comments

3. Data Structures

Design a database schema for a blog.
Return as:
- Table definitions (CREATE TABLE syntax)
- Relationships diagram (Mermaid)
- Sample data (INSERT statements)
- Indexes needed (CREATE INDEX)

4. Comparison Tables

Compare React, Vue, and Angular.
Format as markdown table with columns:
| Feature | React | Vue | Angular |

Common Format Patterns

Documentation Format

Document this API endpoint in this structure:

## Endpoint Name
**Method:** POST
**URL:** /api/...
**Description:** ...

**Request Body:**
```json
{
  "field": "type"
}

Response:

{
  "result": "type"  
}

Error Codes:

  • 400: …
  • 401: …

### Tutorial Format

Create a tutorial for [topic] following this template:

Introduction

What we’ll build and why

Prerequisites

What you need to know

Step 1: Setup

Installation and configuration

Step 2-N: Implementation

Building the feature step by step

Conclusion

What we learned and next steps


### Decision Matrix

Help me choose between options A, B, C.

Format as decision matrix: | Criteria | Weight | A Score | B Score | C Score | | Cost | 0.3 | 8/10 | … | | Performance | 0.4 | … | | Total Weighted Score: …

Recommendation: [choice] because [reasoning]


## Code Formatting

### Full File Structure

Create a complete Express server file with: // Dependencies at top // Configuration section
// Middleware setup // Route handlers // Error handling // Server start // Export for testing


### Minimal Example

Show me just the core logic for JWT verification. No imports, no error handling, just the essential code.


### Production-Ready

Write production-ready code with:

  • Error handling
  • Input validation
  • Logging
  • Type safety
  • Unit tests
  • Documentation

## Response Length Control

### Concise Responses

Explain [concept] in exactly 3 bullet points. Each point maximum 20 words.


### Detailed Responses

Provide comprehensive explanation of [topic]:

  • Minimum 500 words
  • Include examples
  • Cover edge cases
  • List pros and cons

### Progressive Depth

Explain async/await in 3 levels:

  1. ELI5 (Explain Like I’m 5)
  2. For junior developer
  3. For senior developer

## Specialized Formats

### JSON Schema

Create a JSON schema for user registration. Include:

  • All field types
  • Validation rules
  • Required fields
  • Example valid object

### API Specification

Design REST API endpoints for a todo app. Format as OpenAPI 3.0 specification.


### Mermaid Diagrams

Show the authentication flow as: sequenceDiagram participant User participant Client participant Server participant Database


### Comparison Format

Compare approach A vs B:

Approach A

Pros:

  • Cons:
  • Use when:

Approach B

[same structure]

Recommendation

[based on context]


## Template Examples

### Bug Report Template

Analyze this bug and respond in bug report format:

Bug Description:Steps to Reproduce:

  1. Expected Behavior:Actual Behavior:Root Cause:Proposed Fix:Code Changes:

### Code Review Template

Review this code using:

Overview: One-line summary Strengths: What’s good Issues: Problems found (severity: high/medium/low) Suggestions: Improvements Security: Any concerns Performance: Optimization opportunities


### Learning Path Template

Create a learning path for [skill]:

Phase 1: Foundations (Week 1-2)

  • Topic 1
  • Topic 2
  • Practice project

Phase 2: Intermediate (Week 3-4)

Resources

  • Books: …
  • Courses: …
  • Practice: …

## Advanced Formatting

### Conditional Format

Explain [topic]. If the explanation requires code, use JavaScript. If it needs a diagram, use Mermaid. If it’s a process, use numbered steps.


### Multiple Outputs

For this function, provide:

  1. TypeScript implementation
  2. Unit tests (Jest)
  3. Usage example
  4. JSDoc comments
  5. Performance notes

Each in separate code blocks.


### Hybrid Format

Design a caching system with:

  • Overview (prose)
  • Architecture (Mermaid diagram)
  • Implementation (code)
  • Performance metrics (table)
  • Decision rationale (pros/cons list)

## Best Practices

### 1. Show Examples

Format your response like this example:

Input: “Hello World” Output: “HELLO WORLD” Explanation: Converts to uppercase

Now do the same for [my case]


### 2. Specify Boundaries

List exactly 5 best practices. No more, no less. Each with one code example.


### 3. Use Templates

Provide a specific template like this:

Use this exact template:

[Title]

Purpose: [one sentence] Use when: [scenario] Example: [code block here] Gotchas: [warnings]


## Practical Examples

### Example 1: Documentation

**Request:**
````text
Document this function in JSDoc format:
function calculateDiscount(price, percentage) {
  return price * (1 - percentage / 100);
}

Example 2: Explanation

Request:

Explain closures in JavaScript.
Format:
1. Definition (1 sentence)
2. Simple analogy
3. Code example with comments
4. Common use case
5. Gotcha to avoid

Example 3: Comparison

Request:

Compare SQL vs NoSQL databases.
Format as:
| Aspect | SQL | NoSQL |
With rows for: Structure, Scalability, Use Cases, Examples

Key Takeaways

  1. Be explicit about desired format
  2. Show examples of what you want
  3. Use templates for consistency
  4. Specify boundaries (length, sections)
  5. Request structure (headings, lists, code)
  6. Control depth (concise vs detailed)

What's the best way to get a specific format?

Hope Claude guesses right
Explicitly specify the format
Use lots of examples
Ask multiple times

How should you request code formatting?

Just ask for code
Specify language only
Specify language, style, comments needed
Let Claude decide

What's a template in this context?

A pre-made solution
A coding framework
A design pattern
A structural format to follow

How can you control response length?

Specify exact number of points/words
Can't control it
Ask for 'short' or 'long'
Set a time limit