Home / Session 2
session-2.md — ~/hacking-with-ai
2

Memory & Context

Train your AI teammate

45 min CLAUDE.md Loading...
Session 2 of 8

// Overview

Your CLAUDE.md file is how you teach Claude your team's conventions. A well-crafted CLAUDE.md means AI-generated code that looks like your team wrote it - passing the blind review test.

Learning Objectives

// Setup

Review the existing CLAUDE.md and prepare for customization

bash
cd my-full-app
cat CLAUDE.md
# Review the current project guidelines
Verify: You should see the existing project guidelines with @Rule annotations

// Challenge

Pass the blind review test

25m +15m bonus

Customize CLAUDE.md so that AI-generated code is indistinguishable from human-written code. Your partner will review code without knowing which is AI-generated.

✓ Success Criteria

  • CLAUDE.md includes specific code examples, not just rules
  • Partner cannot identify AI-generated code
  • Code follows team naming conventions exactly
  • Error handling matches existing patterns
  • Test style matches team standards
⚠ Hints (click to reveal)
  • Ask Claude to analyze patterns first: 'What are the coding patterns in this codebase?'
  • Include DO NOT rules for common mistakes
  • Use /memory to edit project memory, or /init to bootstrap CLAUDE.md
  • Use /clear to fully reset session caches (skills, MCP tools, context) for a clean slate

Code Playground

Interactive

Try These Prompts

Analyze patterns
Analyze the coding patterns in this codebase: naming conventions, error handling, test structure, logging patterns. Give me specific examples I can put in CLAUDE.md.
Concrete examples teach better than abstract rules
Expected: Claude extracts real patterns with code snippets
Generate CLAUDE.md section
Help me write a CLAUDE.md section for error handling. Include DO and DO NOT examples based on patterns in this codebase.
Anti-patterns are as important as patterns
Expected: Claude creates actionable instructions with examples
Test the result
Using the patterns you found, write a service method that fetches a merchant by ID and handles the not-found case.
This tests if CLAUDE.md is working
Expected: Code should follow the documented patterns exactly
Explore auto-memory
Check what auto-memory entries exist for this project. Look in ~/.claude/projects/ for memory files. What has Claude learned across our sessions? Can you verify cross-session persistence?
Auto-memory is project-scoped — each project gets its own memory directory
Expected: Claude finds the auto-memory directory, shows entries, and explains how memories persist across sessions
Conditional rules & context control
Create a .claude/rules/api-endpoints.md file with paths: frontmatter so it only loads when working on API route files. Then check if includeGitInstructions is set in settings — if not, show how to disable built-in git instructions to save context.
Conditional rules use paths: frontmatter with glob patterns; includeGitInstructions can also be set via CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS env var
Expected: Claude creates a conditional rule file and explains context-saving settings
Customize output style
Try /output-style to switch between Default, Explanatory, and Learning modes. Then create a custom output style that matches your team's preferred verbosity level — e.g., terse with no commentary, or detailed with rationale for every decision.
/output-style has built-in modes (Default, Explanatory, Learning) plus you can define custom styles for project-specific needs
Expected: Claude switches output style and demonstrates the difference in verbosity and explanation depth

Config Generator

Code Examples

Effective CLAUDE.md Structure
# Project Guidelines

## Tech Stack
- Java 25, Spring Boot 3.5
- PostgreSQL 17, Kafka 7.9
- JUnit 5 + AssertJ + Testcontainers

## Code Style
@Rule Use record types for DTOs
@Rule Prefer constructor injection over field injection
@Rule Log with structured fields: log.info("msg", kv("key", value))

## Error Handling
@Rule Custom exceptions extend ServiceException
@Rule Always include error code and message

DO:
```java
throw new MerchantNotFoundException(
    ErrorCode.MERCHANT_NOT_FOUND,
    "Merchant not found",
    kv("merchantId", id)
);
```

DO NOT:
```java
throw new RuntimeException("Not found"); // No error code, no context
```

## Testing
@Rule Unit tests: *Test.java with @Nested classes
@Rule Integration tests: *IT.java with @Testcontainers
@Rule Use AssertJ for all assertions

## Modular Rules
@.claude/rules/error-handling.md
@.claude/rules/testing-patterns.md

## DO NOT
- Do not use Lombok @Data (prefer records)
- Do not catch generic Exception
- Do not use System.out.println (use structured logging)

## Also see
- .claude/rules/*.md for modular rule files (supports paths: frontmatter)
- CLAUDE.local.md for personal project-specific overrides
- Settings: set includeGitInstructions: false to reduce context usage
Memory Hierarchy
Memory Priority (highest to lowest):
================================
1. Managed Policy     - Enterprise rules (macOS plist / Windows Registry)
2. Project Memory     - CLAUDE.md in repo root
3. Project Rules      - .claude/rules/*.md files (supports paths: frontmatter)
4. User Memory        - ~/.claude/CLAUDE.md
5. User Rules         - ~/.claude/rules/*.md (global rules, like project rules but user-wide)
6. Local Project      - CLAUDE.local.md (gitignored)
7. Auto Memory        - ~/.claude/projects/<project>/memory/ (project-scoped)
8. Session Context    - Current conversation

Commands:
  /memory              Opens memory in editor
  /init                Bootstrap starter CLAUDE.md
  /clear               Full session reset (clears skills, MCP tools, caches)
  /output-style        Switch output modes (Default, Explanatory, Learning, or custom)

Settings:
  includeGitInstructions: false
    Removes built-in git/commit/PR instructions from system prompt
    (also: CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1)
  claudeMdExcludes: ["packages/legacy/**"]
    Exclude certain CLAUDE.md files from loading (useful for monorepos)

Features:
  @path.md             Include modular rule files
  .claude/rules/       Directory for modular rules
    - Supports paths: frontmatter for conditional loading
    - Supports symlinks (e.g., symlink shared rules across projects)
  ~/.claude/rules/     User-level global rules (same format as project rules)
  CLAUDE.local.md      Personal overrides (gitignored)
  Auto Memory          Claude automatically saves useful context per-project
    - MEMORY.md is the index file; topic-specific files alongside it
    - 200-line limit on MEMORY.md
    - All worktrees share one memory directory
    - Location: ~/.claude/projects/<project>/memory/

CLI Flags:
  --add-dir <path>     Add additional directories to Claude's scope
  Env: CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD
    Load CLAUDE.md from additional directories

Hooks:
  InstructionsLoaded   Fires when CLAUDE.md or .claude/rules/*.md are loaded

// Key Takeaways

// Resources

Memory System Docs Best Practices - Context Output Styles Settings Reference