Prompt engineering

From oyi77/1ai-skills · View interactive version

Prompt engineering is an AI agent skill from the GitHub repository oyi77/1ai-skills. Use when improving system prompts, agent instructions, tool descriptions, or reusable prompt templates. Defines a measurable baseline → diagnose → change one variable → evaluate → keep/revert loop for quality, reliability, cost, and latency.

How to use this skill

Skill file

Prompt Engineering & Optimization

Treat prompts as versioned software. Do not optimize by taste alone: define success, preserve a baseline, change one thing at a time, measure, and keep or revert. Prompts should make the desired behavior easier to produce—not demand hidden reasoning or add ritual verbosity.

When to Use

When NOT to Use

The Prompt Contract

A production prompt should answer these questions, in this order:

  1. Role/scope: what the model is responsible for—and what it must defer.
  2. Task: one clear action, not a vague aspiration.
  3. Inputs: what is authoritative, what may be missing, and how to treat untrusted text.
  4. Constraints: safety, permissions, boundaries, and non-goals.
  5. Procedure: only the necessary sequence and decision branches.
  6. Output contract: schema, format, length, citations, or acceptance criteria.
  7. Failure behavior: ask, abstain, report uncertainty, or return a typed error when blocked.

Use the smallest prompt that reliably enforces the contract. Separate durable instructions from per-request data. Put untrusted user documents in clearly delimited data blocks; never let quoted content override system/developer policy.

Workflow: Measure Before You Tune

1. Build a baseline fixture set

Collect 5–20 representative inputs: ordinary cases, edge cases, known failures, and adversarial/conflicting inputs. Preserve the original outputs. Include realistic inputs; avoid synthetic-only tests.

For each case define mechanically checkable criteria where possible (schema valid, required facts present, prohibited action absent). Use a rubric only for genuinely subjective qualities such as tone or usefulness.

{
  "id": "missing-citation",
  "input": "Summarize this excerpt: [source text]",
  "criteria": [
    {"type": "contains", "value": "uncertain"},
    {"type": "not_contains", "value": "invented statistic"},
    {"type": "schema", "required": ["summary", "evidence", "unknowns"]}
  ]
}

For the harness and pass@k method, use meta/eval-harness. Do not claim an optimization worked without baseline and candidate measurements on the same cases.

2. Classify the failure before editing

| Symptom | Likely cause | First fix to try |

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

| Wrong task / scope drift | Task statement or role unclear | State one task + non-goals |

| Missing fields / malformed JSON | Output contract unclear | Use structured output/schema validation rather than more prose |

| Invented facts | Evidence boundary unclear | Require source-grounded claims; separate unknowns |

| Tool not called / wrong tool | Tool description, trigger, or routing ambiguity | Make tool purpose + invocation conditions explicit |

| Inconsistent quality | Criteria underspecified or examples too narrow | Add representative fixtures; clarify rubric |

| Prompt too long / slow | Duplicated or irrelevant instructions | Remove repetition; split durable context from task data |

| Injection succeeds | Untrusted content treated as instruction | Delimit data, state precedence, test hostile quoted inputs |

Do not rewrite the whole prompt because one fixture failed. Trace the failed criterion to the smallest prompt segment or tool contract that could cause it.

3. Change one variable

Examples of a single variable: task verb, output schema, one example, one conflict rule, one tool description. Keep the prompt version and eval set pinned. Do not simultaneously change model, sampling/configuration, tools, and prompt; attribution becomes impossible.

4. Evaluate candidate against baseline

Run baseline and candidate on identical fixtures. For stochastic workloads run repeated trials (typically k≥5) and report both pass@1 and consistency (cons@k). Compare:

Prefer a deterministic grader. If using an LLM judge, pin the judge/model and rubric; calibrate against a small human-scored sample. Never allow the candidate prompt to grade itself.

5. Keep or revert

Keep only if the target metric improves and no critical regression appears on protected cases. If quality ties, prefer shorter, clearer, cheaper prompts. Record prompt version, eval version, results, and the reason for keeping/reverting.

Prompt Patterns

Few-shot examples

Reasoning and explanation

Do not request hidden chain-of-thought. Ask for the observable work product: a concise rationale, cited evidence, checked assumptions, or a verification checklist. For multi-step tasks, specify checkpoints and intermediate artifacts—not private reasoning transcripts.

Structured output

Prefer JSON Schema, a typed tool, or a parser with validation over “return valid JSON” prose. Validate at the boundary; on invalid output, return a concise validation error and retry only with a bounded repair policy.

Tool descriptions

State: what the tool does, when to call it, required inputs, side effects, and failure cases. Avoid duplicate tools with overlapping descriptions. Test selection with positive examples, negative examples, and near-boundary tasks.

Prompt caching

Keep stable system instructions and tool definitions at the front; place volatile user/task data last. Avoid timestamps, random IDs, unsorted serialization, or per-request details inside cacheable prefixes. Measure cache-read tokens rather than assuming a hit. See provider-specific cache guidance in the relevant API skill.

Prompt Injection & Trust Boundaries

Anti-Rationalization Table

| Rationalization | Reality |

|---|---|

| “This version feels clearer.” | Feeling is not a metric. Run the fixture set. |

| “Add more rules so it cannot fail.” | Repetition and conflicting rules make behavior worse. Fix the diagnosed cause. |

| “Ask it to think step by step.” | Hidden reasoning is not a product contract. Specify evidence, checks, and the deliverable. |

| “One successful run proves it.” | Stochastic outputs vary. Measure pass@k and cons@k on representative cases. |

| “The prompt caused the failure.” | Tool, data, model, and harness failures look similar. Diagnose before editing prompt text. |

| “Shorter is always better.” | Shorten only while measured task quality and safety hold. |

Verification

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.