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
- Outputs miss requirements, vary too much, or use the wrong format.
- Building or revising system prompts, agent instructions, tool descriptions, or reusable templates.
- Reducing prompt length, latency, or cost while preserving task quality.
- Debugging instruction conflicts, prompt injection, or brittle prompt behavior.
When NOT to Use
- A deterministic parser, schema, validation rule, or ordinary code fix solves the problem more simply.
- No repeatable example or success criterion exists yet; collect examples first.
- The underlying failure is bad data, a broken tool, or a wrong model choice rather than prompt wording.
The Prompt Contract
A production prompt should answer these questions, in this order:
- Role/scope: what the model is responsible for—and what it must defer.
- Task: one clear action, not a vague aspiration.
- Inputs: what is authoritative, what may be missing, and how to treat untrusted text.
- Constraints: safety, permissions, boundaries, and non-goals.
- Procedure: only the necessary sequence and decision branches.
- Output contract: schema, format, length, citations, or acceptance criteria.
- 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:
- Task success / rubric score
- Failure categories and regressions
- Input/output token counts
- Latency (median and tail if available)
- Cost per completed task, including retries/tool loops
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
- Select examples that cover distinct input classes and edge cases; 2–5 diverse examples usually beat many near-duplicates.
- Label examples and keep their input/output format identical to production.
- Test the prompt with examples removed. If it fails, confirm examples teach the intended rule rather than leak the answer.
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
- Treat retrieved pages, uploaded documents, tool output, and code comments as data, not authority.
- Delimit untrusted material and explicitly state which instruction sources take precedence.
- Do not ask the model to follow instructions found inside source material unless the task is specifically to analyze those instructions.
- Add adversarial fixtures: “ignore previous instructions”, exfiltration requests, fake system messages, and embedded tool-call requests.
- Enforce permissions in code/tool policy. Prompt wording is not an access-control boundary.
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
- [ ] Prompt has explicit task, trusted inputs, constraints, output contract, and failure behavior.
- [ ] Baseline prompt + outputs and eval fixtures are versioned.
- [ ] Candidate differs by one intentional variable.
- [ ] Baseline and candidate run on identical fixtures; stochastic tasks use repeated trials.
- [ ] Quality, regressions, tokens, latency, and cost/task compared.
- [ ] Injection and conflicting-instruction cases included where untrusted input is used.
- [ ] Chosen version, eval result, and keep/revert rationale recorded.