Chapter 8
Controlling Agent Behavior with System Prompts
IN THIS CHAPTER
Introducing the system prompt and how it works
Identifying why the tools JSON alone isn’t enough
Adding a prompt to your MCP server so that the client can fetch it automatically
Utilizing call limits to manage agent loops
Fixing the system prompt if it’s ignored by the model
In Chapter 6, I show you how to build a genuinely impressive creation: a Python client that connects Ollama to an MCP server, lets the model decide which tools to call, and hands back a final answer — all automatically. The agent loop in client2.py is doing real work.
But you may have noticed something odd: The agent doesn’t always behave the way you’d expect. Ask it a vague question, and it might call the same tool twice. Give it a question it can’t answer, and it might make up a reply rather than admit it doesn’t know. Ask about the weather and the holidays, and it might be confused about which tool to use first.
None of this indicates ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access