AI コーディングの長すぎるプロンプトを圧縮する方法:コンテキスト整理と実用テンプレート

AI コーディングの長すぎるプロンプトを圧縮する実践ガイド。目的、背景、制約、ファイル範囲、ログ、プロジェクトルールを短く実行しやすいタスクカードに変える方法を整理します。

AI コーディングを長く使うほど、プロンプトは長くなりがちです。

最初は「この bug を直して」だけだったものが、数百字の背景、十数個のルール、完全なエラーログ、ディレクトリ説明、過去の判断、個人の好み、テスト要件、多くの「ついでに」へ膨らみます。完全に見えて、実際には使いにくくなります。モデルは焦点を見失い、不要なファイルを読み、token を消費し、タスクから外れやすくなります。

プロンプト圧縮とは、単に短く書くことではありません。低密度な内容を削り、タスク境界、入力、制約、受け入れ条件を AI がすぐ理解できるようにすることです。

Quick Answer

多くの AI coding prompt は 6 行のタスクカードにできます。

1
2
3
4
5
6
Goal: Fix failed login session refresh.
Scope: Start with src/auth/session.ts and tests/auth/session.test.ts.
Limit: First pass is analysis only; do not modify files; do not scan the whole repo.
Constraints: Do not change public API; do not delete files.
Validation: Give the smallest test command.
Output: List the 3 most likely causes and next steps.

長い文章より安定します。何をするか、どこを見るか、何をしないか、どう検証するか、何を出すかが明確だからです。

目的は「少なく書く」ことではなく、「モデルに推測させない」ことです。

長いプロンプトが悪くなる理由

1. 目的が背景に埋もれる

履歴や個人的判断が多いと、モデルは現在の問題を解く代わりに背景を要約し始めます。

2. 制約が衝突する

最小変更、ついでの refactor、テスト追加、大きく変えない、architecture 変更可、早く直す、が同時にあると優先順位が不明です。

3. ファイル範囲が広すぎる

“whole project”、“check everything”、“all related files” は不要な読み込みを招きます。

4. ログとコードを貼りすぎる

完全な logs、files、diff は重要情報を埋もれさせます。

5. 一時的な要望が長期ルールになる

一回限りの task を CLAUDE.mdAGENTS.md、memory に入れると後続タスクを汚します。

Claude Code の token spike が問題なら /ja/2026/07/08/claude-code-token-usage-spike-troubleshooting-faq/ も参照してください。

ステップ 1:何が長いか分ける

種類 必須か 圧縮方法
現在の目的 必須 明確な動詞を一つ残す
ファイル範囲 必須 正確な path を書く
制約 必須 操作に影響するものだけ残す
受け入れ条件 必須 test command や確認可能な結果を書く
過去背景 場合による 現在タスクに関係する結論だけ
完全ログ 通常不要 key error のみ
完全コード 通常不要 path を渡して読ませる
好み 場合による 長期 rule に移す
感情表現 不要 削除
“also” tasks 非推奨 次のタスクへ分ける

判断基準は、その文が Agent の次の行動を変えるかです。

ステップ 2:自然文をフィールドに変える

長い書き方:

1
I recently changed the login module. Users report that refreshing the page often logs them out. I suspect session expiry handling or cookie updates. Please look at it, avoid affecting other login logic, don't rewrite the architecture, maybe add tests, and check if similar problems exist elsewhere.

圧縮後:

1
2
3
4
5
Goal: Diagnose login state lost after page refresh.
Scope: src/auth/session.ts, src/auth/cookie.ts, tests/auth/session.test.ts.
Limit: Do not change auth architecture; first pass analysis only.
Validation: List minimal reproduction and tests to add.
Output: 3 likely causes ranked by probability.

情報量は減っていません。実行しやすくなっています。

ステップ 3:背景は結論だけ残す

低密度な背景は、次のように事実とリスクへ変えます。

1
2
3
4
Background facts:
- Login has a legacy compatibility layer; do not remove it.
- Web and mobile share session logic.
- There was a previous cookie write issue; check cookie update paths first.

モデルに必要なのは、あなたの思い出し方ではなく、変更判断に影響する事実です。

ステップ 4:“しないこと” を具体化する

曖昧な「慎重に」ではなく、禁止事項を書きます。

1
2
3
4
5
Forbidden:
- Do not change public API.
- Do not change database schema.
- Do not delete legacy compatibility logic.
- Do not do unrelated refactoring.

削除、移動、refactor は特に明確に制限します。誤削除対策: /ja/2026/07/10/ai-coding-avoid-accidental-file-deletion/。

ステップ 5:“also” を分割する

“also” は scope を広げます。

1
2
3
4
Phase 1: Diagnose the bug only; do not modify.
Phase 2: Fix the minimal code path.
Phase 3: Add the smallest related test.
Phase 4: Separately evaluate whether refactoring is needed.

