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.
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_agentSYSTEM = """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 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.