Prompt engineering

2 min read
On trails:AGCTXstep 1/11

Prompt engineering is engineering the instruction given to the model: who it is, what it may do, and what its answer must look like. It is the smallest layer of an agent, and every layer above it (context engineeringContext engineeringDeciding what the model sees on every call, because the window is finite.Opens in a new tab, flow engineeringFlow engineeringDesign the task as a flow of steps: a model where judgement is needed, plain code everywhere else.Opens in a new tab) still leans on it being right. On its own it is tactic, not strategy, and fragile (see why agentic patternsWhy agentic patternsA prompt is a tactic and breaks easily. A pattern is the strategy around it.Opens in a new tab).

What goes in

PartWhat it does
Role and scopeWho the model is, and where its job ends.
Structured instructionsSeparate parts, not one paragraph. Tags keep instructions, context and examples apart.
Output formatThe exact shape of the reply, so code can read it.
Examples for hard decisionsA few varied cases where words alone are ambiguous.
Reusable (DRY)Shared rules live in one place, not copied between prompts.
ParameterizedProduct name, limits and examples are variables filled per request.
Versioned and evaluatedA prompt change is a code change: it has a version and a test.

In LangChain

A prompt template keeps the fixed parts in one place and the changing parts as variables. Here role and scope sit in their own tags, the output format is spelled out so the reply can be parsed, and {product}, {rules} and {examples} are filled per request. The doubled {{ braces are literal braces. The name carries a version on purpose.

Python
from langchain_core.prompts import ChatPromptTemplate SUPPORT_PROMPT_V3 = ChatPromptTemplate.from_messages([    ("system",     "<role>You are a support agent for {product}.</role>\n"     "<scope>Answer billing questions only. Escalate anything else.</scope>\n"     "<rules>\n{rules}\n</rules>\n"     "<output_format>At most {max_sentences} sentences, then one line: "     '{{"action": "answer"}} or {{"action": "escalate"}}</output_format>\n'     "<examples>\n{examples}\n</examples>"),    ("human", "{question}"),])

Where it matters

Claude’s docs describe the model as a brilliant new employee who lacks context on your norms: the more precisely the job is explained, the better the result. Examples are one of the most reliable ways to steer format and tone, and the same docs suggest three to five that are relevant and varied.

A prompt is code nobody compiles, so it needs the habits code gets. A prompt change that is not evaluated is a guess. Anthropic’s own order is success criteria first, a way to test them second, and only then a draft to improve (see agent evaluationAgent evaluationTest the flow an agent took and what it left behind, against the goal and the guardrails.Opens in a new tab).


Sources