Back to roadmaps langchain Course

Project: AI Travel Planner with Weather API Tool

In this project, we will build an AI Travel Planner Agent. The agent accepts a vacation destination query, calls a custom weather API tool to determine the destination temperature, and structures a travel itinerary recommending clothing based on the weather.


1. Defining the Weather Tool

Use the tool function helper from @langchain/core/tools to declare the weather query action:

// src/tools/weatherTool.ts
import { tool } from "@langchain/core/tools";
import { z } from "zod";

export const fetchWeatherTool = tool(
  async (argumentsObj) => {
    const city = argumentsObj.cityLocation.toLowerCase();
    
    // Mocking external weather API lookups
    if (city === "tokyo") {
      return JSON.stringify({ temp: 15, condition: "rainy" });
    } else if (city === "miami") {
      return JSON.stringify({ temp: 32, condition: "sunny" });
    }
    
    return JSON.stringify({ temp: 22, condition: "cloudy" });
  },
  {
    name: "fetchWeather",
    description: "Lookup current temperature and weather conditions for a city.",
    schema: z.object({
      cityLocation: z.string().describe("The city name (e.g. Miami, Tokyo)."),
    }),
  }
);

2. Implementing the Travel Planner Agent

Connect the tool to the LLM agent executor:

// src/services/travelAgent.ts
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { fetchWeatherTool } from "../tools/weatherTool";
import { createToolCallingAgent, AgentExecutor } from "langchain/agents";

export async function generateTravelPlan(city: string) {
  const model = new ChatOpenAI({
    modelName: "gpt-4o-mini",
    temperature: 0.2,
  });

  const toolsList = [fetchWeatherTool];

  // 1. Setup system prompts explaining agent rules
  const prompt = ChatPromptTemplate.fromMessages([
    [
      "system",
      "You are a travel compiler. Use tools to check the weather of the target city first, and then suggest activities."
    ],
    ["placeholder", "{chat_history}"],
    ["human", "{input}"],
    ["placeholder", "{agent_scratchpad}"]
  ]);

  // 2. Instantiate the tool calling agent
  const agent = createToolCallingAgent({
    llm: model,
    tools: toolsList,
    prompt: prompt,
  });

  // 3. Create the execution runtime
  const executor = new AgentExecutor({
    agent: agent,
    tools: toolsList,
  });

  // 4. Run executor
  const response = await executor.invoke({
    input: `Plan a 1-day trip itinerary to ${city}.`,
  });

  console.log("Agent output details:", response.output);
  return response.output;
}

3. Execution Verification

Call the travel planner function:

// Run planner for Tokyo (which is mock configured as rainy)
const itinerary = await generateTravelPlan("Tokyo");

Review the output in your logs:

  1. The agent notices that to plan activities, it needs the weather.
  2. It triggers the fetchWeather tool passing { cityLocation: "Tokyo" }.
  3. It receives the mock result { temp: 15, condition: "rainy" }.
  4. It compiles a travel guide focusing on indoor activities (like museum visits) and advises bringing an umbrella.
Published on Last updated: