Prompt task contracts
What is a prompt task contract?
A prompt is input to a generative model. A task contract is a more precise description of what the application asks the model to do, what information it may use, and what result counts as acceptable. It is an engineering agreement, not a guarantee that a model will follow it. For a fictional bookstore, “Summarize this policy” leaves the audience, length and missing-fact behavior unclear. “Produce exactly two bullets: return window and receipt requirement. Use only the policy. Write unknown for any missing fact.” gives the model and the evaluator a common target.
This lesson assumes the preceding chapter on context windows and factual limits. No model or account is required. If the policy already has structured fields, ordinary code is often the more reliable choice.
Separate the task from its input
A reusable template stores fixed instructions independently from changing input. State the task, relevant context, output shape, allowed evidence, constraints, and acceptance criteria. In the example, the policy is stored under input; the task and constraints are fixed by the application. This is a local data structure, not a provider API payload. A provider adapter would have to map those fields into its actual supported message roles. JSON serialization keeps data fields intact but does not stop a model from treating text as an instruction.
Build a request without a model
import json
def make_request(policy): return { "task": "Summarize the return policy in two bullets.", "constraints": ["Use only the supplied policy facts.", "Say unknown when a fact is missing."], "input": {"policy": policy}, }
request = make_request("Returns within 14 days. Receipt required.")print(json.dumps(request, sort_keys=True))Zero-shot and few-shot prompting
Zero-shot prompting gives the task without in-context demonstrations. Few-shot prompting includes a small number of worked input/output examples in addition to the task. For the bookstore contract, a zero-shot prompt specifies the two bullets and missing-fact rule. A few-shot prompt adds a demonstration such as Input: Returns in 7 days. Output: Return window: 7 days; Receipt: unknown (render the two fields as separate bullets in the actual prompt). A second example can show Receipt not required without a return deadline. Use different cases for evaluation; demonstrations are not held-out tests. Examples should match the written format and constraints, cover meaningful variation, and not leak private data. More demonstrations consume context and can introduce contradictions. Few-shot prompting is not weight training or a guarantee of higher accuracy.
Define abstention and ambiguity
Abstention means declining to assert a fact that is unsupported. In our fixed contract, missing days means unknown, not zero days; missing receipt information is not equivalent to False. If two equally authoritative policy versions conflict, report the conflict or request the correct version rather than invent a resolution. If the requested audience or task is unclear and would change the answer, ask a focused clarification. A known missing field can instead be reported immediately. Define which constraints are strict (two bullets, no invented facts) and which are preferences (plain language). If a format and a truthfulness requirement conflict, preserve truthfulness and escalate rather than guess.
Specify a deterministic reference result
def return_summary(days, receipt): if days is not None and (type(days) is not int or days < 0): raise ValueError("days must be a nonnegative integer or None") if receipt is not None and type(receipt) is not bool: raise ValueError("receipt must be a boolean or None") window = "unknown" if days is None else str(days) + " days" required = {True: "required", False: "not required", None: "unknown"} return ["Return window: " + window, "Receipt: " + required[receipt]]
print(return_summary(14, True))print(return_summary(None, False))Read the result and choose a simpler approach
The first call produces a 14-day window and a required receipt. The second keeps the unknown deadline separate from the explicitly false receipt requirement. Invalid inputs are rejected rather than silently converted. This function is a deterministic renderer for already extracted facts, not an extractor or language model. If all inputs arrive in this shape, the renderer may replace model generation entirely. For unstructured policy text, a model can propose extracted facts, but a separate reviewer or evidence-checking process must verify them before publishing.
Common mistake: add examples that say 30 days to a policy whose actual window is 14 days. The demonstrations describe the output format, not a default policy. Another mistake is treating a request template as a security control. Keep access controls in ordinary application code. Practice: write a task contract for a fictional library loan rule with a missing renewal limit; define exactly how to report it. Next, test the contract across held-out cases.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Prompt task contracts quiz
Apply the contract and evaluation concepts to fictional policy records.
Which instruction defines the clearest acceptance criteria for a policy summary?
What distinguishes few-shot prompting from zero-shot prompting?
What does return_summary(0, None) return, shown as two fields?
Why store fixed instructions separately from the changing policy text?
When is the deterministic return_summary renderer a suitable alternative to an LLM?