Skip to Content
ExamplesAgentsAgent System Prompt

Agent System Prompt

When building AI agents, you need to define their role, personality, and capabilities. A system prompt (provided via instructions) acts as the agent’s foundational identity. It tells the agent who it is and how it should behave. Unlike user messages that ask what to do, the system prompt defines the agent’s expertise, tone, and approach to all interactions.

Prerequisites

This example uses the openai model. Make sure to add OPENAI_API_KEY to your .env file.

.env
OPENAI_API_KEY=<your-api-key>

Creating an agent

Create a simple agent that returns facts about a city.

src/mastra/agents/example-city-agent.ts
import { openai } from "@ai-sdk/openai"; import { Agent } from "@mastra/core/agent"; export const cityAgent = new Agent({ name: "city-agent", description: "Create facts for a city", instructions: "You are an expert in geography and travel. When given the name of a city, respond with one interesting, lesser-known fact about that city. Keep the response concise and factual.", model: openai("gpt-4o") });

See Agent for a full list of configuration options.

Registering an agent

To use an agent, register it in your main Mastra instance.

src/mastra/index.ts
import { Mastra } from "@mastra/core/mastra"; import { cityAgent } from "./agents/example-city-agent"; export const mastra = new Mastra({ // ... agents: { cityAgent } });

Example usage

Use getAgent() to retrieve a reference to the agent, then call generate() with a prompt.

import "dotenv/config"; const agent = mastra.getAgent("cityAgent"); const result = await agent.generate("Create an interesting fact about London"); console.log(result.text);
View Example on GitHub