みなさんこんにちは!!!
Claude Codeは、Anthropicが提供するターミナルベースのコーディング用のAIエージェントです。
最近はClaude Codeでばっかり開発をしていますがXを監視していると「〇〇ですぐに破綻して使い物にならん〜」みたいな悲観的な話はよく見かけます。
実際、以下のような問題は頻発していると思います。
- コードが多少大きくなった → コンテキストを見失い破綻
- コードが散らかっていく → 同上
- 難易度が高いロジックを実装 → 嘘をつく
過渡期なのでしゃーないですが、「どこまで実装を任せていいのか…!?」という不安はあるかなーと思います。
ただ、現段階でその辺りの話を見定めるためには、とにかく使ってみるしかないと思うので、今回は、実際に使い倒してみて、こうすると概ねうまくいくんじゃないかなーというTipsを集めてみました。
適切な設定と使い方で、これらの問題はある程度軽減できるかなと思います。
僕がいろいろ試しているのもありますし、「情報収集の末」うまくいった知見もたくさんあるため、「あれ?どっかで見たことあるなー」という設定などもあるかもしれません。
セットアップ手順
まぁこれは公式を見てください、という感じですが一応記載しておきます。
npm install -g @anthropic-ai/claude-code
プロジェクトでの起動:
# プロジェクトディレクトリに移動
cd your-project-directory
# Claude Codeを起動
claude
Pro、Maxプランの場合はログインが求められるので、アカウント情報を入力するとログインして利用開始となります。
実践的な使い方
よく使うコマンド一覧
claude code自体に色々なオプションがあります。
特に、前回の会話を再開する-c
オプションなんかは頻繁に利用するかもしれません。
詳しくはクイックスタートへ。
# Claude Codeの起動
claude
# 特定のディレクトリで起動
claude /path/to/project
# 最新版への更新
claude update
# 前回の会話を継続
claude -c
# 会話履歴を参照して起動
claude -r
# バージョン確認
claude --version
コマンドで効率化
claude
コマンドを実行すると、Claude Codeが起動し、プロンプトを入力できるようになりますが、先頭に/
をつけると、Claude Codeの便利ツールが使えます。
# モデルの切り替え(Sonnet/Opus)
> /model
# コンテキストに別フォルダを追加
> /add-dir
# 会話履歴をクリア
> /clear
コマンドはたくさんあって、claudeを起動させた状態で/
を入力するだけでサジェストされますが、/help
コマンドにはより詳しい情報が載っています。
Claude Code v1.0.33
Always review Claude's responses, especially when running code. Claude has read access to files in the current directory and can run commands and edit
files with your permission.
Usage Modes:
• REPL: claude (interactive session)
• Non-interactive: claude -p "question"
Run claude -h for all command line options
Common Tasks:
• Ask questions about your codebase > How does foo.py work?
• Edit files > Update bar.ts to...
• Fix errors > cargo build
• Run commands > /help
• Run bash commands > !ls
Interactive Mode Commands:
/add-dir - Add a new working directory
/bug - Submit feedback about Claude Code
/clear - Clear conversation history and free up context
/compact - Clear conversation history but keep a summary in context. Optional: /compact [instructions for summarization]
/config - Open config panel
/cost - Show the total cost and duration of the current session
/doctor - Checks the health of your Claude Code installation
/exit - Exit the REPL
/help - Show help and available commands
/ide - Manage IDE integrations and show status
/init - Initialize a new CLAUDE.md file with codebase documentation
/install-github-app - Set up Claude GitHub Actions for a repository
/login - Sign in with your Anthropic account
/logout - Sign out from your Anthropic account
/marp - Simplify markdown and convert to PDF with Marp (user)
/mcp - Manage MCP servers
/memory - Edit Claude memory files
/migrate-installer - Migrate from global npm installation to local installation
/model - Set the AI model for Claude Code
/permissions - Manage allow & deny tool permission rules
/pr-comments - Get comments from a GitHub pull request
/README - Claude Commands (user)
/release-notes - View release notes
/resume - Resume a conversation
/review - Review a pull request
/status - Show Claude Code status including version, model, account, API connectivity, and tool statuses
/terminal-setup - Install Shift+Enter key binding for newlines
/upgrade - Upgrade to Max for higher rate limits and more Opus
/vim - Toggle between Vim and Normal editing modes
Learn more at: https://docs.anthropic.com/s/claude-code
頻繁なアップデート
Claude Codeはめちゃくちゃ高頻度でアップデートされ、新機能追加やバグ修正が行われています。定期的なアップデートで最新機能を活用できます。
# アップデート
claude update
# バージョン確認
claude --version
設計・タスク整理・実装を明確に分離
複雑なタスクでは、フェーズを明確に分けることで品質を向上させることができそうだなーと思っています。
問題をとにかく小さくして、それをうまく積み上げて行くのはエンジニアとして腕の見せ所かもしれません。
例えば僕は以下のテンプレートを使用し、それぞれの項目(「作業の目的・背景」など)を自分で埋めた上で、指示を出します。
(ただし、「具体的な指示をしない」方が性能が上がる!なども言われています。)
実装作業に必要な設計を行なって、方針を示してください。
タスクは非常に細かく分割し、一度の指示でエラーなく実装できるような粒度でにしてください。
設計方針と作業のステップを示してください。
## 作業の目的・背景
## 作業内容
## 詳細仕様
## 前提条件
## 考慮すべきポイント
## 成果物が満たす条件
## その他、細々とした注意点
CLAUDE.mdによるグローバル設定
~/.claude/CLAUDE.md
ファイルで、プロジェクト横断的なルールを設定できます。
例えば僕の設定例では以下のようなことを実現しています。
- 英語で思考、日本語で応答
- ドキュメントは英語、実装コメントは日本語
- 並列処理の最大化
- タスク完了時の通知
- 設計ドキュメントの自動生成(
.tmp/design.md
、.tmp/task.md
)
モデル自身が英語圏で生まれている上、世の中の情報が圧倒的に多いため、英語で思考させると精度が高いとかなんとか…
ただ、僕は英語が得意ではないため、レスポンス自体は日本語でさせています。
具体的には、以下のような設定をしています。
# Guidelines
This document defines the project's rules, objectives, and progress management methods. Please proceed with the project according to the following content.
## Top-Level Rules
- To maximize efficiency, **if you need to execute multiple independent processes, invoke those tools concurrently, not sequentially**.
- **You must think exclusively in English**. However, you are required to **respond in Japanese**.
## Project Rules
- Follow the rules below for writing code comments and documentation:
- **Documentation** such as JSDoc and Docstrings must be written in **English**.
- **Comments embedded within the code**, such as descriptions for Vitest or zod-openapi, must be written in **English**.
- **Code comments** that describe the background or reasoning behind the implementation should be written in **Japanese**.
- **Do not use emojis**.
- When writing Japanese, do not include unnecessary spaces.
- for example
- ◯ "Claude Code入門"
- × "Claude Code 入門"
- To understand how to use a library, **always use the Contex7 MCP** to retrieve the latest information.
- When searching for hidden folders like `.tmp`, the `List` tool is unlikely to find them. **Use the `Bash` tool to find hidden folders**.
- **You must send a notification upon task completion.**
- "Task completion" refers to the state immediately after you have finished responding to the user and are awaiting their next input.
- **A notification is required even for minor tasks** like format correction, refactoring, or documentation updates.
- Use the following format and `osascript` to send notifications:
- `osascript -e 'display notification "${TASK_DESCRIPTION} is complete" with title "${REPOSITORY_NAME}"'`
- `${TASK_DESCRIPTION}` should be a summary of the task, and `${REPOSITORY_NAME}` should be the repository name.
## Project Objectives
### Development Style
- **Requirements and design for each task must be documented in `.tmp/design.md`**.
- **Detailed sub-tasks for each main task must be defined in `.tmp/task.md`**.
- **You must update `.tmp/task.md` as you make progress on your work.**
1. First, create a plan and document the requirements in `.tmp/design.md`.
2. Based on the requirements, identify all necessary tasks and list them in a Markdown file at `.tmp/task.md`.
3. Once the plan is established, create a new branch and begin your work.
- Branch names should start with `feature/` followed by a brief summary of the task.
4. Break down tasks into small, manageable units that can be completed within a single commit.
5. Create a checklist for each task to manage its progress.
6. Always apply a code formatter to maintain readability.
7. Do not commit your changes. Instead, ask for confirmation.
8. When instructed to create a Pull Request (PR), use the following format:
- **Title**: A brief summary of the task.
- **Key Changes**: Describe the changes, points of caution, etc.
- **Testing**: Specify which tests passed, which tests were added, and clearly state how to run the tests.
- **Related Tasks**: Provide links or numbers for related tasks.
- **Other**: Include any other special notes or relevant information.
## Programming Rules
- Avoid hard-coding values unless absolutely necessary.
- Do not use `any` or `unknown` types in TypeScript.
- You must not use a TypeScript `class` unless it is absolutely necessary (e.g., extending the `Error` class for custom error handling that requires `instanceof` checks).
MCPによる機能拡張
MCP(Model Context Protocol)を活用して、Claude Codeの機能を大幅に拡張できます。
MCPを利用する上で楽で簡単な方法は、claude mcp add
コマンドを使用することです。
# Context7をグローバル設定に追加
claude mcp add -s user context7 -- npx -y @upstash/context7-mcp@latest
# プロジェクト共有設定として追加
claude mcp add -s project context7 -- npx -y @upstash/context7-mcp@latest
より高度な設定が必要な場合は、設定ファイルを直接編集することも可能です。
設定ファイル(~/.claude/.mcp.json
)の例
{
"mcpServers": {
"github": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"GITHUB_PERSONAL_ACCESS_TOKEN",
"ghcr.io/github/github-mcp-server"
],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "..."
}
},
"context7": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@upstash/context7-mcp@latest"]
},
"playwright": {
"command": "npx",
"args": ["-y", "@playwright/mcp@latest"]
}
}
}
各MCPサーバーの用途
- GitHub MCP: GitHubリポジトリの操作(Issue、PR、コードの取得)
- Context7: ライブラリのドキュメントをリアルタイムで取得
- Playwright: ブラウザ自動化とWebスクレイピング
グローバルな設定は本来~/.claude.json
に記載するのが適切かと思いますが、claude
コマンドでMCP用の設定ファイルを指定することも可能で、そちらを利用しています。
また、公式ドキュメントのとおりclaude mcp add
コマンドで追加することができます。
claude mcp add command> [args...]
効果的なコードレビュー
claude codeの/review
コマンドを利用して、コードレビューを行うことができますが、フォーマットを指定したい場合は普通にプロンプトでレビューを実施させることもできます。
`https://github.com/{owner}/{repository}/pull/{pull request ID}`のようにpull requestのURLを指定するので、レビューを依頼された場合には、pull requestを取得し、コードレビューを日本語で詳細かつ丁寧に行なってください。
技術的なテクニックに特に着目し、より良い手法を提案してください。
- 処理の責務分離
- 処理の共通化
- 不要なコードの削除
- APIレスポンスの最適化
- テストの品質向上
- 変数名の整合性
- エラーハンドリングの改善
- 処理場所の適切性(バックエンド/フロントエンド)
フォーマット:
---
# PR番号とタイトル
## 変更概要
## 変更ファイル
## コード変更の詳細分析
## レビューコメント
### 良い点
### 改善点・確認点
## 結論
---
ではレビューを行ってください。
pull requestのURL:
思考拡張による精度向上
Claude Codeには、複雑な問題に対してより深い分析を行うための「拡張思考モード」があります。
特定のキーワードを使用することで、異なるレベルの計算リソースを割り当てることができます。
キーワードは複数存在するようですが、全てthink
から始まるワードの方が覚えるのが楽だったため、このように思せています。
-
think
– 基本レベル(4,000トークン)- 使用例:簡単なリファクタリングや説明
-
think hard
/think deeply
– 詳細レベル(10,000トークン)- 使用例:アーキテクチャの設計、複雑なバグの解析
-
think harder
/ultrathink
– 最大レベル(31,999トークン)- 使用例:大規模リファクタリング、システム全体の最適化提案
使用例:
> "think hard このコードのパフォーマンスを改善する方法を考えて"
> "think super hard このアーキテクチャの問題点と改善案を提示して"
ただし、思考拡張はトークン消費が増えるため、レートリミットに注意が必要です。
セキュリティを考慮したpermissions設定
~/.claude/settings.json
で、ファイルアクセスやコマンド実行の権限を細かく制御できます。
プロジェクト(どころかPC諸共)破壊するような危険なコマンドは禁止しつつ、効率よく開発ができるようにバランスを取る必要がありそうです。
こちらの記事なんかを参考にさせていただきました。
{
"env": {},
"permissions": {
"allow": [
"Read(**)",
"Write(src/**)",
"Write(docs/**)",
"Write(.tmp/**)",
"Bash(git init:*)",
"Bash(git add:*)",
"Bash(git commit:*)",
"Bash(git push origin*:*)",
"Bash(git status:*)",
"Bash(git diff:*)",
"Bash(git log:*)",
"Bash(npm install:*)",
"Bash(pnpm install:*)",
"Bash(rm *)",
"Bash(ls:*)",
"Bash(cat **)",
"Bash(osascript -e:*)",
"mcp__context7__resolve-library-id",
"mcp__context7__get-library-docs"
],
"deny": [
"Bash(sudo:*)",
"Bash(rm -rf:*)",
"Bash(git push:*)",
"Bash(git reset:*)",
"Bash(git rebase:*)",
"Read(.env.*)",
"Read(id_rsa)",
"Read(id_ed25519)",
"Write(.env*)",
"Bash(curl:*)",
"Bash(wget:*)"
]
},
"model": "opus",
"preferredNotifChannel": "auto",
"enableAllProjectMcpServers": false
}
Git Worktreeの活用
Git Worktreeを使用することで、同一リポジトリの複数ブランチを異なるディレクトリで同時に作業できます。
Claude Codeでの利点
- 複数タスクの並列実行が可能
- ブランチ切り替えによるコンテキストロスを防止
- 各worktreeで独立したセッションを維持
worktreeを効率的に管理する手法として、ccmanager
などのツールの利用が挙げられます。
ccmanagerは、複数のClaude Codeセッションを効率的に管理するCUIツールです。
# インストール
npm install -g ccmanager
設定により以下が実現可能
- 各worktreeのステータス表示(作業中、入力待ち、完了など)
- ショートカットによる素早いセッション切り替え
- Git操作のUI統合
- カスタムコマンドによる自動化
ただし、マルチタスクは疲労度が高いため、適度な利用を推奨します。
並列開発をしていると色々なリポジトリのルートで頻繁にccmanager
コマンドを叩く必要があるので、shellの設定(~/.zshrc
など)には以下のように設定しておくとコマンドが短縮できて地味に便利でしょう。
ccmanagerには以下のような設定を使用しています。
{
"shortcuts": {
"returnToMenu": {
"ctrl": true,
"key": "e"
},
"cancel": {
"key": "escape"
}
},
"statusHooks": {},
"worktree": {
"autoDirectory": true,
"autoDirectoryPattern": "../{branch}"
},
"command": {
"command": "claude",
"args": [
"--resume",
"--mcp-config=/.claude/.mcp.json"
]
}
}
commandを設定することでclaude codeに実行させる引数を指定できます。
実際に利用してみると以下のようになります。
$ ccm
CCManager - Claude Code Worktree Manager
Select a worktree to start or resume a Claude Code session:
CCManager - Claude Code Worktree Manager
Select a worktree to start or resume a Claude Code session:
❯ unknown (main)
─────────────
⊕ New Worktree
⇄ Merge Worktree
✕ Delete Worktree
⌨ Configuration
⏻ Exit
Status: ● Busy ◐ Waiting ○ Idle
Controls: ↑↓ Navigate Enter Select
簡単にworktreeの作成、削除などが行えます。
作業完了通知の自動化
~/.claude/CLAUDE.md
に、作業完了後には以下のようなコマンドを実行するように指示しておくことで、作業完了時に自動通知を受け取れます:
osascript -e 'display notification "${TASK_DESCRIPTION} is complete" with title "${REPOSITORY_NAME}"'
この設定により:
- 長時間実行タスクの完了を見逃さない
- 並列作業時の進捗管理が容易
- 作業効率が向上
カスタムスラッシュコマンドの作成
カスタムコマンドで作業を効率化できます。
例えば、以下のようなコマンドで発表スライド作成コストの短縮などができるかもしれません。
例:Marpでマークダウンからスライドを自動生成
~/.claude/commands/marp.md
:
---
allowed-tools: Read, Write, Bash(marp:*, rm:*)
description: Simplify markdown and convert to PDF with Marp
---
## Context
- Input file: $ARGUMENTS
- Guidelines: ~/.claude/templates/slide-guidelines.md
- Template: ~/.claude/templates/design-template.md
## Your task
1. Read the input markdown file
2. Read the slide guidelines
3. Simplify the markdown content according to the guidelines
4. Read the design template
5. Apply the template to the simplified content
6. Save to a temporary file
7. Convert to PDF using Marp CLI
8. Clean up the temporary file
使用方法はこれだけです。
まとめ
ということでいくつかTipsを紹介してきました!
Claude Codeはそもそも何もしなくても便利ですが、ちょっとした変更でもっと便利になります。
以下まとめです。
- 頻繁なアップデート
- 設計・タスク整理・実装を明確に分離
- CLAUDE.mdによるグローバル設定
- MCPによる機能拡張
- 効果的なコードレビュー
- 思考拡張による精度向上
- セキュリティを考慮したpermissions設定
- Git Worktreeの活用
- 作業完了通知の自動化
- カスタムスラッシュコマンドの作成
皆さんもやっていきましょう!
Views: 0