---
name: prompt-engineering
description: Structure and tune LLM prompts using battle-tested patterns. Use when writing new prompts, restructuring underperforming ones, or iterating on accuracy. Triggers on "write prompt", "fix prompt", "prompt not working", "LLM wrong", "false positives", "tune prompt", "restructure prompt", "prompt engineering".
license: MIT
title: prompt-engineering
canonical_url: https://skilld.dev/gh/ayushjj/skills/prompt-engineering
last_updated: 2026-10-26T13:15:43.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [references/output-template.md](https://skilld.dev/api/skills-raw/ayushjj/skills/prompt-engineering/references/output-template.md), [scripts/measure-accuracy.ts](https://skilld.dev/api/skills-raw/ayushjj/skills/prompt-engineering/scripts/measure-accuracy.ts).
>
> If the user asked to install this Skill, run `npx skilld install ayushjj/skills/prompt-engineering`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/prompt-engineering`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/ayushjj/skills/prompt-engineering) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/prompt-engineering`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/prompt-engineering --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/prompt-engineering --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Prompt Engineering

Two phases: **Structure** (get the architecture right) then **Tune** (measure and iterate). Use Phase 1 alone for new prompts. Use both phases when an existing prompt underperforms.

## Before You Start

- You have an LLM prompt to write, restructure, or tune.
- You know what the prompt should accomplish.
- You can measure or observe its results.

<critical_sequence enforce_order="strict">

<step number="0" required="true" blocking="true">
## Phase 1: Structure Audit

Before writing or changing a single word, audit the prompt's architecture against these patterns. For new prompts, use this as a construction checklist.

### Correct Section Order

LLMs attend most to the beginning and end. This ordering is non-negotiable:

```
1. ANCHOR — exact output format first ("Respond with...", "Return JSON with...")
2. HARD CONSTRAINTS — rules that must never be broken (safety, hallucination ban, char limits)
3. ROLE + BOUNDARY — who the LLM is + what to do when data is missing
4. FORMAT OPTIONS — if multiple formats exist, describe them with default bias
5. EXAMPLES — GOOD, GOOD (different case), BOUNDARY, BAD (in that order)
6. CONTEXT — conversation history, retrieved data (in user prompt, not system)
```

**Why this order:** Constraints after examples are "effectively dead" — the LLM has already formed its response pattern from examples and ignores trailing rules. (Pattern credit: Brady Long. Position = priority.)

### 7 Structural Rules

| # | Rule | Diagnostic Question |
|---|------|-------------------|
| 1 | **Anchor first** — open with exact output format, not role | Does the prompt open with "You are..." instead of output format? Move format up. |
| 2 | **Constraints before examples** — rules right after anchor | Are any rules listed AFTER the examples section? Move them up. |
| 3 | **7 rules max** — beyond 7, LLMs start ignoring or mis-prioritizing | Count the rules. If >7, combine or move deterministic ones to code. |
| 4 | **Concrete examples > abstract rules** — 2-4 diverse examples outperform 10+ rules | Are there rules that could be replaced by a single example? Replace. |
| 5 | **Every new power needs a boundary example** — "you may reject ALL" needs a companion showing when NOT to reject | Does the prompt grant a new capability without showing its limits? Add boundary example. |
| 6 | **Deterministic rules in code, not prompt** — if it has a deterministic answer, enforce in code | Can any rule be enforced with regex, char count, or code check? Remove from prompt, add to code. |
| 7 | **Explicitly permit zero/empty results** — LLMs default to "pick the best available" | Could the correct answer be "nothing matches"? If yes, the prompt must explicitly allow it. |

### Structure Audit Checklist

<validation_gate name="structure-audited" blocking="true">
Read the prompt (or draft it if new), then check every box:

- [ ] Section order matches the 6-section template above
- [ ] Hard constraints appear BEFORE examples
- [ ] Total rule count is 7 or fewer
- [ ] No rule that could be enforced in code instead
- [ ] Every example uses the SAME FORMAT the LLM sees in real data
- [ ] If prompt grants a power (reject, skip, abbreviate), a boundary example exists
- [ ] If zero/empty is a valid result, explicitly permitted

**Document what you moved or changed.** If the prompt was already well-structured, note that and proceed to Phase 2.
</validation_gate>
</step>

<step number="1" required="true" blocking="true">
## Phase 2: Measure and Tune

Skip this phase when writing a brand-new prompt — there is no baseline to compare against. Come
back when real output exists to evaluate.

Four moves: measure, try several things, choose on data, lock it in with tests.

1. **Test cases — at least 10.** Interface in `scripts/measure-accuracy.ts`. Cover 3+ clear
   positives, 3+ clear negatives, 3+ boundary/edge, every case that has failed in production,
   and mixed-language cases if the prompt sees them. Fewer than 10, or missing one of those
   categories, and the measurement below cannot distinguish a real gain from noise.

2. **Baseline.** Run all cases against the CURRENT prompt before changing anything. Record
   accuracy (N/M), each failed case with actual vs expected, and the failure *patterns* — the
   pattern is what tells you which of the four moves below to reach for.

3. **At least 3 approaches**, generated with Phase 1's structural rules. The high-impact ones:
   reorder only (constraints up, same words) · reduce to ≤7 rules · replace 3 abstract rules
   with 1 concrete example · add a boundary example where the limit is. Note each one's
   accuracy, strengths and weaknesses.

4. **Choose on the numbers, not on feel.** Compare approaches on accuracy, robustness, false
   positives and false negatives — the last two separately, because a prompt that trades one for
   the other can hold accuracy flat while getting worse for its actual job. State the trade-off
   you accepted.

5. **Every failed case becomes a regression test** in the project's suite. This is what stops
   the next tuning pass from silently re-breaking what this one fixed.
</step>

</critical_sequence>

## Quick Reference: Anti-Patterns

- **Rules at the bottom** — move constraints before examples
- **>7 rules** — combine or enforce in code
- **"Never do X" without showing what X looks like** — add a BAD example
- **"You may skip/reject" without showing when NOT to** — add boundary example
- **Tweaking words without measuring** — always measure before/after
- **Same example repeated with minor variations** — use 2-4 DIVERSE examples instead
- **Asking LLM to enforce char limits** — enforce in code (deterministic)
- **Context in system prompt** — put retrieved data in user prompt where it gets more attention

## Output Template

For the full output template, see `references/output-template.md`.

<success_criteria>
## Done When ALL Are True

**Phase 1 (Structure):**
- [ ] Section order follows the 6-section template
- [ ] Constraints before examples
- [ ] 7 or fewer rules
- [ ] Boundary examples for every granted power

**Phase 2 (Tune) — if applicable:**
- [ ] Minimum 10 test cases
- [ ] Baseline measured
- [ ] 3+ approaches tested
- [ ] Decision matrix with chosen approach justified
- [ ] Regression tests added
</success_criteria>

<decision_gate wait_for_user="true">
## Prompt Engineering Complete

Structure changes: [list what moved/changed]
Baseline accuracy: [X]% (if tuning)
New accuracy: [Y]% (if tuning)

What's next?
1. Deploy the prompt
2. Test with more cases
3. Try additional approaches
4. Apply to another prompt

Choose (1-4): _
</decision_gate>

<integration_protocol>
## Integration Points

**Invoked by:**
- Manual: `/prompt-engineering`
- Auto-suggest: when user mentions prompt issues, LLM wrong output, or "restructure prompt"

**Complements:**
- Post-deploy verification: executing-batch-operations → Query-correctness validation (expected behavior → real data → precision/recall)

**Handoff:**
Creates decision documentation in `specs/llm-tune-{feature}/`.
Phase 1 can run standalone for new prompts.
Phase 2 produces test cases + regression tests.
</integration_protocol>