長時間タスクは phase plan を作り、段階的に進めます。復旧方法: /ja/2026/07/10/ai-agent-long-task-resume-guide/。

ステップ 6:完全なファイルを貼らない

Agent に path を渡します。

1
2
3
4
5
6
Please read:
- src/auth/session.ts
- src/auth/cookie.ts
- tests/auth/session.test.ts

First pass: summarize relevant logic only. Do not modify.

大きいファイルならさらに絞ります。

1
Focus only on session refresh, cookie update, and logout branches.

ステップ 7:ログは key lines だけ

1
2
3
4
Command: pnpm test -- auth
Failing test: refresh session keeps user logged in
Key error: Expected status 200, received 401
Stack: tests/auth/session.test.ts:42

完全ログが必要ならファイルに置きます。

1
2
Full log is in .tmp/auth-test.log.
Read only the last 80 lines first.

ステップ 8:長期ルールは外へ出す

毎回繰り返す pnpm、migration 禁止、API 変更後の test、削除禁止、中国語 summary、既存 helper 優先などは、AGENTS.mdCLAUDE.md.cursor/rules/docs/ai-workflow.md、Codex Skill、Claude Code subagent に移します。

プロンプトでは次のように書きます。

1
2
Follow project AGENTS.md.
Extra limit for this task: do not modify src/db/migrations.

コードベース記憶と rule files は /ja/2026/07/10/ai-agent-codebase-memory-tools-comparison/。

ステップ 9:CLAUDE.md / AGENTS.md も圧縮する

残すもの:build commands、test commands、forbidden paths、style constraints、security boundaries、commit/validation rules、project-specific pitfalls。

削るもの:古い履歴、重複、感情的注意、長い architecture background、README の一般説明、一回限りの task record、曖昧な表現。

例:

1
2
- Search `src/components` before adding UI; prefer existing components.
- Do not create a generic component for a one-page change.

caveman のような圧縮ツールを使う場合も、安全ルールは人間が確認してください: /ja/2026/07/03/caveman-agent-output-compression-guide/。

ステップ 10:三つのテンプレート

Diagnosis

1
2
3
4
5
6
Goal:
Scope:
Symptom:
Key error:
Limit: First pass analysis only; do not modify files.
Output: 3 most likely causes + minimal validation steps.

Change

1
2
3
4
5
6
Goal:
Allowed files:
Forbidden files:
Implementation requirements:
Validation command:
Output: changed files + validation result + risks.

Review

1
2
3
4
Goal: Read-only review current diff.
Focus:
Limit: Do not modify files.
Output: List issues by severity; if none, say no high-risk issue found.

Cursor、Codex、Claude Code を併用する場合は、Cursor が locate、Claude Code が implement、Codex が review を担当できます: /ja/2026/07/10/cursor-codex-claude-code-workflow-division/。

Before / After

1
2
3
4
5
6
Goal: Diagnose login state lost after page refresh.
Scope: src/auth/session.ts, src/auth/cookie.ts, tests/auth/session.test.ts.
Background: There is old token compatibility; do not remove it.
Limit: First pass analysis only; do not modify; do not scan the whole repo.
Validation: Give minimal reproduction and test suggestions.
Output: 3 causes ranked by probability.

短く、明確です。

圧縮しすぎてはいけないもの

safety boundaries、permission limits、database migration rules、production risk、削除禁止ファイル、acceptance criteria、legal/compliance/privacy、正確な code、commands、paths、error strings は削らないでください。

悪い例:

1
Run related tests.

良い例:

1
Run `pnpm test -- auth`.

Team Rule

1
2
3
4
5
6
7
8
9
## Prompt Discipline

- Treat each task as a small work order.
- Prefer fields: goal, scope, limits, validation, output.
- Do not scan the whole repository unless explicitly requested.
- Ask before reading large logs or unrelated directories.
- Keep command output summarized; save full logs to files.
- Do not mix new feature work, refactor, docs, and cleanup in one task.
- Preserve exact paths, commands, error messages, and safety rules.

Checklist

問題 対処
目的が一文を超える verb + object にする
背景が 5 行超 3 facts に圧縮
file scope が曖昧 exact paths
“also” が複数 later tasks
制約が多い must / forbidden / optional
full log command、failing test、key error
full code paths
repeated rules AGENTS.md / CLAUDE.md
output requirements が長い list format
編集すべきか不明 first pass analysis only

まとめ

AI coding prompt が長くなるのは、目的、背景、制約、ログ、コード、長期ルールが混ざるからです。

安定した圧縮方法はタスクカード化です。目的を一文、scope を path、limits を明確に、validation を command、output を固定形式にします。背景は判断に影響する事実だけ、ログは key lines だけ、長期ルールは project files へ、“also” は次のタスクへ分けます。

记录并分享
Hugo で構築されています。
テーマ StackJimmy によって設計されています。