Codex: Codex プロンプトのベストプラクティス

最終更新:2026-08-31

プロンプトは Codex への架け橋です。良いプロンプトは Codex を一発で正しく導き、悪いプロンプトは繰り返しの修正が必要になります。

📋 前提条件: Codex の基本操作を理解していること

1. 学ぶ内容


2. コア原則

(1) CLEAR の原則

原則 説明
Context コンテキストを提供 "Follow the style of auth.py"
Limit スコープを定義 "Only modify src/api/ directory"
Example 例を示す "Output format following types.ts"
Assert 検証を含める "Ensure npm test passes"
Reason 理由を説明 "Because Unicode support is needed"

(2) 良い例と悪い例の比較

TEXT 📖 参照専用
# 悪いプロンプト
"Help me write a function"

# 良いプロンプト
"In src/utils/date.ts, create a formatDate function,
input: Date object and locale string,
output: localized date string,
follow the style of formatNumber in the same file,
include null handling and unit tests."

3. 構造化記述

(1) 標準構造

TEXT 📖 参照専用
[Goal]: 何を達成するか
[Scope]: どのファイル/ディレクトリを変更するか
[Reference]: どの既存コードを参照するか
[Constraints]: どのルールに従うか
[Verification]: 完了をどう確認するか

▶ 例1:Alice の構造化プロンプト

TEXT 📖 参照専用
[Goal] ユーザーパスワードリセット機能を追加
[Scope] src/auth/reset.ts, src/api/reset.ts
[Reference] src/auth/login.ts のコードスタイル
[Constraints] bcrypt ハッシュを使用、トークンは1時間で有効期限切れ
[Verification] npm test -- --grep "reset" がすべて通過

(2) ステップバイステップの指示

複雑なタスクでは、ステップで説明します:

TEXT 📖 参照専用
Step 1: Create PasswordReset model with token, expires_at, user_id fields
Tell me when done, I'll confirm before continuing to step 2.

Step 2: Implement POST /api/auth/reset-request endpoint
Step 3: Implement POST /api/auth/reset-confirm endpoint
Step 4: Write integration tests for all endpoints

4. コンテキスト提供のテクニック

(1) 精密な参照

TEXT 📖 参照専用
# 良い:特定のファイルを参照
> Follow the User type definition in src/models/user.ts, add Profile type

# 悪い:曖昧な説明
> Follow existing models, add new ones

(2) 補足情報の添付

TEXT 📖 参照専用
# App で画像を添付
> Implement this page following the UI layout in the screenshot

# エラーログを参照
> Output from cat error.log:
> TypeError: Cannot read property 'id' of undefined
> Fix this error

(3) 環境の説明

TEXT 📖 参照専用
> Project uses Next.js 14 + App Router + TypeScript
> Database uses Prisma ORM + PostgreSQL
> Test framework is Vitest
> Add CRUD functionality for the new page

5. 検証指示

(1) テスト検証

TEXT 📖 参照専用
# テストの実行を要求
> Ensure npm test passes
> Run pytest, fix all failing cases
> Test coverage no less than 80%

(2) 型チェック

TEXT 📖 参照専用
> Ensure tsc --noEmit has no errors
> Add complete TypeScript type annotations

(3) Lint チェック

TEXT 📖 参照専用
> Ensure eslint has no errors
> Fix all lint warnings

▶ 例2:Bob の完全な検証

TEXT 📖 参照専用
Add pagination to the orders API:
1. Modify GET /api/orders endpoint
2. Support page and pageSize query parameters
3. Return pagination metadata
4. Ensure tsc --noEmit has no errors
5. Ensure eslint has no errors
6. npm test all pass
7. New tests covering pagination logic

6. 一般的なプロンプトテンプレート

(1) バグ修正

TEXT 📖 参照専用
Fix <Bug-description> in <file>.
Error message: <paste-error-log>
Expected behavior: <correct-behavior>
Ensure <verify-command> passes.

(2) 機能追加

TEXT 📖 参照専用
Add <feature> to <module>.
Follow the code style of <existing-file>.
Include input validation and error handling.
Write unit tests, ensure <verify-command> passes.

(3) コードリファクタリング

TEXT 📖 参照専用
Refactor <old-impl> to <new-impl>.
Maintain the external interface.
Ensure all existing tests pass.
Add new tests covering the new implementation.

(4) コードレビュー

TEXT 📖 参照専用
Review <file/PR>, focusing on:
1. Security vulnerabilities (SQL injection, XSS, etc.)
2. Performance issues (N+1 queries, memory leaks, etc.)
3. Code style consistency
4. Error handling completeness
5. Test coverage

(5) ドキュメント生成

TEXT 📖 参照専用
Generate documentation for <module/API>:
- API interface docs (Markdown format)
- Usage examples (including curl commands)
- Parameter description table
- Error code list

7. 高度なテクニック

(1) 役割設定

TEXT 📖 参照専用
You are a senior security engineer specializing in Web security.
Review the following code for security vulnerabilities, categorized by OWASP Top 10.

(2) 出力フォーマット制御

TEXT 📖 参照専用
Output in the following format:
1. One issue per line
2. Format: [Severity] file:line - Issue description
3. Severity: 🔴Critical 🟡Medium 🟢Low
4. Finally, provide a fix priority ranking

(3) 制約条件

TEXT 📖 参照専用
Only use dependencies already in the project, do not install new packages.
Do not modify .env files.
Do not delete existing tests.
All new functions must have TypeScript type annotations.

❓ よくある質問

Q プロンプトは英語で書くべきですか?
A Codex は複数の言語をサポートしていますが、英語の方が一般的に良い結果が得られます。複雑なタスクには英語を使用し、単純なタスクには好みの言語を使用できます。
Q 長いプロンプトは結果に影響しますか?
A いいえ。詳細な説明は短い説明より良い結果をもたらします。コンテキストウィンドウを超えないように注意してください。
Q 一度に複数のタスクを与えられますか?
A はい、優先順位をつけるか、ステップバイステップで実行することを明確に指定してください。一度に多すぎるタスクを与えると、Codex が一部を見落とす可能性があります。
Q Codex に特定のコードスタイルに従わせるにはどうすればよいですか?
A AGENTS.md にスタイルルールを定義するか、プロンプトでスタイルファイルを参照してください。ESLint/Prettier の設定を参照することもできます。
Q プロンプトで URL を参照できますか?
A Codex は URL に直接アクセスできませんが、URL から重要な内容をコピーしてプロンプトに含めることができます。

📖 まとめ


📝 練習問題

  1. 基本 (⭐):CLEAR 原則を使って3つの異なるシナリオのプロンプトを書く。
  2. 中級 (⭐⭐):5つの一般的なシナリオをカバーする独自のプロンプトテンプレートライブラリを作成する。
  3. 上級 (⭐⭐⭐):同じタスクで「良いプロンプト」と「悪いプロンプト」を比較し、実行の違いに関する分析レポートを作成する。
Web-Tutorial.com

Web-Tutorial 技術チーム

複数の開発者によって共同維持されているプログラミングチュートリアルプラットフォーム。各チュートリアルは専門分野の開発者が執筆・レビューしています。正確で信頼性の高いコンテンツを目指しています — 問題を見つけた場合はお知らせください。

100%