コンテンツにスキップ

Claude Cheat Sheet

Overview

Claude is Anthropic's AI assistant designed to be helpful, harmless, and honest. It excels at code generation, analysis, debugging, and technical writing. Claude supports multiple programming languages and can assist with complex software development tasks, architectural decisions, and code reviews.

⚠️ Note: Requires Anthropic account. Free tier available with usage limits. Professional and Team plans offer higher limits and additional features.

Access Methods

Web Interface

# Direct browser access
# Visit: https://claude.ai
# Sign up with email or Google account
# Start chatting immediately

API Integration

# Install Anthropic SDK
pip install anthropic

# Or using npm
npm install @anthropic-ai/sdk

# Set API key
export ANTHROPIC_API_KEY=your_api_key_here

Third-party Integrations

# Popular IDE extensions:
# - Claude for VS Code (community)
# - Claude Dev (VS Code extension)
# - Cline (formerly Claude Dev)

# Install Cline extension
code --install-extension saoudrizwan.claude-dev

API Usage

Python SDK

import anthropic

# Initialize client
client = anthropic.Anthropic(
    api_key="your_api_key_here"
)

# Basic message
message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1000,
    temperature=0,
    messages=[
        {
            "role": "user",
            "content": "Write a Python function to calculate fibonacci numbers"
        }
    ]
)

print(message.content)

Node.js SDK

import Anthropic from '@anthropic-ai/sdk';

const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

const msg = await anthropic.messages.create({
  model: "claude-3-5-sonnet-20241022",
  max_tokens: 1000,
  temperature: 0,
  messages: [
    {
      "role": "user",
      "content": "Create a React component for a todo list"
    }
  ]
});

console.log(msg.content);

cURL Examples

# Basic API call
curl https://api.anthropic.com/v1/messages \
  --header "x-api-key: $ANTHROPIC_API_KEY" \
  --header "content-type: application/json" \
  --header "anthropic-version: 2023-06-01" \
  --data '{
    "model": "claude-3-5-sonnet-20241022",
    "max_tokens": 1000,
    "messages": [
      {
        "role": "user",
        "content": "Explain async/await in JavaScript"
      }
    ]
  }'

Model Versions

Claude 3.5 Sonnet

# Model: claude-3-5-sonnet-20241022
# Best overall performance
# Excellent for coding tasks
# Strong reasoning capabilities
# 200K context window

Claude 3 Opus

# Model: claude-3-opus-20240229
# Most capable model
# Best for complex reasoning
# Highest accuracy
# 200K context window
# Higher cost per token

Claude 3 Haiku

# Model: claude-3-haiku-20240307
# Fastest and most affordable
# Good for simple tasks
# Quick responses
# 200K context window
# Lower cost per token

Code Generation

Function Creation

# Prompt: "Create a Python function to validate email addresses with regex"
import re

def validate_email(email):
    """
    Validate email address using regex pattern.

    Args:
        email (str): Email address to validate

    Returns:
        bool: True if valid email, False otherwise
    """
    pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    return re.match(pattern, email) is not None

# Example usage
print(validate_email("user@example.com"))  # True
print(validate_email("invalid-email"))     # False

Class Implementation

// Prompt: "Create a JavaScript class for managing a shopping cart"
class ShoppingCart {
    constructor() {
        this.items = [];
        this.total = 0;
    }

    addItem(product, quantity = 1) {
        const existingItem = this.items.find(item => item.product.id === product.id);

        if (existingItem) {
            existingItem.quantity += quantity;
        } else {
            this.items.push({ product, quantity });
        }

        this.updateTotal();
    }

    removeItem(productId) {
        this.items = this.items.filter(item => item.product.id !== productId);
        this.updateTotal();
    }

    updateTotal() {
        this.total = this.items.reduce((sum, item) => {
            return sum + (item.product.price * item.quantity);
        }, 0);
    }

    getItemCount() {
        return this.items.reduce((count, item) => count + item.quantity, 0);
    }
}

API Endpoint Development

# Prompt: "Create a FastAPI endpoint for user authentication"
from fastapi import FastAPI, HTTPException, Depends
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel
import jwt
import bcrypt
from datetime import datetime, timedelta

app = FastAPI()
security = HTTPBearer()

class UserLogin(BaseModel):
    username: str
    password: str

class UserResponse(BaseModel):
    username: str
    token: str

@app.post("/auth/login", response_model=UserResponse)
async def login(user_data: UserLogin):
    # Verify user credentials (implement your user verification logic)
    if not verify_user(user_data.username, user_data.password):
        raise HTTPException(status_code=401, detail="Invalid credentials")

    # Generate JWT token
    token_data = {
        "username": user_data.username,
        "exp": datetime.utcnow() + timedelta(hours=24)
    }
    token = jwt.encode(token_data, "your-secret-key", algorithm="HS256")

    return UserResponse(username=user_data.username, token=token)

def verify_user(username: str, password: str) -> bool:
    # Implement your user verification logic here
    # This is a placeholder implementation
    return True

Code Analysis & Review

Code Explanation

# Prompt: "Explain this code and suggest improvements"
# Claude analyzes code structure, logic, and provides detailed explanations
# Identifies potential issues and optimization opportunities
# Suggests best practices and alternative approaches

Security Review

# Prompt: "Review this code for security vulnerabilities"
# Claude identifies:
# - SQL injection risks
# - XSS vulnerabilities
# - Authentication issues
# - Input validation problems
# - Cryptographic weaknesses
# - Access control flaws

Performance Analysis

# Prompt: "Analyze this code for performance bottlenecks"
# Claude examines:
# - Algorithm complexity
# - Memory usage patterns
# - Database query efficiency
# - Caching opportunities
# - Concurrent processing potential

Debugging Assistance

Error Diagnosis

# Share error message and code context
# Claude provides:
# - Root cause analysis
# - Step-by-step debugging approach
# - Multiple solution options
# - Prevention strategies

# Example prompt:
"""
I'm getting this error: "TypeError: 'NoneType' object is not subscriptable"
Here's my code:
[paste your code here]
Can you help me debug this?
"""

Testing Strategy

// Prompt: "Create comprehensive tests for this function"
// Claude generates:
// - Unit tests
// - Integration tests
// - Edge case scenarios
// - Mock implementations
// - Test data fixtures

describe('ShoppingCart', () => {
    let cart;

    beforeEach(() => {
        cart = new ShoppingCart();
    });

    test('should initialize with empty items and zero total', () => {
        expect(cart.items).toEqual([]);
        expect(cart.total).toBe(0);
    });

    test('should add new item to cart', () => {
        const product = { id: 1, name: 'Test Product', price: 10.99 };
        cart.addItem(product, 2);

        expect(cart.items).toHaveLength(1);
        expect(cart.items[0].quantity).toBe(2);
        expect(cart.total).toBe(21.98);
    });
});

Advanced Features

Multi-turn Conversations

# Maintain context across multiple interactions
# Claude remembers previous code discussions
# Can iterate on solutions
# Builds upon previous suggestions

# Example conversation flow:
# 1. "Create a user management system"
# 2. "Add password hashing to the previous code"
# 3. "Now add email verification functionality"
# 4. "Include rate limiting for login attempts"

Code Refactoring

# Prompt: "Refactor this code to use modern Python patterns"
# Claude transforms:
# - Legacy code to modern syntax
# - Procedural to object-oriented
# - Synchronous to asynchronous
# - Monolithic to modular design

Architecture Design

# Prompt: "Design a microservices architecture for an e-commerce platform"
# Claude provides:
# - Service decomposition strategy
# - API design patterns
# - Database architecture
# - Communication protocols
# - Deployment considerations

IDE Integration

VS Code with Cline

# Install Cline extension
code --install-extension saoudrizwan.claude-dev

# Features:
# - Inline code generation
# - File editing capabilities
# - Terminal command execution
# - Multi-file project understanding
# - Automated testing

Configuration

// VS Code settings for Claude integration
{
    "claude-dev.apiKey": "your_api_key",
    "claude-dev.model": "claude-3-5-sonnet-20241022",
    "claude-dev.maxTokens": 4000,
    "claude-dev.temperature": 0.1
}

Keyboard Shortcuts

# Common shortcuts in Claude-enabled editors:
Ctrl+Shift+P    # Open command palette
Ctrl+K Ctrl+I   # Inline code generation
Ctrl+K Ctrl+C   # Code explanation
Ctrl+K Ctrl+T   # Generate tests
Ctrl+K Ctrl+R   # Refactor code

Best Practices

Effective Prompting

# Be specific and provide context
# Good: "Create a Python function that validates email addresses using regex, handles edge cases, and returns detailed error messages"
# Bad: "Make email validator"

# Include relevant details:
# - Programming language
# - Framework/library preferences
# - Performance requirements
# - Error handling needs
# - Testing requirements

Code Quality

# Always review generated code:
# 1. Test functionality thoroughly
# 2. Check for security vulnerabilities
# 3. Verify error handling
# 4. Ensure code style consistency
# 5. Validate performance implications

Iterative Development

# Use Claude for iterative improvement:
# 1. Start with basic implementation
# 2. Add error handling
# 3. Improve performance
# 4. Add comprehensive tests
# 5. Refactor for maintainability

Pricing & Limits

Free Tier

# Claude.ai web interface:
# - Limited messages per day
# - Access to Claude 3.5 Sonnet
# - Basic conversation features
# - No API access

Pro Plan ($20/month)

# Enhanced features:
# - 5x more usage
# - Priority bandwidth
# - Early access to new features
# - Create Projects with Claude

API Pricing

# Claude 3.5 Sonnet:
# Input: $3 per million tokens
# Output: $15 per million tokens

# Claude 3 Opus:
# Input: $15 per million tokens
# Output: $75 per million tokens

# Claude 3 Haiku:
# Input: $0.25 per million tokens
# Output: $1.25 per million tokens

Troubleshooting

Common Issues

# API rate limits:
# - Implement exponential backoff
# - Monitor usage quotas
# - Use appropriate model for task

# Context window limits:
# - Break large requests into chunks
# - Summarize previous context
# - Use conversation memory efficiently

Error Handling

# Robust API error handling
import anthropic
from anthropic import APIError, RateLimitError

try:
    response = client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=1000,
        messages=[{"role": "user", "content": prompt}]
    )
except RateLimitError:
    print("Rate limit exceeded. Please wait and try again.")
except APIError as e:
    print(f"API error occurred: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Performance Optimization

# Optimize API usage:
# - Use appropriate model for task complexity
# - Set reasonable max_tokens limits
# - Implement caching for repeated queries
# - Batch similar requests when possible

Resources

Documentation

Community

Tools & Extensions