AI Hints
Home›LangChain Tutorial›Prompts, Streaming, Memory
LangChain · Lesson 46 of 120

Agent System Prompts

Giving it a job.

⏱ 2 min read▶ Video at 50:44🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

An agent without instructions is a generic assistant. A system prompt gives it a job, a personality, and rules. Who it works for, what it should do, and what it must never do.

Agent System Prompts
From the video: Agent System Prompts
  • Generic by default
  • A job and rules
  • What it must never do

A system prompt

We pass a system prompt to create agent. It says the agent works for Riverside Books, answers in at most two sentences, and politely refuses questions that aren't about books or orders. Run it. The book question gets a short answer, and the off topic request is politely turned down.

system_prompt.pypython
from langchain.agents import create_agent

SYSTEM = """You are the assistant for Riverside Books, an online bookshop.
Answer in at most two sentences.
Only help with books and orders. Politely refuse anything else."""

agent = create_agent("google_genai:gemini-flash-lite-latest", tools=[], system_prompt=SYSTEM)
for q in ["Can you recommend a cosy mystery?", "Write my maths homework."]:
    out = agent.invoke({"messages": [{"role": "user", "content": q}]})
    print("Q:", q, "\nA:", out["messages"][-1].text, "\n")
Output
Q: Can you recommend a cosy mystery?
A: I recommend *The Thursday Murder Club* by Richard Osman, a charming and clever
mystery set in a peaceful retirement village. Let me know if you would like to place an
order for it!

Q: Write my maths homework.
A: I'm sorry, but I can only help with books and orders at Riverside Books. If you need
a textbook or study guide for your math class, I'd be happy to help you find one!

A good agent prompt

A good agent prompt has a few parts. The role, who it is and who it serves. The goal, what a good outcome looks like. The rules, like length, tone, and limits. And tool guidance, like, always check stock before promising a book is available.

keyvalue
Rolewho it is
Goalwhat success means
Ruleslength, tone, limits
Tool guidancewhen to use which

Summary

A system prompt gives an agent its role, goal, rules, and guidance on tools. Pass it to create agent. Next, watching an agent work, step by step.

Key takeaways
  • system_prompt=...
  • Role, goal, rules
  • Tool guidance
Next up: Streaming Agent Steps — watching it work