Claude Code
Using Claude Code for agentic development
Claude Code is Anthropic's official CLI tool for agentic software development. It provides an interactive terminal interface where Claude can read files, edit code, run commands, and execute multi-step workflows autonomously. Unlike traditional chat interfaces, Claude Code operates directly in your development environment with configurable permissions, hooks, and context management.
Key Concepts
CLI Commands and Flags
Claude Code is invoked via `claude` in the terminal. Key flags include `-dangerously-skip-permissions` for fully autonomous mode (use with caution), `-print` for non-interactive output, `-resume` to continue a previous session, and `-model` to select a specific model. Slash commands like `/compact`, `/clear`, `/config`, and `/help` provide in-session controls. The `claude -help` command shows all available options.
CLAUDE.md Context Files
CLAUDE.md files provide persistent context to Claude Code sessions. Place a CLAUDE.md in your project root to define coding conventions, architecture notes, preferred libraries, and workflow instructions. Claude reads this file at session start, ensuring consistent behavior across sessions. Use it to document project-specific patterns, testing requirements, and code review standards.
Permission Modes
Claude Code has three permission modes: 'ask' (default) prompts for approval on file edits and commands, 'allow' auto-approves certain safe operations, and 'auto-edit' automatically applies file changes while still asking for shell commands. Use `claude -permission-mode auto-edit` for faster iteration on trusted codebases. The `-dangerously-skip-permissions` flag removes all guardrails-only use in isolated environments.
MCP (Model Context Protocol) Servers
MCP servers extend Claude Code's capabilities by exposing external tools and data sources. Configure MCP servers in your settings to connect to databases, APIs, file systems, or custom tools. Each server provides tools that Claude can invoke during agentic loops. Popular MCP servers include filesystem access, PostgreSQL connections, web scraping tools, and Git operations. MCP follows a standardized JSON-RPC protocol.
Agentic Loops and Tool Use
Claude Code operates in agentic loops: it plans, executes tools, observes results, and iterates. Tools available include file read/write, shell command execution, and MCP-provided tools. Claude decides which tools to use based on your prompt and context. It can chain multiple operations-reading files, analyzing code, making edits, and running tests-all in a single session without manual intervention.
Hooks and Automation
Hooks allow you to trigger automated actions at specific points in Claude Code's workflow. Pre-tool hooks run before a tool executes, enabling validation or modification of inputs. Post-tool hooks run after completion, useful for logging, notifications, or cleanup. Hooks are configured in settings and can call external scripts or APIs. Use hooks to enforce coding standards, run linters automatically, or integrate with CI/CD pipelines.
Context Management and /compact
Claude Code maintains a conversation context that grows as the session progresses. Use `/compact` to summarize and condense the context, retaining key information while reducing token usage. This is essential for long sessions. The `-resume` flag lets you continue previous sessions, and context is persisted across restarts. Strategic use of `/clear` and `/compact` keeps sessions responsive.
Session Persistence and History
Claude Code saves session state, allowing you to resume work after interruptions. Use `claude -resume` to pick up where you left off. Session history is stored locally and includes conversation, file states, and tool outputs. You can also export sessions for sharing or documentation. This persistence enables iterative development over days, not just single sessions.
Solved Examples
Problem 1:
You want Claude Code to automatically apply file edits without asking for permission, but still confirm before running shell commands. What command-line flag or mode should you use?
Solution:
Step 1: Identify the permission mode that auto-approves file edits but not shell commands.
Step 2: The 'auto-edit' mode fits this requirement-it applies file changes automatically.
Step 3: Shell commands still require approval in auto-edit mode.
Answer: Use `claude -permission-mode auto-edit` to enable automatic file edits while maintaining shell command confirmation.
Problem 2:
Your project has specific coding conventions (use tabs, prefer named exports, no console.log in production). How do you ensure Claude Code follows these consistently across all sessions?
Solution:
Step 1: Create a CLAUDE.md file in the project root directory.
Step 2: Document the coding conventions clearly:
```markdown
# Project Conventions
- Use tabs for indentation
- Prefer named exports over default exports
- No console.log statements in production code
- Run `npm test` after each change
```
Step 3: Claude Code reads this file automatically at session start.
Answer: Place coding conventions in a CLAUDE.md file at the project root-Claude reads it each session and follows the documented patterns.
Problem 3:
You're working on a long-running Claude Code session that has accumulated many messages. The response quality is degrading due to context bloat. How do you reduce context size without losing important information?
Solution:
Step 1: Identify the problem-context window is filling with irrelevant or repetitive history.
Step 2: Use the `/compact` slash command to summarize the session.
Step 3: `/compact` condenses the conversation, retaining key decisions and file states while removing verbose outputs.
Step 4: Alternatively, use `/clear` to reset if you want to start fresh.
Answer: Use the `/compact` command during the session to summarize and condense context, or use `/clear` to reset entirely. For future sessions, use `-resume` to continue from a compacted state.
Problem 4:
You need Claude Code to connect to a PostgreSQL database and query data as part of an agentic workflow. What mechanism enables this external tool integration?
Solution:
Step 1: Claude Code needs external tool access beyond its built-in file/shell capabilities.
Step 2: MCP (Model Context Protocol) servers provide this extensibility.
Step 3: Configure a PostgreSQL MCP server in Claude Code settings.
Step 4: Once configured, Claude can invoke the MCP server's tools to run queries.
Answer: Use an MCP (Model Context Protocol) server for PostgreSQL. Configure it in settings, and Claude Code can invoke the server's tools to execute database queries during agentic loops.
Tips & Tricks
- Start with the default 'ask' permission mode until you trust the codebase-then graduate to 'auto-edit' for faster iteration. Reserve `--dangerously-skip-permissions` for isolated test environments only.
- Keep CLAUDE.md focused and concise-include only conventions, patterns, and instructions that Claude can act on. Long context files dilute signal and waste tokens.
- Use `/compact` proactively every 20-30 turns in long sessions to maintain response quality and keep context relevant.
- Configure hooks to run linters (ESLint, Pylint) automatically after file edits-this catches issues early and maintains code quality without manual intervention.
- When debugging complex issues, use `--resume` to continue from a previous session rather than re-explaining the problem. Claude retains the investigation context.
- MCP servers are powerful but add complexity. Start with the built-in tools (file read/write, shell commands), then add MCP servers only when you need capabilities like database access or API integration.
Ready to practice?
Test your understanding with questions and get instant feedback.