Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 31, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 31, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 31, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 31, 2025

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025

      I love Elden Ring Nightreign’s weirdest boss — he bargains with you, heals you, and throws tantrums if you ruin his meditation

      May 31, 2025

      How to install SteamOS on ROG Ally and Legion Go Windows gaming handhelds

      May 31, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Oracle Fusion new Product Management Landing Page and AI (25B)

      May 31, 2025
      Recent

      Oracle Fusion new Product Management Landing Page and AI (25B)

      May 31, 2025

      Filament Is Now Running Natively on Mobile

      May 31, 2025

      How Remix is shaking things up

      May 30, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025
      Recent

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025

      I love Elden Ring Nightreign’s weirdest boss — he bargains with you, heals you, and throws tantrums if you ruin his meditation

      May 31, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Machine Learning»Creating An AI Agent-Based System with LangGraph: A Beginner’s Guide

    Creating An AI Agent-Based System with LangGraph: A Beginner’s Guide

    January 30, 2025

    What is an Agent?

    An agent is a Large Language Model (LLM)-powered system that can decide its own workflow. Unlike traditional chatbots, which operate on a fixed path (ask → answer), agents are capable of:

    • Choosing between different actions based on context.
    • Using external tools such as web search, databases, or APIs.
    • Looping between steps for better problem-solving.

    This flexibility makes agents powerful for complex tasks like conducting research, analyzing data, or executing multi-step workflows.

    Key Components of Agents

    Understanding the building blocks of agents is crucial before diving into implementation. These components work together to create intelligent, adaptable workflows:

    Agent (LLM Core)

    At the heart of every agent lies the “brain” of the system—the LLM. It is responsible for:

    • Interpreting user inputs and understanding intent.
    • Making decisions about the next steps based on pre-defined prompts and available tools.

    For example, when a user asks a question, the LLM analyzes the input and decides whether it can answer directly or if additional steps (like a web search) are needed.

    Memory

    Memory enhances an agent’s ability to maintain context and learn over time:

    • Short-term memory: Keeps track of the current interaction or task’s progress. For instance, it remembers the user’s last question during a session.
    • Long-term memory: Stores information from past interactions to personalize responses or improve performance over time.

    This dual memory system allows agents to provide context-aware and consistent interactions.

    Tools

    Tools extend the agent’s functionality beyond simple text generation. These are external functions or APIs that the agent can call to perform specific tasks, such as:

    • Web search tools: Fetch the latest information from the internet.
    • Calculators: Solve complex mathematical problems.
    • APIs: Access services like search web, weather updates or stock data.

    Together, these components enable agents to operate dynamically and handle complex, multi-step workflows.

    What is LangGraph?

    LangGraph is a Python library designed to create stateful, multi-step AI workflows. It acts as a bridge between the agent’s components, allowing developers to orchestrate their interactions efficiently.

    What Does LangGraph Offer?

    LangGraph simplifies the process of building intelligent agents by providing tools to:

    • Build decision-making loops that guide the agent’s workflow.
    • Seamlessly connect LLMs to external tools for enhanced capabilities.
    • Manage shared memory and state, ensuring smooth transitions between steps.

    Key Concepts

    LangGraph relies on three main concepts to structure workflows:

    • Nodes: These are the fundamental units of work, such as calling an LLM or performing a web search.
    • Edges: Connections between nodes that define the sequence of operations. These can be linear (step-by-step) or conditional (based on specific criteria).
    • State: Shared data that flows through the workflow, keeping track of progress and context.

    By combining these elements, LangGraph provides a structured and flexible framework for designing complex AI workflows.

    Let’s Build a Simple Agent

    Step 1: Setup

    Start by installing the required packages:

    Copy CodeCopiedUse a different Browser
    pip install langgraph langchain-community langchain-core langchain-groq

    Next, obtain free API keys for the tools we’ll use:

    • Groq for LLM access.
    • Tavily for web search functionality.

    Set your environment variables to store the API keys securely:

    Copy CodeCopiedUse a different Browser
    import os
    os.environ['TAVILY_API_KEY'] = "your_tavily_key"
    os.environ['GROQ_API_KEY'] = "your_groq_key"

    Step 2: Basic Chatbot

    We’ll begin by creating a simple chatbot that uses Groq’s LLM to respond to user inputs.

    1. Import Dependencies :

    Copy CodeCopiedUse a different Browser
    from langgraph.graph import StateGraph, START, END, MessagesState
    from typing import TypedDict, Annotated
    import operator
    from langchain_groq import ChatGroq
    from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage, ToolMessage
    from langgraph.prebuilt import ToolNode

    2. Initialize LLM: We first need to initialize a LLM model which supports tools (e.g., Llama 3 series, GPT-4o etc). Then we need to create a function which will act as a node for invoking the LLM.

    Copy CodeCopiedUse a different Browser
    llm = ChatGroq(temperature=0, model="Llama-3.3-70b-Specdec")
    
    system_prompt = """You are a helpful chatbot. You can help users with their questions.You can also ask questions to clarify the user's intent. You can also provide information to the user."""
    
    def call_llm(state: AgentState):
        messages = state["messages"]
        messages = [SystemMessage(content=system_prompt)] + messages
        message = llm.invoke(messages)
        return {"messages" : [message]}

    3. Define AgentState: The Agent State will keep a list of all the messages so far. It will be updated at each node.

    Hostinger
    Copy CodeCopiedUse a different Browser
    class AgentState(TypedDict):
        messages: Annotated[list[AnyMessage], operator.add]

    4. Define workflow and create agent: Finally, we define the workflow of our agent using StateGraph.

    Copy CodeCopiedUse a different Browser
    # Build graph
    graph = StateGraph(AgentState)
    graph.add_node("llm", call_llm)
    graph.add_edge(START, "llm")
    graph.add_edge("llm", END)
    agent = graph.compile()
    
    # Visualize the graph
    from IPython.display import Image, display
    display(Image(agent.get_graph().draw_mermaid_png()))
    Copy CodeCopiedUse a different Browser
    # Test it
    response = agent.invoke({"messages": [HumanMessage(content="What is the capital of France?")]})
    print(response["messages"][-1].content)

    Output: The capital of France is Paris.

    Explanation:

    1. LLM Initialization: We configure Groq’s LLM to process user inputs. The temperature parameter controls response randomness.
    2. StateGraph: Tracks the flow of data (“state”) through the workflow.
    3. Chatbot Node: A single step that processes messages with the LLM.
    4. Invoke: Runs the workflow, taking the user’s input and returning the LLM’s response.

    Step 3: Add Web Search Tool

    Let’s enhance the agent by integrating a web search tool. We will use Tavily for this purpose.1. Define the Tool: The Tavily search tool will enable the agent to fetch real-time information from the internet given a query.

    Copy CodeCopiedUse a different Browser
    from langchain_community.tools.tavily_search import TavilySearchResults
    
    tool = TavilySearchResults(max_results=4) #increased number of results
    print(type(tool))
    print(tool.name)
    tools = [tool]

    2. Binding the tool with LLM: Binding tools to LLM simply means, we are letting the LLM know that it has access to the given tools and how it can use them.

    Copy CodeCopiedUse a different Browser
    model = llm.bind_tools(tools)
    tools_map = {tool.name: tool for tool in tools}
    
    def call_llm(state: AgentState):
        messages = state['messages']
        messages = [SystemMessage(content=prompt)] + messages
        message = model.invoke(messages)
        return {'messages': [message]}

    3. Enhanced Workflow: We’ll add a new node to handle tool usage. This node will check if the LLM has asked to use which tool with which arguments. The function take_action will then use those tools to get the answer and will revert back to LLM.

    Copy CodeCopiedUse a different Browser
    def take_action(state: AgentState):
        tool_calls = state['messages'][-1].tool_calls
        results = []
        for tool_call in tool_calls:
            print(f"Calling {tool_call['name']} with {tool_call['args']}")
            if not tool_call['name'] in tools_map:
                print("n....bad tool name....")
                result = "bad tool name, retry"
            else:
                result = tools_map[tool_call['name']].invoke(tool_call['args'])
            results.append(ToolMessage(tool_call_id=tool_call['id'], name=tool_call['name'], content=str(result)))
        print("Back to model!")
        return {"messages": results}
    
    graph = StateGraph(AgentState)
    graph.add_node("llm", call_llm)
    graph.add_node("action", take_action)

    4. Adding the Conditional Edge: This is one of the most crucial parts of the workflow. The conditional edge determines whether to use a tool or escape from an LLM response.

    Copy CodeCopiedUse a different Browser
    def route_action(state: AgentState):
        result = state['messages'][-1]
        return len(result.tool_calls) > 0
    
    graph.add_conditional_edges(
        "llm",
        route_action,
        {True: "action", False: END}
    )
    graph.add_edge("action", "llm")
    graph.set_entry_point("llm")
    graph = graph.compile()
    
    # Visualize the workflow
    from IPython.display import Image, display
    display(Image(graph.get_graph().draw_mermaid_png()))

    Explanation:

    1. Tool Binding: The LLM is connected to the Tavily search tool. This allows the LLM to perform web searches as part of its workflow.
    2. Conditional Routing: The workflow decides whether to use the tool based on the LLM’s response.
    3. Looping: After fetching results, the tool node sends the data back to the LLM for further processing.

    3. Test with Web Access Run the enhanced agent to see it in action:

    Copy CodeCopiedUse a different Browser
    messages = [HumanMessage(content="What is the weather in sf?")]
    result = graph.invoke({"messages": messages})
    result['messages'][-1].content

    Output: ‘The current weather in San Francisco is overcast with a temperature of 45.3°F (7.4°C) and a feels-like temperature of 44.2°F (6.8°C). The wind is blowing at 3.1 mph (5.0 kph) from the west, and the humidity is 87%. There is no precipitation, and the visibility is 9.0 miles (16.0 km).’

    Copy CodeCopiedUse a different Browser
    messages = [HumanMessage(content="Who won the super bowl in 2024? In what state is the winning team headquarters located? 
    What is the GDP of that state? Answer each question.")]
    result = graph.invoke({"messages": messages})
    result['messages'][-1].content

    Output: “The Kansas City Chiefs won the Super Bowl in 2024. The winning team’s headquarters is located in the state of Missouri. The GDP of Missouri is $331 billion.”

    Next Steps

    Now that you have a functional agent, consider expanding its capabilities:

    • Add More Tools: Include calculators, weather APIs, or database connectors.
    • Implement Memory: Store session-specific data for follow-up questions.

    Create Multi-Agent Systems: Use multiple specialized agents to tackle complex workflows.

    Congratulations!

    You’ve built an AI agent capable of:

    • Making decisions dynamically.
    • Using external tools for real-time information retrieval.
    • Refining its responses through iterative processing.

    Experiment with LangGraph to create your own intelligent agents tailored to specific tasks!

    Sources:

    1. (DeepLearning.ai)https://learn.deeplearning.ai/courses/ai-agents-in-langgraph

    Also, don’t forget to follow us on Twitter and join our Telegram Channel and LinkedIn Group. Don’t Forget to join our 70k+ ML SubReddit.

    🚨 Meet IntellAgent: An Open-Source Multi-Agent Framework to Evaluate Complex Conversational AI System (Promoted)

    The post Creating An AI Agent-Based System with LangGraph: A Beginner’s Guide appeared first on MarkTechPost.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleYuE: An Open-Source Music Generation AI Model Family Capable of Creating Full-Length Songs with Coherent Vocals, Instrumental Harmony, and Multi-Genre Creativity
    Next Article NVIDIA AI Releases Eagle2 Series Vision-Language Model: Achieving SOTA Results Across Various Multimodal Benchmarks

    Related Posts

    Machine Learning

    How to Evaluate Jailbreak Methods: A Case Study with the StrongREJECT Benchmark

    May 31, 2025
    Machine Learning

    Cisco’s Latest AI Agents Report Details the Transformative Impact of Agentic AI on Customer Experience

    May 31, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Evaluating potential cybersecurity threats of advanced AI

    Artificial Intelligence

    Robotic Vision in Debian: Mastering Image Processing and Object Recognition for Intelligent Robots

    Development

    GhostSec Announces Shift in Operations from Ransomware to Hacktivism

    Development

    Motion Highlights #3

    News & Updates

    Highlights

    Linux

    Rilasciata Archcraft 2025.04.24: la distribuzione GNU/Linux minimalista e moderna basata su Arch Linux

    April 26, 2025

    Archcraft è una distribuzione GNU/Linux leggera e minimalista, costruita sulla solida base di Arch Linux,…

    AI method radically speeds predictions of materials’ thermal properties

    July 27, 2024

    Microsoft hints at potential Windows 11 25H2 update-related changes

    March 25, 2025

    Forget Satya Nadella’s Windows Phone fumble, Bill Gates’ “greatest mistake of all time” cost Microsoft $400 billion

    April 2, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.