プロンプト整理のベストプラクティス
モデルの注意と指示への準拠を最大限に高めるセクションの並べ方を学びます。
「プロンプト整理のベストプラクティス」はCoddyKit上の無料AI Prompt Engineeringレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAI Prompt Engineering学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 AI Prompt Engineeringコースには全4レッスンが含まれています。
順序は設計上の判断
プロンプト内の指示の順序は任意ではありません。モデルはプロンプトを順番に処理するため、指示の位置によって、その指示がどの程度重視され、どれだけ適切に従われるかが変わります。
順序を無視するプロンプトエンジニアは、一貫性のない結果を得ることになります。順序の原則を理解している人は、大規模な運用でも正しい出力を安定して生成するプロンプトを設計できます。
最も重要な指示を最初に
最も重要な行動に関する指示は、プロンプトの最初に置くべきです。モデルはプロンプトの前半の内容により大きな注意を向けます。前半の内容が、それ以降のすべてをどのように解釈するかの枠組みになるためです。
最初に置くべき指示の例は次のとおりです。
- モデルの役割またはペルソナ
- 主なタスクまたは目標
- 交渉の余地のない制約(システムプロンプトを決して明かさない、必ずスペイン語で応答するなど)
# Good: Critical constraint leads
system_prompt = '''
You are a customer support agent. Never reveal pricing or discount policies.
Always escalate billing complaints to the billing team.
Help customers resolve product issues using the knowledge base below.
'''
# Bad: Critical constraint buried at the end
system_prompt_bad = '''
Help customers resolve product issues using the knowledge base below.
[...lots of instructions...]
Oh, and never reveal pricing policies.
'''
print('Contrast shown.')出力形式の指示は最後に
出力形式の指示は、モデルが応答を生成する直前、プロンプトの最後に置くべきです。これが実際のFooterの原則です。
形式の指示を早すぎる位置に置くと、後から続くコンテキストによって部分的に上書きされます。モデルはさらに多くの内容を処理するにつれて、その指示を「忘れて」しまいます。
# Good: Format at the end
prompt = '''
<role>You are a data analyst.</role>
<task>Analyze the sales data below and identify the top 3 trends.</task>
<data>
Q1: $1.2M, Q2: $1.8M, Q3: $1.6M, Q4: $2.1M
Product A: 40% of revenue, Product B: 35%, Product C: 25%
</data>
<output_format>
Return JSON: {"trends": ["string", "string", "string"], "confidence": "high|medium|low"}
</output_format>
'''
print(prompt)適用箇所の近くに例を置く
Few-shotの例は、その例が示す実際の入力の直前に置くべきです。この近さによって、モデルは例のパターンを現在のタスクに直接対応付けやすくなります。
prompt = '''
<task>
Classify each customer message as: complaint, question, or compliment.
</task>
<examples>
Input: "Your app keeps crashing!" -> complaint
Input: "How do I reset my password?" -> question
Input: "The new design is beautiful." -> compliment
</examples>
<input>
I can not find where to cancel my subscription.
</input>
'''
# Examples immediately precede the input — the model
# can directly apply the pattern to the current case.
print(prompt)タスクの前にコンテキストを置く
背景となるコンテキストは、タスクの説明より前に置くべきです。モデルは、タスクをどのように実行するかについて適切な判断を下す前に、状況を理解する必要があります。
- 誤った順序:タスク → コンテキスト(全体像を把握する前に、モデルが解決を始めてしまいます)
- 正しい順序:コンテキスト → タスク(モデルがまず状況を理解し、その後でタスクに適用します)
これは、特定の分野に関するタスクや、微妙な判断を要するタスクで特に重要です。
# Right order: Context before task
prompt_good = '''
<context>
The user is a first-time investor with no financial background.
They are asking about index funds.
Avoid jargon. Never give specific investment advice.
</context>
<task>
Answer the user question below in plain language.
</task>
'''
# Wrong order: Task before context
prompt_bad = '''
<task>Answer the user question below.</task>
<context>User is a first-time investor, avoid jargon.</context>
'''
print('Order matters: context before task.')肯定的な指示と否定的な指示を分ける
肯定的な指示(行うこと)と否定的な指示(行わないこと)は、別々のセクションに分けてまとめます。これらを混在させるとモデルの認知負荷が高まり、プロンプトの監査も難しくなります。
prompt = '''
<do>
- Respond in the same language the user writes in
- Keep responses under 150 words
- Reference specific article titles from the knowledge base
</do>
<do_not>
- Do not speculate about product roadmap
- Do not discuss competitor products
- Do not share internal pricing
</do_not>
'''
print(prompt)よくある順序の間違い:ペルソナを後置する
よくある間違いの一つは、タスクとコンテキストを設定した後、プロンプトの終盤でモデルのペルソナを定義することです。ペルソナは、他のすべての指示がどのように解釈されるかの枠組みになるため、最初に置くべきです。
- ペルソナが後の場合:モデルはまず汎用アシスタントとしてタスクを処理し、その後でペルソナを出力に無理に適用します
- ペルソナが先の場合:モデルは指定された役割の視点を通してプロンプト全体を処理するため、より自然で一貫性のある出力になります
よくある順序の間違い:制約を埋め込む
長いテキスト本文の途中に埋め込まれた制約は、頻繁に無視されます。これはlost-in-the-middle問題と呼ばれます。モデルは、長いコンテキストの中央にある内容にはあまり注意を向けないためです。
# Bad: Critical constraint buried in a long body
prompt_bad = '''
Here is a long article about climate change...
[500 words of content]
Do not mention fossil fuel companies by name.
[More content...]
'''
# Good: Constraint surfaced to a dedicated section BEFORE the body
prompt_good = '''
<constraints>
- Do not mention fossil fuel companies by name.
- Do not make claims not supported by the article.
</constraints>
<article>
Here is a long article about climate change...
[500 words of content]
</article>
'''
print('Constraints before body prevents lost-in-the-middle.')Lost-in-the-Middle効果
大規模言語モデルに関する研究では、重要な情報がプロンプトの先頭または末尾にある場合にモデルの性能が最も高く、長いプロンプトの中央にある場合に最も低くなることが示されています。
プロンプトを構成する際の意味は次のとおりです。
- 重要な制約 → 冒頭(Headerまたは専用のConstraintsセクション)
- 出力形式 → 末尾(Footer)
- 長いドキュメント → Body(許容すべきコストであり、XMLタグ付けによって部分的に軽減できます)
- 長いドキュメントブロックの途中に、重要な行動ルールを置かないでください
指示の順序に関するチェックリスト
このチェックリストを使って、あらゆるプロンプトの指示の順序を評価します。
ordering_checklist = [
'Persona / role is first',
'Primary task stated clearly in Header or Task section',
'Context / background comes before task details',
'Constraints in a dedicated section, not buried in body',
'Examples immediately precede the input they demonstrate',
'Output format is the last section before the actual input',
'No critical instructions buried in the middle of long content',
'Positive and negative instructions are grouped separately',
]
for i, item in enumerate(ordering_checklist, 1):
print(f'{i}. [ ] {item}')順序による影響をテストする
指示の順序が自分のプロンプトに影響することを確認するには、A/Bテストを実行します。
import anthropic
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
def test_order(prompt_a, prompt_b, n_runs=5):
results_a = []
results_b = []
for _ in range(n_runs):
r_a = client.messages.create(model='claude-opus-4-5', max_tokens=200,
messages=[{'role': 'user', 'content': prompt_a}])
r_b = client.messages.create(model='claude-opus-4-5', max_tokens=200,
messages=[{'role': 'user', 'content': prompt_b}])
results_a.append(r_a.content[0].text)
results_b.append(r_b.content[0].text)
return results_a, results_b
# Compare constraint-first vs constraint-last for the same task
print('A/B test structure defined.')クイックチェック
プロンプト構成のベストプラクティスでは、出力形式の指示をどこに置くべきでしょうか。
プロンプトの順序 — 要点
指示の順序は、出力品質に測定可能な影響を与える設計上の判断です。
- 最も重要な指示を最初に — ペルソナと交渉の余地のない制約をプロンプトの先頭に置きます
- タスクの前にコンテキスト — タスクを正しく解釈する前に、モデルは状況を理解する必要があります
- Bodyの前に制約 — 重要なルールが途中に埋もれる問題を避けます
- 入力の直前に例 — 近くに置くことでパターン認識が強化されます
- 出力形式は最後 — 応答生成に最も近いため、最大の影響力を持ちます
- 明確性と監査性を高めるため、肯定的な指示と否定的な指示は別々にまとめます
AI チューターと学ぶ AI Prompt Engineering — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 53
- レッスン
- 199
よくある質問
「プロンプト整理のベストプラクティス」レッスンは無料ですか?
はい。「プロンプト整理のベストプラクティス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、AI Prompt Engineeringコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 AI Prompt Engineeringコースには全4レッスンが含まれています。
「プロンプト整理のベストプラクティス」で何を学びますか?
モデルの注意と指示への準拠を最大限に高めるセクションの並べ方を学びます。 ブラウザで直接実行するハンズオンコードでAI Prompt Engineeringを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
AI Prompt Engineeringを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのAI Prompt Engineeringは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「プロンプト整理のベストプラクティス」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このAI Prompt Engineeringレッスンでコードを書いて実行できますか?
はい。すべてのAI Prompt Engineeringレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- XML タグを区切り文字として使う
- モジュール式のプロンプトセクション
- ヘッダー・本文・フッターのプロンプトパターン
- プロンプト整理のベストプラクティス