DocsHow toCreating Agents

Creating Agents

Agents are systems where the language model chooses a sequence of actions.

Where the LLM class is more similar to a contractor you might hire for a one-off task, agents are more like employees who can be used for ongoing projects. They have names, a memory you can persist, and a model configuration and instructions that will be consistent across calls, as well as a set of enabled tools (functions they can call).

Agent Configuration

Here’s a simple example of creating an agent, registering it with Mastra, and then using it to generate text.

Let’s start by creating a simple agent that can help you cook meals.

import { Agent } from '@mastra/core';
 
export const chefAgent = new Agent({
  name: 'Stock Agent',
  instructions: 'You are Michel, a practical and experienced home chef who helps people cook great meals with whatever ingredients they have available. Your first priority is understanding what ingredients and equipment the user has access to, then suggesting achievable recipes. You explain cooking steps clearly and offer substitutions when needed, maintaining a friendly and encouraging tone throughout.',
  model: {
    provider: 'OPEN_AI',
    name: 'gpt-4o',
    toolChoice: 'auto'
  },
});
 
// When you register an agent with Mastra, they get access to the tools, integrations, and logger.
 
export const mastra = new Mastra<typeof tools>({
  agents: [chefAgent],
});

Generating and streaming text

Now we can use the agent to generate text.

async function main() {
  // Query 1: Basic pantry ingredients
 
  const query1 = "In my kitchen I have: pasta, canned tomatoes, garlic, olive oil, and some dried herbs (basil and oregano). What can I make?";
 
  console.log({`Query: ${query1}`});
  const pastaResponse = await chefAgent.text({
    messages: [{
      content: query1
    }]
  });
  console.log('\n👨‍🍳 Chef Michel:', pastaResponse.text);
  console.log('\n-------------------\n');
}
 
main()

Or, if you want a streaming responses:

 
  const query2 = "Now I'm over at my friend's house, and they have: chicken thighs, coconut milk, sweet potatoes, and some curry powder.";
 
  const stream = await chefAgent.stream({
    messages: [{ content: query2 }],
    onStepFinish: (step) => {
      // Log each step as it completes
      process.stdout.write(step);
    },
    onFinish: async (result) => {
      // Log when the entire response is complete
      console.log('\n\n✅ Recipe complete!');
    }
  });
 
  // Handle the stream
  for await (const chunk of stream) {
    // Write each chunk without a newline to create a continuous stream
    process.stdout.write(chunk);
  }

In the next section, we’ll look at how to add tools to the agent.