Blog

Multi-Agent AI Travel Planning with Mastra

Jan 28, 2025

We recently built a demo showing how multiple AI agents can work together to plan travel itineraries.

The system takes user preferences and returns a complete travel package with flights, accommodations, and activities.

Implementation

The system uses two specialized agents:

  1. A primary travel agent that coordinates the planning process and interfaces with travel APIs
  2. An analyzer agent that validates and formats the data

Here's how we implemented the primary travel agent:

import { anthropic } from "@ai-sdk/anthropic";

export const travelAgent = new Agent({
  name: "travelAgent",
  instructions: `You are an expert travel agent responsible for finding a flight, hotel, and three attractions for a user. You will be given a set of user preferences along with some tools and you will need to find the best options for them. Be as concise as possible with your response.`,
  model: anthropic("claude-3-5-sonnet-20241022"),
  tools: {
    searchFlights,
    searchHotels,
    searchAttractions,
    searchAirbnbLocation,
    searchAirbnb,
  },
});

The agent receives structured input including:

  • Departure and arrival locations
  • Trip goals and interests
  • Flight preferences and priorities
  • Accommodation type and price range
  • Travel dates

Here's the prompt that guides the agent's decision-making:

const message = `
  You are a travel agent and have been given the following information about a customer's trip requirements.

  - Find the best flight option for the customer (use departureLocation and arrivalLocation)
  - Find the best accommodation option (use arrivalCityId)
  - Find three activities based on their interests (use arrivalAttractionId)
  - Find the best return flight option
  - For Airbnb stays, search location then listings (use searchAirbnbLocation then searchAirbnb)

  Notes:
  - Include layover information when present
  - Add images for hotels and accommodations
  - flightPriority ranges 0-100 (0: prioritize price, 100: prioritize convenience)
  - Use complete timestamps for departure/arrival times
  - Return complete flight objects
  - Only call relevant accommodation search based on user preference

  Trip Requirements:
  Departure: ${formObject.departureLocation}
  Arrival: ${formObject.arrivalLocation}
  Goals: ${formObject.tripGoals}
  ...
`;

The analyzer agent then validates and formats the data:

import { anthropic } from "@ai-sdk/anthropic";

export const travelAnalyzer = new Agent({
  name: "travel-analyzer",
  instructions:
    "You are an expert travel agent responsible for finding a flight, hotel, and three attractions for a user. You will be given a set of user preferences along with some data to find the best options for them.",
  model: anthropic("claude-3-5-sonnet-20240620"),
});

This agent also gets a fairly detailed prompt:

const messageToAnalyze = `
  You are a travel agent analyzing research results.

  Format the response according to the output schema for the travel planner.

  For hotel ratings:
  - Extract numeric rating from description/accessibilityLabel
  - Rating format: "X.X out of 5 stars" or "X out of 5 stars"
  - Use only first number (before "out of")
  - Rating must be ≤ 5
  - Include layover details in flight legs
  - Replace <UNKNOWN> values with empty strings

  ${JSON.stringify(data)}
`;

Type Safety

The system uses TypeScript and Zod schemas throughout:

const FlightSearchSchema = z.object({
  budget: z.number(),
  departure: z.string(),
  destination: z.string(),
  dates: z.object({
    start: z.date(),
    end: z.date(),
  }),
});

This ensures data consistency between agents and external APIs.

The code is available on GitHub, and you can try it at mastra-eight.vercel.app.

Share

Stay up to date