Overview
Learn the Claude Code CLI by doing. This by-example tutorial teaches the claude command - the AI coding assistant you control from the terminal - through 85 heavily annotated, self-contained examples achieving 95% coverage. Master interactive usage, non-interactive automation (-p flag), npm scripts, git hooks, GitHub Actions, and custom agent patterns.
What is By Example?
By Example is a code-first learning approach designed for experienced developers who want to master Claude Code efficiently. Instead of lengthy explanations followed by examples, you'll see complete, runnable command sequences with inline annotations explaining what each interaction does and why it matters.
Target audience: Developers with command-line experience who want to integrate AI assistance into their workflow and understand Claude Code's full capabilities.
How This Tutorial Works
Structure
- Beginner (Examples 1-30):
claudecommand fundamentals, interactive mode, print mode (-p), npm scripts, git hooks - 0-40% coverage - Intermediate (Examples 31-60): GitHub Actions, CI/CD pipelines, multi-language subprocess integration (Python/Node/Java/Go) - 40-75% coverage
- Advanced (Examples 61-85): Custom agents (
--agents), MCP servers, production orchestration, configuration management - 75-95% coverage
Example Format
Each example follows a five-part structure:
- Brief explanation (2-3 sentences) - What is this pattern and why does it matter?
- Diagram (when appropriate) - Visual representation of workflow or interaction
- Heavily annotated commands/code - Complete, runnable examples with inline
# =>annotations - Key takeaway (1-2 sentences) - The essential insight distilled
- Why it matters (50-100 words) - Production relevance and real-world impact
Example: Annotation Style
# Start interactive session
claude # => Launches Claude Code CLI
# => Enters conversation mode
# => Ready to accept natural language prompts
# Ask for code generation
You: Create a Python function to validate email addresses
# => Claude Code analyzes request
# => Generates function with regex validation
# => Offers to explain or create tests
# Review generated code
# => Claude Code shows complete function
# => Includes imports, docstring, examples
# => Asks for confirmation before writing
# Accept and write file
You: Yes, write it to utils/validation.py
# => Creates utils/ directory if needed
# => Writes function to file
# => Updates project index
# => Confirms completionWhat You'll Learn
Coverage: 95% of Claude Code for Production Work
Included:
- Core commands (interactive mode, file operations, status checks)
- Conversation patterns (asking questions, refining requests, context management)
- Code generation (functions, classes, modules, tests)
- File operations (read, write, edit, search, refactor)
- Terminal integration (running commands, interpreting output)
- Workflow patterns (debugging, refactoring, documentation)
- Multi-file operations (codebase analysis, coordinated changes)
- Advanced prompting (context injection, style guides, constraints)
- Error handling (understanding failures, iteration strategies)
- Best practices (security, performance, maintainability)
Excluded (the 5% edge cases):
- Rarely-used configuration options
- Platform-specific advanced features
- Internal API implementation details
- Experimental or deprecated features
Self-Contained Examples
Every example is copy-paste-runnable. Each example includes:
- Complete command sequences
- All necessary context and setup
- Expected outputs and responses
- No references to previous examples (you can start anywhere)
Example independence: You can jump to Example 42, copy the commands, run them, and understand the pattern without reading Examples 1-41.
Why Command-First?
Traditional tutorials explain concepts, then show commands. By Example inverts this:
- See the commands first - Complete, working interaction
- Run it immediately - Verify it works in your environment
- Read annotations - Understand what each step does
- Absorb the pattern - Internalize through direct interaction
Benefits:
- Faster learning - No walls of text before seeing actual commands
- Immediate verification - Run commands to confirm understanding
- Reference-friendly - Come back later to find specific patterns
- Production-focused - Examples use real-world workflows, not toy scenarios
How to Use This Tutorial
If You're New to AI Coding Tools
Start with Quick Start first, then return to By Example for comprehensive coverage.
If You Know Command-Line Tools
Jump straight into Beginner examples. You'll recognize command patterns but see AI-specific workflows and best practices.
If You've Used AI Assistants Before
Start with Intermediate or Advanced based on your comfort level. Each example is self-contained, so you won't get lost.
As a Reference
Use the example index to find specific topics (e.g., "How do I debug with Claude Code?" → Example 47).
Comparison to Narrative Tutorials
| Aspect | By Example (This Tutorial) | Narrative Tutorial |
|---|---|---|
| Approach | Command-first (show, then explain) | Explanation-first (tell, then show) |
| Target Audience | Experienced developers | Beginners and intermediate |
| Coverage | 95% (comprehensive) | 60-85% (focused depth) |
| Example Count | 85 examples | 15-30 examples |
| Learning Style | Run commands, understand output | Read explanation, see examples |
| Use as Reference | Excellent (self-contained) | Moderate (sequential narrative) |
Both are valid approaches. Choose based on your learning style and experience level.
Diagrams and Visualizations
Approximately 40% of examples include Mermaid diagrams visualizing:
- Conversation flow (user request → AI response → file operation)
- Multi-file refactoring workflows
- Context management and indexing
- Error handling and iteration patterns
- Workflow optimization strategies
All diagrams use a color-blind friendly palette (Blue #0173B2, Orange #DE8F05, Teal #029E73, Purple #CC78BC, Brown #CA9161) meeting WCAG AA accessibility standards.
Prerequisites
- Claude Code installed and configured - See Initial Setup
- Command-line experience - Comfortable using terminal, running commands
- Programming knowledge - Familiarity with at least one programming language
- Git basics - Understanding version control for experimentation
No prior AI coding experience required - Examples start from fundamentals.
Learning Path
%% Color Palette: Blue #0173B2, Orange #DE8F05, Teal #029E73, Purple #CC78BC, Brown #CA9161
graph TD
A["Initial Setup<br/>0-5%"]
B["Quick Start<br/>5-30%"]
C["By Example: Beginner<br/>0-40%<br/>Examples 1-30"]
D["By Example: Intermediate<br/>40-75%<br/>Examples 31-60"]
E["By Example: Advanced<br/>75-95%<br/>Examples 61-85"]
A --> B
B --> C
C --> D
D --> E
style A fill:#0173B2,stroke:#000,color:#fff
style B fill:#DE8F05,stroke:#000,color:#fff
style C fill:#029E73,stroke:#000,color:#fff
style D fill:#CC78BC,stroke:#000,color:#fff
style E fill:#CA9161,stroke:#000,color:#fff
What's Next?
Ready to start learning? Choose your entry point:
- New to Claude Code? → Beginner Examples (1-30)
- Know the basics? → Intermediate Examples (31-60)
- Experienced with AI tools? → Advanced Examples (61-85)
Learning Progression
We measure progress by coverage depth, not time:
- Beginner: 0-40% coverage through 30 examples (essential commands, basic workflows)
- Intermediate: 40-75% coverage through 30 examples (production patterns, advanced features)
- Advanced: 75-95% coverage through 25 examples (expert techniques, optimization)
Work at your own pace. Move through examples as you build confidence with each pattern.
Key Principles
- Every example is runnable - Copy, paste, run, verify
- Annotations explain WHY - Understand the reasoning, not just commands
- Self-contained is non-negotiable - No hunting for context in other examples
- Production patterns, not toys - Real-world workflows you'll actually use
- Modern Claude Code - Latest CLI version, contemporary best practices
AI-Assisted Development Benefits
Throughout examples, you'll see how Claude Code provides:
- Speed - Generate boilerplate and common patterns instantly
- Understanding - Explain unfamiliar code in plain language
- Quality - Follow best practices automatically
- Learning - Discover new patterns and techniques
- Confidence - Validate approaches before implementation
- Efficiency - Reduce context switching and manual work
Feedback and Improvements
Found an error? See a better way to use Claude Code? Examples are continuously improved based on user feedback and new Claude Code features.
Let's start coding with AI! Choose your level and dive into the examples.
Examples by Level
Beginner (Examples 1–30)
- Example 1: What is Claude Code
- Example 2: Starting Interactive Session
- Example 3: Interactive Session with Initial Prompt
- Example 4: Understanding Claude's Tool Usage
- Example 5: File Operations in Interactive Mode
- Example 6: Exiting and Resuming Sessions
- Example 7: Basic Print Mode (
-p) - Example 8: Piping Content to Claude
- Example 9: Output Formats (text vs json)
- Example 10: Continuing Conversations Non-Interactively
- Example 11: JSON Output Parsing in Scripts
- Example 12: Session Management in Automation
- Example 13: npm Script Calling Claude for Code Generation
- Example 14: npm Script for Documentation Generation
- Example 15: npm Script with Environment Variables
- Example 16: npm Script Error Handling
- Example 17: npm Script Chaining Multiple Claude Commands
- Example 18: npm Script Output Capture and Processing
- Example 19: Pre-commit Hook Using Claude for Validation
- Example 20: Pre-push Hook Using Claude for Code Review
- Example 21: Managing Conversation History
- Example 22: Asking Follow-Up Questions
- Example 23: Canceling Operations
- Example 24: Checking Project Status
- Example 25: Clearing Context for Fresh Start
- Example 26: Basic Refactoring - Extract Variable
- Example 27: Adding Error Handling
- Example 28: Generating README Files
- Example 29: Creating .gitignore Files
- Example 30: Exiting Claude Sessions
Intermediate (Examples 31–60)
- Example 31: Basic GitHub Actions Workflow with Claude
- Example 32: Matrix Strategy with Claude - Testing Across Node Versions
- Example 33: Artifact Generation in CI/CD
- Example 34: Secret Management for Claude API Keys
- Example 35: Conditional Workflow Execution
- Example 36: Multi-Stage Pipeline (Lint → Test → Claude Analysis → Deploy)
- Example 37: PR Comment Generation with Claude
- Example 38: Release Notes Automation
- Example 39: Deployment Approval Gates with Claude Risk Assessment
- Example 40: Automated Rollback with Claude Failure Detection
- Example 41: Python Subprocess Calling Claude
- Example 42: Node.js Child Process with Claude
- Example 43: Java ProcessBuilder with Claude
- Example 44: Go exec.Command with Claude
- Example 45: Advanced Piping - Multi-Stage Claude Processing
- Example 46: Async/Await Migration from Callbacks
- Example 47: Error Handling Pattern Standardization
- Example 48: Configuration Extraction and Environment Variables
- Example 49: Design Pattern Implementation - Strategy Pattern
- Example 50: Dead Code Elimination
- Example 51: Automated Git Workflow - Branch Creation and Commits
- Example 52: Commit Message Generation from Changes
- Example 53: Pull Request Description Generation
- Example 54: Merge Conflict Resolution Assistance
- Example 55: CI/CD Configuration Generation
- Example 56: Automated API Documentation Generation
- Example 57: Architecture Diagram Generation with Mermaid
- Example 58: Code Comment and Docstring Addition
- Example 59: Performance Optimization Suggestions
- Example 60: Accessibility Audit and Improvements
Advanced (Examples 61–85)
- Example 61: Defining Custom Agents with --agents JSON
- Example 62: Subagent Delegation Patterns
- Example 63: MCP Server Integration (--mcp-config)
- Example 64: System Prompt Customization (--system-prompt, --append-system-prompt)
- Example 65: Tool Restriction and Permission Control
- Example 66: Complex Multi-Agent Workflows
- Example 67: Session Forking for Parallel Experiments
- Example 68: Production Monitoring with Claude
- Example 69: Error Recovery Patterns in Automation
- Example 70: Advanced Configuration Management for Production
- Example 71: Docker Optimization for Multi-Stage Builds
- Example 72: Kubernetes Manifest Generation
- Example 73: CI/CD Pipeline Optimization
- Example 74: Infrastructure as Code with Terraform
- Example 75: Advanced Testing - Property-Based Testing
- Example 76: Custom Agent Configuration for Project-Specific Workflows
- Example 77: Code Metrics and Quality Gates
- Example 78: Security Audit Automation
- Example 79: Compliance Checking - Licensing and Standards
- Example 80: Team Collaboration Patterns - Shared Context Documents
- Example 81: Performance Profiling Integration
- Example 82: API Versioning Strategies for Microservices
- Example 83: Custom Linting Rules Generation
- Example 84: Automated Release Notes Generation
- Example 85: Advanced Configuration and Customization - Monorepo Workspace Setup
Last updated February 1, 2026