Building a Tiny LangGraph Agent with a Tool: A Weather-Aware Trip Planner

Building a Tiny LangGraph Agent with a Tool: A Weather-Aware Trip Planner

Most agent demos are either too simple (toy calculators) or too complex (enterprise-scale pipelines).
Here’s a middle ground: a 5-node LangGraph agent that builds a half-day itinerary for a city, adapts to weather, respects user preferences, and calls a real tool along the way.


Why this example?

  • Small, visualizable graph: plan → weather → search → pick → draft → finalize

  • Conditional routing: rainy → indoor picks, sunny → outdoor picks

  • Real tool usage: random_weather is a LangChain tool, called explicitly from the graph

  • Typed state with Pydantic v2

  • Optional LLM for nicer itinerary text (but not required), this example used OpenAI

  • Two runnable scenarios: London (rainy) and Barcelona (sunny)


Architecture

  • Nodes

    • plan: log initial context

    • weather: calls random_weather tool → Weather state

    • search: load city places

    • pick: filter by weather & prefs (budget, dislikes)

    • draft: generate itinerary text

    • fallback: relax prefs if nothing chosen

    • finalize: wrap up

  • Edges

    • Normal path: plan → weather → search → pick → draft → finalize

    • Fallback path: pick → fallback → draft → finalize


The Weather Tool

from langchain_core.tools import tool import random @tool def random_weather(city: str) -> dict: """ Return randomized weather for the city. """ conditions = ["sunny", "cloudy", "rain", "storm"] cond = random.choice(conditions) low, high = (12, 30) return { "city": city, "condition": cond, "high_c": float(random.randint(20, high)), "low_c": float(random.randint(low, 19)) }

And in the weather_node:

def weather_node(s: GraphState) -> GraphState: w = random_weather.invoke({"city": s.city}) # tool call s.weather = Weather(**w) s.audit.append( f"weather: {s.weather.city} {s.weather.condition} " f"{s.weather.high_c}/{s.weather.low_c}°C" ) return s


Example Run

Scenario A: London, dislikes museums, free-only budget

=== Scenario A: London + dislikes=['museum'], free_only === [Node] plan [Node] weather [Tool] random_weather(London) -> {'city': 'London', 'condition': 'sunny', 'high_c': 30.0, 'low_c': 28.0} [Node] search [Node] pick [Node] draft [Node] finalize plan: city=London, half_day=morning, date=2025-08-31 weather: London sunny 30.0/28.0°C search: 5 candidates pick: Thames Riverside Walk, Hyde Park Loop, Sky Garden draft: itinerary ready finalize: done Itinerary: - **Time:** 9:00 AM - 10:00 AM - **Tip:** Begin your day with a refreshing stroll along the river. Enjoy the views and the morning sun. Bring water and wear sunscreen! - **Transport:** Take the Tube from Embankment Station to Lancaster Gate (Circle or District Line). - **Tip:** This journey takes about 15 minutes. Consider grabbing a coffee from a nearby café before heading to the park. - **Time:** 10:15 AM - 11:30 AM - **Tip:** Enjoy the beautiful greenery and perhaps find a shaded spot to relax. Look out for the Serpentine Lake and the Diana Memorial Fountain. - **Transport:** Take the Tube from Lancaster Gate to Monument Station (Circle or District Line). - **Tip:** Allocate about 30 minutes for travel and check-in. Arrive by 11:45 AM to enjoy the stunning views of London. - **Time:** 12:00 PM - 12:45 PM - **Tip:** Enjoy a drink or snack at the café while taking in panoramic views of the city. Make sure to book your free entry in advance to avoid long waits! Notes: Weather: sunny



Scenario B: Barcelona, no dislikes, mixed budget

=== Scenario B: Barcelona + no dislikes, mixed budget === [Node] plan [Node] weather [Tool] random_weather(Barcelona) -> {'city': 'Barcelona', 'condition': 'sunny', 'high_c': 29.0, 'low_c': 26.0} [Node] search [Node] pick [Node] draft [Node] finalize plan: city=Barcelona, half_day=afternoon, date=2025-08-31 weather: Barcelona sunny 29.0/26.0°C search: 5 candidates pick: La Sagrada Família (Exterior), Gothic Quarter Stroll, Park Güell draft: itinerary ready finalize: done Itinerary: - **Start at La Sagrada Família (3:00 PM - 3:45 PM)** - Arrive early to admire the stunning exterior of this iconic basilica. Take photos and enjoy the intricate details of Gaudí's masterpiece. - **Gothic Quarter Stroll (4:00 PM - 5:00 PM)** - Take a short taxi or tram ride (about 10-15 minutes) to the Gothic Quarter. Wander through the narrow streets, explore hidden squares, and admire the medieval architecture. - **Park Güell (5:15 PM - 6:45 PM)** - Head to Park Güell via taxi or public transport (approximately 15-20 minutes). Enjoy a leisurely walk through the park, taking in Gaudí's colorful mosaics and panoramic views of the city. - **Transport:** Use taxis or rideshare apps for quick transfers between locations, especially in the heat. - **Hydration:** Bring water to stay hydrated, as temperatures can be high. - **Footwear:** Wear comfortable shoes for walking on cobblestone streets and park paths. Notes: Weather: sunny



Why this matters

  • Graph, not guesses: deterministic orchestration

  • Tools in action: explicit .invoke() calls with visible output

  • Policy in code: dislikes, budget filters, weather routing

  • Verbose logs: you can follow the agent step by step


Pros & Cons

Pros

  • Compact, easy-to-follow

  • Demonstrates both nodes and tools

  • Works with or without an LLM

  • Great for teaching graph concepts

Cons

  • Weather is mocked/randomized

  • Places are static, not from real APIs

  • No travel-time optimization


How to run

python trip_agent.py
  • Scenario A: London, free-only, dislikes museums → rainy picks

  • Scenario B: Barcelona, mixed budget → sunny picks


Comments

Popular Posts