In the evolving world of AI, retrieval-augmented generation (RAG) systems have become standard for handling straightforward queries and generating contextually relevant responses. However, as demand grows for more sophisticated AI applications, there is a need for systems that move beyond simple retrieval tasks. Enter AI agents—autonomous entities capable of executing complex, multi-step processes, maintaining state across interactions, and dynamically adapting to new information. LangGraph, a powerful extension of the LangChain library, is designed to help developers build these advanced AI agents, enabling stateful, multi-actor applications with cyclic computation capabilities. AI Assistants Using LangGraph.
In this insight, we’ll explore how LangGraph revolutionizes AI development and provide a step-by-step guide to building your own AI agent using an example that computes energy savings for solar panels. This example will demonstrate how LangGraph’s unique features enable the creation of intelligent, adaptable, and practical AI systems.
What is LangGraph?
LangGraph is an advanced library built on top of LangChain, designed to extend Large Language Model (LLM) applications by introducing cyclic computational capabilities. While LangChain allows for the creation of Directed Acyclic Graphs (DAGs) for linear workflows, LangGraph enhances this by enabling the addition of cycles—essential for developing agent-like behaviors. These cycles allow LLMs to continuously loop through processes, making decisions dynamically based on evolving inputs.
LangGraph: Nodes, States, and Edges
The core of LangGraph lies in its stateful graph structure:
- State: This represents the context or memory that is maintained and updated as the computation progresses. It ensures each step in the graph can access relevant information from prior steps, facilitating dynamic decision-making based on accumulated data.
- Nodes: These are the building blocks of the graph, representing individual computation steps or functions. Each node performs a specific task, such as processing inputs, making decisions, or interacting with external systems.
- Edges: Edges connect the nodes and define the flow of computation. They support conditional logic, allowing the execution path to change based on the current state, enabling complex, multi-step workflows.
LangGraph redefines AI development by managing the graph structure, state, and coordination, allowing for the creation of sophisticated, multi-actor applications. With automatic state management and precise agent coordination, LangGraph facilitates innovative workflows while minimizing technical complexity. Its flexibility enables the development of high-performance applications, and its scalability ensures robust and reliable systems, even at the enterprise level.
Step-by-step Guide
Now that we understand LangGraph’s capabilities, let’s dive into a practical example. We’ll build an AI agent that calculates potential energy savings for solar panels based on user input. This agent can function as a lead generation tool on a solar panel seller’s website, providing personalized savings estimates based on key data like monthly electricity costs. This example highlights how LangGraph can automate complex tasks and deliver business value.
Step 1: Import Necessary Libraries
We start by importing the essential Python libraries and modules for the project.
pythonCopy codefrom langchain_core.tools import tool
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import Runnable
from langchain_aws import ChatBedrock
import boto3
from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph.message import AnyMessage, add_messages
from langchain_core.messages import ToolMessage
from langchain_core.runnables import RunnableLambda
from langgraph.prebuilt import ToolNode
Step 2: Define the Tool for Calculating Solar Savings
Next, we define a tool to calculate potential energy savings based on the user’s monthly electricity cost.
pythonCopy code@tool
def compute_savings(monthly_cost: float) -> float:
"""
Tool to compute the potential savings when switching to solar energy based on the user's monthly electricity cost.
Args:
monthly_cost (float): The user's current monthly electricity cost.
Returns:
dict: A dictionary containing:
- 'number_of_panels': The estimated number of solar panels required.
- 'installation_cost': The estimated installation cost.
- 'net_savings_10_years': The net savings over 10 years after installation costs.
"""
def calculate_solar_savings(monthly_cost):
cost_per_kWh = 0.28
cost_per_watt = 1.50
sunlight_hours_per_day = 3.5
panel_wattage = 350
system_lifetime_years = 10
monthly_consumption_kWh = monthly_cost / cost_per_kWh
daily_energy_production = monthly_consumption_kWh / 30
system_size_kW = daily_energy_production / sunlight_hours_per_day
number_of_panels = system_size_kW * 1000 / panel_wattage
installation_cost = system_size_kW * 1000 * cost_per_watt
annual_savings = monthly_cost * 12
total_savings_10_years = annual_savings * system_lifetime_years
net_savings = total_savings_10_years - installation_cost
return {
"number_of_panels": round(number_of_panels),
"installation_cost": round(installation_cost, 2),
"net_savings_10_years": round(net_savings, 2)
}
return calculate_solar_savings(monthly_cost)
Step 3: Set Up State Management and Error Handling
We define utilities to manage state and handle errors during tool execution.
pythonCopy codedef handle_tool_error(state) -> dict:
error = state.get("error")
tool_calls = state["messages"][-1].tool_calls
return {
"messages": [
ToolMessage(
content=f"Error: {repr(error)}n please fix your mistakes.",
tool_call_id=tc["id"],
)
for tc in tool_calls
]
}
def create_tool_node_with_fallback(tools: list) -> dict:
return ToolNode(tools).with_fallbacks(
[RunnableLambda(handle_tool_error)],
exception_key="error"
)
Step 4: Define the State and Assistant Class
We create the state management class and the assistant responsible for interacting with users.
pythonCopy codeclass State(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]
class Assistant:
def __init__(self, runnable: Runnable):
self.runnable = runnable
def __call__(self, state: State):
while True:
result = self.runnable.invoke(state)
if not result.tool_calls and (
not result.content
or isinstance(result.content, list)
and not result.content[0].get("text")
):
messages = state["messages"] + [("user", "Respond with a real output.")]
state = {**state, "messages": messages}
else:
break
return {"messages": result}
Step 5: Set Up the LLM with AWS Bedrock
We configure AWS Bedrock to enable advanced LLM capabilities.
pythonCopy codedef get_bedrock_client(region):
return boto3.client("bedrock-runtime", region_name=region)
def create_bedrock_llm(client):
return ChatBedrock(model_id='anthropic.claude-3-sonnet-20240229-v1:0', client=client, model_kwargs={'temperature': 0}, region_name='us-east-1')
llm = create_bedrock_llm(get_bedrock_client(region='us-east-1'))
Step 6: Define the Assistant’s Workflow
We create a template and bind the tools to the assistant’s workflow.
pythonCopy codeprimary_assistant_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
'''You are a helpful customer support assistant for Solar Panels Belgium.
Get the following information from the user:
- monthly electricity cost
Ask for clarification if necessary.
''',
),
("placeholder", "{messages}"),
]
)
part_1_tools = [compute_savings]
part_1_assistant_runnable = primary_assistant_prompt | llm.bind_tools(part_1_tools)
Step 7: Build the Graph Structure
We define nodes and edges for managing the AI assistant’s conversation flow.
pythonCopy codebuilder = StateGraph(State)
builder.add_node("assistant", Assistant(part_1_assistant_runnable))
builder.add_node("tools", create_tool_node_with_fallback(part_1_tools))
builder.add_edge(START, "assistant")
builder.add_conditional_edges("assistant", tools_condition)
builder.add_edge("tools", "assistant")
memory = MemorySaver()
graph = builder.compile(checkpointer=memory)
Step 8: Running the Assistant
The assistant can now be run through its graph structure to interact with users.
python import uuid
tutorial_questions = [
'hey',
'can you calculate my energy saving',
"my montly cost is $100, what will I save"
]
thread_id = str(uuid.uuid4())
config = {"configurable": {"thread_id": thread_id}}
_printed = set()
for question in tutorial_questions:
events = graph.stream({"messages": ("user", question)}, config, stream_mode="values")
for event in events:
_print_event(event, _printed)
Conclusion
By following these steps, you can create AI Assistants Using LangGraph to calculate solar panel savings based on user input. This tutorial demonstrates how LangGraph empowers developers to create intelligent, adaptable systems capable of handling complex tasks efficiently. Whether your application is in customer support, energy management, or other domains, LangGraph provides the
🔔🔔 Follow us on LinkedIn 🔔🔔