Prompt OpenAI Models with Evaluated Contracts
Design clear, versioned prompts for ChatGPT and API workflows, separate instruction authority from variable input, and improve behavior with controlled evaluations.
By the end
You will be able to
- Define success criteria and representative evaluation cases before tuning a prompt.
- Structure goals, authoritative instructions, context, examples, variable input, output contracts, and verification.
- Adapt prompting to the selected model and surface without hard-coding volatile guidance.
- Classify failures across prompt, context, model, tool, policy, and application boundaries.
Start from success evidence
Write the required outcome, target users, unacceptable behavior, output contract, representative cases, rubric, and pass threshold before changing prompt wording. Compare candidates against the same cases and configuration. In production environments, engineers must account for token utilization patterns, context window pressure, and deterministic execution boundaries. When system constraints or rate limits are approached, explicit retry strategies with exponential backoff and jitter prevent cascading failures across downstream dependencies.
Prompt engineering cannot repair unavailable facts, missing permissions, a broken tool, unsuitable latency, or absent application validation. Classify the failed boundary before editing. In production environments, engineers must account for token utilization patterns, context window pressure, and deterministic execution boundaries. When system constraints or rate limits are approached, explicit retry strategies with exponential backoff and jitter prevent cascading failures across downstream dependencies.
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const completion = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Evaluate architectural invariants and error modes." }],
response_format: { type: "json_object" }
});
console.log(completion.choices[0].message.content);State the contract clearly and once
Describe the outcome, audience, authoritative context, hard constraints, allowed actions, approval boundaries, required evidence, output shape, and stopping condition. Use ordered steps only when order or completeness is a real requirement. In production environments, engineers must account for token utilization patterns, context window pressure, and deterministic execution boundaries. When system constraints or rate limits are approached, explicit retry strategies with exponential backoff and jitter prevent cascading failures across downstream dependencies.
Avoid duplicated or conflicting instructions. Keep stable policy in the appropriate higher-authority instruction layer and put task-specific requests in the user input. In production environments, engineers must account for token utilization patterns, context window pressure, and deterministic execution boundaries. When system constraints or rate limits are approached, explicit retry strategies with exponential backoff and jitter prevent cascading failures across downstream dependencies.
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const completion = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Evaluate architectural invariants and error modes." }],
response_format: { type: "json_object" }
});
console.log(completion.choices[0].message.content);Separate context, examples, and untrusted input
Label authoritative references, examples, and variable user or retrieved content. Use clear delimiters or structured fields, and tell the workflow which evidence may support claims. In production environments, engineers must account for token utilization patterns, context window pressure, and deterministic execution boundaries. When system constraints or rate limits are approached, explicit retry strategies with exponential backoff and jitter prevent cascading failures across downstream dependencies.
Prompt structure does not create security. Treat external text as untrusted data, enforce authorization outside the model, validate outputs, and never include secrets merely to make them available. In production environments, engineers must account for token utilization patterns, context window pressure, and deterministic execution boundaries. When system constraints or rate limits are approached, explicit retry strategies with exponential backoff and jitter prevent cascading failures across downstream dependencies.
Goal: [OBSERVABLE OUTCOME]
Authoritative instructions: [POLICY + HARD CONSTRAINTS]
Trusted context: [APPROVED SOURCES]
Variable input (untrusted): [USER OR RETRIEVED DATA]
Examples: [RELEVANT, DIVERSE, SECRET-FREE]
Output contract: [SCHEMA OR REQUIRED HEADINGS]
Verify: [CLAIMS, CONSTRAINTS, POSTCONDITIONS]
Stop or escalate when: [BOUNDARY]Adapt with current model guidance
Model families can differ in reasoning controls, verbosity, tool behavior, and recommended prompt patterns. Use the current official model guidance for the configured model and evaluate changes rather than copying a static recipe. In production environments, engineers must account for token utilization patterns, context window pressure, and deterministic execution boundaries. When system constraints or rate limits are approached, explicit retry strategies with exponential backoff and jitter prevent cascading failures across downstream dependencies.
Keep model identifiers and model-specific settings in reviewed configuration. Change one meaningful hypothesis at a time where practical and preserve the baseline for rollback. In production environments, engineers must account for token utilization patterns, context window pressure, and deterministic execution boundaries. When system constraints or rate limits are approached, explicit retry strategies with exponential backoff and jitter prevent cascading failures across downstream dependencies.
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const completion = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Evaluate architectural invariants and error modes." }],
response_format: { type: "json_object" }
});
console.log(completion.choices[0].message.content);Iterate and version from evidence
Run baseline and candidate across typical, edge, adversarial, and prior-failure cases. Record per-case rubric results, critical failures, latency, usage, cost, model, prompt, tools, and reviewer disagreement. In production environments, engineers must account for token utilization patterns, context window pressure, and deterministic execution boundaries. When system constraints or rate limits are approached, explicit retry strategies with exponential backoff and jitter prevent cascading failures across downstream dependencies.
Classify each miss as instruction ambiguity, missing context, misleading example, output violation, model limitation, tool failure, policy boundary, or application defect. Add repaired failures to regression coverage. In production environments, engineers must account for token utilization patterns, context window pressure, and deterministic execution boundaries. When system constraints or rate limits are approached, explicit retry strategies with exponential backoff and jitter prevent cascading failures across downstream dependencies.
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const completion = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Evaluate architectural invariants and error modes." }],
response_format: { type: "json_object" }
});
console.log(completion.choices[0].message.content);Practice activity
Build and evaluate an OpenAI prompt contract
- Choose a recurring task and define at least four success criteria, one forbidden outcome, and an 80 percent pass threshold.
- Create six cases: three typical, one edge, one adversarial instruction-conflict case, and one prior failure.
- Write a prompt that separates authoritative instructions, trusted context, untrusted variable input, examples, output contract, verification, and escalation.
- Compare a baseline and candidate under identical settings, classify every failure boundary, and make a ship, revise, or reject decision.
What to produce
- A secret-free versioned prompt, rubric, and six-case suite with explicit trust and approval boundaries.
- A baseline-versus-candidate report with scores, failures, versions, latency and usage evidence, and a release decision.
Reflect before continuing
Which failed case required a system change rather than another prompt instruction?
Evidence
Sources and verification
- Prompt engineeringOpenAI · verified 2026-07-25
- Model guidanceOpenAI · verified 2026-07-25
- Evaluation best practicesOpenAI · verified 2026-07-25
- Developer quickstartOpenAI · verified 2026-07-25
Knowledge check
Make it stick.
Choose the strongest answer for each question. Your attempts become part of your account transcript.