Agentic AI - Writing Your First LangGraph Agent
Julian Kaljuvee
Agentic AI / ML Engineering @Microsoft, Ex-quant (Goldman, JPMorgan, LSEG, UBS)│ Alternative Data and Gen AI
The year of 2025 is likely to be the year of Agentic AI, and one of the key new skills in the job market will be the ability create and (fine) tune this new class of software components.
There are many Agentic AI approaches out there, but one of the leaders in the race appears to be LangGraph, a powerful framework for building stateful, multi-step AI applications. In this post, we'll break down how to create a conversational search agent that can perform web searches and temperature conversions using LangGraph, LangChain, and Claude. And all this in about 100 lines of code! You can find he source code of the agent here.
Agent in Action
We will implement a simple agent that has access to web search and temperature conversions and will answer the question:
What is the weather in New York today? Convert it to Celsius if it's in Fahrenheit.
Our agent responds with an elaborate Chain of Thought:
AI: [{'citations': None, 'text': 'Let me search for the current weather in New York.', 'type': 'text'}, {'id': 'toolu_011S8Dp7vDCj5xDLG4p2f8Wc', 'input': {'query': 'current weather new york city'}, 'name': 'web_search', 'type': 'tool_use'}]
TOOL: Current conditions at New York City, Central Park (KNYC) Lat: 40.78°NLon: 73.97°WElev: 154ft ... Local Forecast Office More Local Wx 3 Day History Hourly Weather Forecast. Extended Forecast for New York NY . Winter Weather Advisory February 6, 04:00am until February 6, 11:00am ... National Weather Service; New York, NY; 175
Brookhaven Avenue ... Current conditions at New York City, Central Park (KNYC) Lat: 40.78°NLon: 73.97°WElev: 154ft. New York City Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for the New York City area. In New York, at the moment, weather readings indicate a clear and cloudless sky. The temperature is a frosty 42.8°F (6°C), and the felt air temperature is evaluated at a cold 39.2°F (4°C). The current temperature is almost at the minimum-expected 41°F (5°C) for today. Your local forecast office is. New York, NY. News Headlines. Become a Weather Observer for the NWS!
We need observers in Mattituck and Carmel, NY. Current conditions at New York City, Central Park (KNYC) Lat: 40.78°NLon: 73.97°WElev: 154ft. Fair. 27°F-3°C. Humidity: 58%: ... National Weather Service; New York, NY; 175 Brookhaven Avenue ...
AI: [{'citations': None, 'text': 'Based on the search results, the current temperature in New York is 42.8°F. Let me convert that to Celsius for you.', 'type': 'text'}, {'id': 'toolu_01WfGfzxKrHXPtmYoh1jcFWc', 'input': {'temp_str': '42.8°F', 'to_unit': 'Celsius'}, 'name': 'convert_temperature', 'type': 'tool_use'}]
TOOL: 6.0°C
AI: The current temperature in New York is 42.8°F (6.0°C). The weather is clear and cloudless, though it feels slightly colder with a wind chill of 39.2°F (4°C).
Let us have a look at the detailed steps and the Chain of Thought to accomplish this.
Setting Up the Tools
First, we define two essential tools our agent will use:
2. Web Search: Using DuckDuckGo's search API through LangChain's DuckDuckGoSearchRun tool:
@tool
def web_search(query: str) -> str:
"""Search the web for information about a query."""
search = DuckDuckGoSearchRun()
return search.run(query)
2. Temperature Converter: A custom tool for converting between Fahrenheit and Celsius:
@tool
def convert_temperature(temp_str: str, to_unit: str) -> str:
"""Convert temperature between Fahrenheit and Celsius."""
# Implementation details...
The Graph Architecture
LangGraph uses a state machine approach where each node represents a different stage in our conversation flow. An overview of the entire graph is below.
Figure - Agent Graph Visualisation
Here's how we structure it:
1. State Definition
We use MessagesState to maintain the conversation history and state. This keeps track of all messages exchanged between the user and the agent.
2. Node Types
Our graph has two main types of nodes:
- Agent Node: Handles the LLM (Claude) interactions
- Tool Node: Executes the tools when needed
3. Routing Logic
The should_continue function determines whether to:
- Continue using tools based on the agent's response
- End the conversation when no more tool calls are needed
领英推è
def should_continue(state: MessagesState) -> Literal["tools", END]:
messages = state['messages']
last_message = messages[-1]
if last_message.tool_calls:
return "tools"
return END
Building the Workflow
The workflow is constructed using StateGraph, which connects all components:
- Adding Nodes:
workflow = StateGraph(MessagesState)
workflow.add_node("agent", call_model)
workflow.add_node("tools", tool_node)
2. Defining Edges:
workflow.add_edge(START, "agent")
workflow.add_conditional_edges(
"agent",
should_continue,
{
"tools": "tools",
END: END
}
)
workflow.add_edge("tools", "agent")
This creates a cycle where:
- The agent processes the user's input
- If tools are needed, they're executed
- Results return to the agent for further processing
- The cycle continues until no more tools are needed
The Conversation Flow
Let's break down how a conversation flows through this system:
- User sends a message: "What is the weather in New York today? Convert it to Celsius if it's in Fahrenheit."
- The agent (Claude) processes this request and determines it needs to: Search for current weather information Potentially convert the temperature
- The routing logic directs the flow to the appropriate tool based on the agent's decision
- Results from the tools are fed back to the agent, which can then: Process the search results Convert temperatures as needed Formulate a natural response
Memory and State Management
The system uses MemorySaver for maintaining state between interactions:
checkpointer = MemorySaver()
app = workflow.compile(checkpointer=checkpointer)
This ensures the conversation context is preserved throughout the interaction.
Final Comments
This implementation showcases the power of LangGraph in creating structured, stateful conversations with AI. The graph-based approach provides:
- Clear separation of concerns between different components
- Maintainable and extensible architecture
- Natural conversation flow with tool integration
By combining Claude's capabilities with custom tools and LangGraph's state management, we create a system that can handle complex, multi-step interactions while maintaining context and coherence.
References
- LangGraph Cookbooks - a great collection of Python Notebooks covering a wide range of use cases.
- Source Code - collection of other agentic framework examples on my Github.