Building a LangGraph + ReAct Agent with LangSmith for Tracking & Tool-Aware Reasoning
Summary
Recently, I explored LangGraph with LangSmith tracking and LangChain Groq to build an agent capable of performing arithmetic operations and answering knowledge-based questions. By integrating ReAct-style reasoning and tool invocation, the agent can solve multi-step tasks while maintaining detailed tracking of its decisions in LangSmith.
Recently, I explored LangGraph with LangSmith tracking and LangChain Groq to build an agent capable of performing arithmetic operations and answering knowledge-based questions. By integrating ReAct-style reasoning and tool invocation, the agent can solve multi-step tasks while maintaining detailed tracking of its decisions in LangSmith.
Key Concepts
LangGraph: A framework for building graph-based agents where nodes represent computations or tools, and edges define control flow.
ReAct Agents: Agents that combine reasoning with actions (like invoking tools) iteratively to solve complex tasks.
LangChain Groq: LLM interface that allows binding tools for enhanced reasoning and multi-step decision making.
Example ImplementationFirst, we load our environment variables and initialize the LLM:
GROQ_API_KEY="" LANGCHAIN_API_KEY="" LANGSMITH_TRACING="true" LANGSMITH_ENDPOINT="https://api.smith.langchain.com"import os from dotenv import load_dotenv from langchain_groq import ChatGroq os.environ["LANGSMITH_PROJECT"] = "AgenticAIworkspace" load_dotenv() groq_key = os.getenv("GROQ_API_KEY") llm = ChatGroq(api_key=groq_key, model="llama-3.1-8b-instant")Next, we define some tools for arithmetic:
# Define a simple tool def multiply(a: int, b: int) -> int: """Multiply a and b. Args: a: first int b: second int """ return a * b # This will be a tool def add(a: int, b: int) -> int: """Adds a and b. Args: a: first int b: second int """ return a + b def divide(a: int, b: int) -> float: """Adds a and b. Args: a: first int b: second int """ return a / bWe bind the tools to the LLM:
tools = [add, multiply, divide] llm_with_tools = llm.bind_tools(tools)Then, we define a stateful agent using LangGraph:
from langgraph.graph import StateGraph, START, END from langgraph.prebuilt import ToolNode, tools_condition from langchain_core.messages import HumanMessage, SystemMessage, AnyMessage from typing_extensions import TypedDict from typing import Annotated from langgraph.graph.message import add_messages class State(TypedDict): messages: Annotated[list[AnyMessage], add_messages] sys_message = SystemMessage(content="You are a helpful assistant tasked with performing arithmetic on a set of inputs.") def assistant(state: State): return {"messages": [llm_with_tools.invoke([sys_message] + state["messages"])]}We then build the graph:
builder = StateGraph(State) builder.add_node("assistant", assistant) builder.add_node("tools", ToolNode(tools)) builder.add_edge(START, "assistant") builder.add_conditional_edges("assistant", tools_condition) builder.add_edge("tools", "assistant") # loop back for further reasoning builder.add_edge("assistant", END) graph = builder.compile()Finally, we provide input messages and invoke the agent:
input_messages = [HumanMessage(content="Add 3 and 4. Multiply the output by 2. Divide the output by 5 and what is machine learning (ML) and LLM?")] result = graph.invoke({"messages": input_messages}) for msg in result["messages"]: print(msg)How It Works
The assistant node processes messages and decides which tool to call.
The ToolNode executes arithmetic operations.
The output loops back to the assistant for further reasoning.
ReAct-style reasoning allows the agent to answer knowledge questions while performing calculations.
This setup showcases the power of combining reasoning with tool usage in LangGraph, allowing agents to handle both structured tasks like math and open-ended queries like explaining ML and LLM concepts.
Additional practical notes
Why LangGraph and LangSmith matter
LangGraph is useful when an AI agent needs repeatable steps, tool calls, memory, and conditional routing instead of a single prompt-response flow. LangSmith adds observability so developers can inspect traces, prompts, tool calls, latency, and failures during real agent runs.
Production checklist
Before shipping a LangGraph ReAct agent, log every tool input and output, validate tool parameters, add retry rules for transient API errors, and keep a fallback path for failed reasoning steps. This makes debugging easier when the agent behaves differently from the expected flow.
Practical use cases
This pattern works well for support agents, internal developer assistants, data lookup bots, workflow automation, and research tools where the agent must choose between multiple tools and explain the result.
Frequently asked questions
Is LangGraph only for complex agents?
No. It is also helpful for small agents when you want predictable state transitions, traceable decisions, and easier debugging.
Why use LangSmith with LangGraph?
LangSmith shows the trace behind each run, including prompts, model responses, tool calls, timing, and errors, which makes agent debugging much faster.