Llm prompt engineering

From jleechanorg/claude-commands · View interactive version

Llm prompt engineering is an AI agent skill from the GitHub repository jleechanorg/claude-commands. Write and debug system prompts, agent instructions, and LLM behavior contracts.

How to use this skill

Skill file

LLM Prompt Engineering - System Instruction Best Practices

Purpose: Guidelines for writing effective system instructions that LLMs actually follow, based on real debugging experiences.

Core Principle

When LLMs ignore instructions, it's usually the prompt's fault, not the LLM's.

LLMs are excellent at following clear, explicit instructions but will apply reasoning and judgment to ambiguous ones. This guide shows how to write prompts that remove ambiguity.

The Five Critical Rules

1. Use Unconditional Language for Critical Instructions

Problem: Conditional language gives LLMs room to apply judgment and override instructions.

Real Example from CharacterCreationAgent Bug:

❌ Conditional (LLM overrode this):

If `character_creation_in_progress` is true, clear it.

LLM's Response:

"Character creation flag 'character_creation_in_progress' is still true. Since we are still in the setup phase, I am maintaining it until the user is actually ready to enter story mode."

The LLM applied judgment: "I think we're still setting up" β†’ overrode instruction.

βœ… Unconditional (LLM followed this):

## 🎭 CRITICAL: Character Creation Flag - CLEAR IT NOW

IF YOU SEE `custom_campaign_state.character_creation_in_progress` ANYWHERE IN YOUR INPUT, IT MUST BE FALSE IN YOUR OUTPUT.

NO EXCEPTIONS. NO JUDGMENT. NO "BUT WE'RE STILL SETTING UP".

YOU ARE STORY MODE. FLAG MUST BE FALSE. ALWAYS.

LLM's Response: βœ… Cleared the flag without judgment.

When to Use Each:

2. Document INPUT and OUTPUT Schemas Explicitly

Problem: LLMs can't follow instructions about data structures they don't understand.

MANDATORY for every system prompt:

## Input Schema: What You Receive

You receive a `GAME STATE` section with this structure:

{

"custom_campaign_state": {

"flag_name": true/false, ← Document exact path

"nested_field": {

"value": "..."

}

},

"player_character_data": {...}

}


**CHECK `custom_campaign_state.flag_name` to understand current status.**

## Output Schema: What You Must Return

Your response MUST include:

{

"state_updates": {

"custom_campaign_state": {

"flag_name": false ← Show exact structure

}

}

}


**MANDATORY FIELDS:**
- `state_updates.custom_campaign_state.flag_name` - Always update this
- `state_updates.player_character_data` - Update as choices are made

Why This Works:

3. Position Critical Instructions in Top 50 Lines

Problem: Instruction compliance degrades with position in long prompts.

Compliance by Position (observed in real debugging):

Real Example:

❌ Failed (line 525 in 2064-line file):

### 🎭 Character Creation Flag Management
(line 525 of 2064)

Result: LLM ignored instruction

βœ… Succeeded (line 22 in same file):

## 🎭 CRITICAL: Character Creation Flag - CLEAR IT NOW
(line 22 of 2064)

Result: LLM followed instruction

