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
: callsrandom_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
And in the weather_node
:
Example Run
Scenario A: London, dislikes museums, free-only budget
Scenario B: Barcelona, no dislikes, mixed budget
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
-
Scenario A: London, free-only, dislikes museums → rainy picks
-
Scenario B: Barcelona, mixed budget → sunny picks
Comments
Post a Comment