Introduction to prompt engineering

What is a prompt?

A prompt is everything the model sees: system policy, the user question, retrieved docs, chat history, and tool results. The model will not infer success criteria you never wrote. OpenAI treats prompting as both craft and engineering: the same model, a clearer contract, a better output.

Prompt engineering is not spellcasting. It is writing a reviewable contract: who is speaking, what to deliver, what is forbidden, and which shape to return. Anthropic stresses be clear and direct. OpenAI stresses role in the system message, task and examples in the user message, and treating production prompts as code—git, PRs, evals.

You imagine:   intent in your head  ──magic──►  a good answer
What happens:  intent → contract → model completion → you (or a parser) accept it

Why this still matters in the agent era

Cursor, Claude Code, LangChain, and Dify all give the model tools. Tools stop the model from being “voice only.” They do not lower the bar for the contract:

Without toolsWith tools
The prompt describes the whole jobThe prompt describes goal, bounds, and when to use which tool
Output is often an essayOutput may be tool calls plus a final reply
A bad turn is another chat messageA bad turn can edit the wrong file or hit the wrong API

This site’s agent courses assume you can already state a task. This primer fills that layer. See Tools and agents.

flowchart LR
  I[Intent] --> C[Contract: role/task/constraints/format]
  C --> M[Model]
  M --> T{Tools?}
  T -->|Yes| E[Tool results as data]
  E --> M
  T -->|No| O[Format the output]
  O --> V[Human review or eval]

What a contract usually contains

BlockQuestion it answersAnti-pattern
RoleWho is speaking, to whom“You are a helpful assistant” (empty)
TaskWhat this turn must deliver“Take a look” (no verb + object)
ConstraintsForbidden moves, scope, tone, lengthHoping the model “gets it”
Output formatMarkdown / JSON / table / patch only“Whatever” then the parser dies
ContextSource text, files, retrieved chunksPasting the whole repo
ExamplesWhat good output looks likeAdjective piles (“professional, friendly, thorough”)

The four core blocks live in Four building blocks. Splitting long text is in Structure and delimiters.


What this course covers — and what it does not

Covers:

  • Turning a vague line into an executable contract
  • Using XML / Markdown / delimiters so instructions and source material stay distinct
  • Using a few examples to lock format and edge cases
  • Using a schema so downstream code can ingest the result
  • Regression with gold questions instead of one “feels fine” glance

Does not cover:

  • Jailbreaks, bypassing safety policy, or prompt-injection attack steps
  • Using prompts to impersonate people or evade review
  • Hidden system prompts or undocumented model tricks
  • Longer boilerplate as a substitute for a clearer task

Vendor docs move; this course sticks to stable engineering habits. Model names follow each product’s UI. Examples here use common placeholders such as gpt-4.1-mini.


Chat exploration vs production prompts

Throwaway chat is fine for exploration. If you reuse, share with a team, or run in CI, treat the prompt as source that changes product behavior. OpenAI recommends named modules, typed fill-ins, and the same PR as the feature. A Dify prompt box, Cursor rules, and LangChain system_prompt are different containers for that source.

One line: write the contract first, then pick the container.


Next steps

评论