How to Position:

  1. Place critical instructions at lines 1-50
  2. Use clear visual separators (## CRITICAL, ---)
  3. Use emojis for visual anchors (🎭, 🚨, ⚠️)
  4. Repeat critical instructions before related sections

4. Enforce Prompt Size Budgets

Problem: Prompt bloat causes LLMs to ignore instructions.

Target Token Counts per Agent Type:

| Agent Type | Target Lines | Max Lines | Current Status |

|------------|--------------|-----------|----------------|

| Character Creation | 600-800 | 1000 | βœ… 607 (after removing mechanics) |

| Story Mode | 1500-2000 | 2500 | ⚠️ 2064 (at limit) |

| Combat Mode | 1200-1800 | 2000 | - |

| Info Mode | 500-800 | 1000 | - |

| God Mode | 400-600 | 800 | - |

How β€œCurrent Status” is measured:

Red Flags:

Real Example:

❌ Before (1236 lines):

βœ… After (607 lines, 50% reduction):

How to Cut Bloat:

  1. Remove cross-domain content (combat from creation, narrative from info)
  2. Consolidate redundant sections
  3. Move reference tables to appendix
  4. Use shorter examples
  5. Cut explanatory text that repeats obvious points

5. Debug via Log Evidence

Problem: When instructions fail, you need to see WHY the LLM ignored them.

Debugging Checklist:

  1. Check LLM's thinking logs (if available):

```

"Character creation flag 'character_creation_in_progress' is still true.

Since we are still in the setup phase, I am maintaining it..."

```

This reveals judgment override behavior.

  1. Verify instruction position:

```bash

grep -n "critical instruction text" prompt_file.md

```

If line number > 500 in long file β†’ reposition to top

  1. Count prompt lines:

```bash

wc -l $PROJECT_ROOT/prompts/*.md

```

If total >1500 lines β†’ identify bloat to cut

  1. Test conditional vs unconditional:
  1. Add explicit schemas:

Checklist for New System Prompts

Before deploying any system prompt, verify:

Common Failure Patterns

Pattern 1: "I think..." Override

Symptom: LLM says "I think we're still..." and ignores instruction.

Diagnosis: Conditional language gave room for judgment.

Fix: Change to unconditional ("MUST BE X. NO EXCEPTIONS.")

Pattern 2: Silent Non-Compliance

Symptom: LLM doesn't mention instruction, just doesn't follow it.

Diagnosis: Instruction buried too deep in prompt (line 500+).

Fix: Move to top 50 lines with visual separator.

Pattern 3: Field Path Confusion

Symptom: LLM updates wrong field or nesting level.

Diagnosis: Schema not documented explicitly.

Fix: Add INPUT/OUTPUT schemas with exact paths.

Pattern 4: Intermittent Compliance

Symptom: Instruction followed sometimes, ignored other times.

Diagnosis: Prompt too long, LLM attention drifting.

Fix: Cut bloat to reduce prompt size by 30-50%.

Examples: Before and After

Example 1: Flag Clearing

Before (Failed):

### Character Creation Flag Management
(line 525 of 2064-line file)

If `character_creation_in_progress` is true, clear it when user is done.

After (Succeeded):

## 🎭 CRITICAL: Character Creation Flag - CLEAR IT NOW
(line 22 of 2064-line file)

IF YOU SEE `custom_campaign_state.character_creation_in_progress` ANYWHERE, IT MUST BE FALSE.

NO EXCEPTIONS. NO JUDGMENT. FLAG MUST BE FALSE. ALWAYS.

Result: 100% compliance vs 0% compliance.

Example 2: Schema Documentation

Before (Ambiguous):

Update the character creation flag when done.

After (Explicit):

## Input Schema

{

"custom_campaign_state": {

"character_creation_in_progress": true/false ← CHECK THIS

}

}


## Output Schema

{

"state_updates": {

"custom_campaign_state": {

"character_creation_in_progress": false ← MUST SET THIS

}

}

}

Result: LLM knows exact field paths and structure.

Token Budget Calculation

Rough estimation (for planning):

Example Budget (200K token limit):

Red line: If system prompt >25K tokens (>2000 lines), cut bloat immediately.

Validation Testing

Test every system prompt change:

# Create test that validates instruction compliance
def test_critical_instruction():
    # Set up scenario where instruction should trigger
    game_state = {"custom_campaign_state": {"flag": True}}

    # Call LLM with prompt
    response = call_llm(prompt, game_state, user_input)

    # Verify compliance
    assert response["state_updates"]["custom_campaign_state"]["flag"] == False, \
        f"CRITICAL INSTRUCTION IGNORED: flag should be False, got {response}"

Run this test BEFORE merging prompt changes.

Real-World Case Study

Problem: CharacterCreationAgent not clearing character_creation_in_progress flag.

Symptoms:

Root Cause Analysis:

  1. ❌ Instruction at line 525 of 2064-line file (buried)
  2. ❌ Conditional language: "If flag is true, clear it"
  3. ❌ No explicit INPUT/OUTPUT schemas
  4. ❌ Prompt bloat: 1236 lines (included irrelevant mechanics)

Fixes Applied:

  1. βœ… Moved instruction to line 22 (top 50)
  2. βœ… Changed to unconditional: "FLAG MUST BE FALSE. ALWAYS."
  3. βœ… Added explicit INPUT/OUTPUT schemas
  4. βœ… Cut prompt to 607 lines (removed mechanics bloat)

Result: Test went from 0% β†’ 100% pass rate.

Summary

The Three Laws of Prompt Engineering:

  1. Be Unconditional: "MUST BE X. ALWAYS." beats "if Y, do X"
  2. Be Explicit: Document INPUT/OUTPUT schemas with exact field paths
  3. Be Concise: Shorter prompts = higher compliance

When in doubt: Make it shorter, simpler, and more aggressive.

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.