ExamplesAgentsGiving a System Prompt

Giving an Agent a System Prompt

When building AI agents, you often need to give them specific instructions and capabilities to handle specialized tasks effectively. System prompts allow you to define an agent’s personality, knowledge domain, and behavioral guidelines. This example shows how to create an AI agent with custom instructions and integrate it with a dedicated tool for retrieving verified information.

import { Agent, createTool } from "@mastra/core";
 
import { z } from "zod";
 
const instructions = `You are a helpful cat expert assistant. When discussing cats, you should always include an interesting cat fact.
 
  Your main responsibilities:
  1. Answer questions about cats
  2. Use the catFact tool to provide verified cat facts
  3. Incorporate the cat facts naturally into your responses
 
  Always use the catFact tool at least once in your responses to ensure accuracy.`;
 
const getCatFact = async () => {
  const { fact } = (await fetch("https://catfact.ninja/fact").then((res) =>
    res.json(),
  )) as {
    fact: string;
  };
 
  return fact;
};
 
const catFact = createTool({
  id: "Get cat facts",
  inputSchema: z.object({}),
  description: "Fetches cat facts",
  execute: async () => {
    console.log("using tool to fetch cat fact");
    return {
      catFact: await getCatFact(),
    };
  },
});
 
const catOne = new Agent({
  name: "cat-one",
  instructions: instructions,
  model: {
    provider: "OPEN_AI",
    name: "gpt-4o",
    toolChoice: "required",
  },
  tools: {
    catFact,
  },
});
 
const result = await catOne.generate("Tell me a cat fact");
 
console.log(result.text);





View Example on GitHub

MIT 2025 © Nextra.