LangChain BasicsLesson 6

Prompt Engineering Techniques

Templates give you reusable structure. This lesson is about the text inside — audience, topic detail, and rules that keep the printed reply short and on point. We compare a vague ask with a clear one on the same HTML topic from Prompt Templates.

What it means here

Prompt engineering is not a separate library — it is how you write the strings you pass to invoke. Small additions (who the reader is, which tag to cover, how long the answer should be) often matter more than changing the model name.

Add a system line

SystemMessage sets the reader level and reply format before the question.

Write for Class 10 students. Keep each reply to one short sentence.

Be specific

Name the exact topic instead of a broad ask.

Explain the HTML <a> tag.

Set output rules

Length, format, and must-include facts keep the printed line on target.

Mention that it creates a hyperlink.

Keep temperature low

temperature=0 for study notes and repeatable demos.

ChatOpenAI(..., temperature=0)

Small wording changes in your messages — no new LangChain classes required.

Vague vs clear

Both calls below use the same ChatOpenAI setup with gpt-4o-mini and temperature=0. Only the message wording changes.

Vague

Tell me about the HTML <a> tag.

No role, no length limit, no required detail — reply may ramble or miss the key fact.

Clear

system

Write for Class 10 students. Keep each reply to one short sentence.

human

Explain the HTML <a> tag.
Rules: mention that it creates a hyperlink.

Same ChatOpenAI call, same topic — the clear version adds audience, topic detail, and output rules.

The demo script

prompt_engineering_demo.py prints two replies for the <a> tag so you can see the difference in your own terminal.

prompt_engineering_demo.py
"""prompt_engineering_demo.py — vague vs clear instructions"""
… imports, load_dotenv(), llm = ChatOpenAI(temperature=0) …
# Call 1 — one short human message
"Tell me about the HTML <a> tag."
# Call 2 — system line + rules in human message
SystemMessage(content="Write for Class 10 students…")
HumanMessage(content="Explain… Rules: mention hyperlink…")
Save as prompt_engineering_demo.py — included in the starter ZIP from Project Setup.

Run it

python prompt_engineering_demo.py
PowerShell — (.venv) active
(.venv) PS C:\projects\langchain-course> python prompt_engineering_demo.py
--- vague ---
The HTML <a> tag defines a hyperlink. You use the href attribute to set the destination URL. The link text goes between the opening and closing tags. It is one of the most common elements on the web…
--- clear ---
The <a> tag marks a hyperlink to another page or file.
The clear reply is shorter and includes the required fact — hyperlink — in one sentence.

Checklist before you invoke

QuestionIf no, add it
Did I add a system line?SystemMessage with reader level or format rules
Is the topic named clearly?Exact tag, chapter, or task — not "tell me about HTML"
Did I set length or format?One sentence, bullet list, table, etc.
Do I need a steady answer?temperature=0 (see Model Parameters)

Further reading: OpenAI prompt engineering guide.

What's Next

Clear system lines and rules change what prints. Next: wire steps together with LCEL.