How to Build AI Agents with LangChain and LangGraph? A Practical Guide for Enterprises
Sep 2, 2026 Artificial Intelligence
Sep 2, 2026 Artificial Intelligence
Basic prompt wrappers hit hard when handling dynamic enterprise logic, leaving teams stuck with breakable scripts that fail under unpredictable input. By building AI agents with LangChain and LangGraph, you move beyond simple input-output chains to construct systems capable of dynamic tool routing, step recovery, and reliable production execution.
When developers first start working with agentic AI and other large language models, the standard pattern involves passing a text prompt into an API endpoint and receiving a response. In early framework versions, LangChain simplified this by organizing prompts, models, and output parsers into deterministic chains.
A deterministic chain follows a rigid route:
[Input Query] ---> [Prompt Template] ---> [LLM Call] ---> [Output Parser] ---> [Result]
While deterministic chains handle basic tasks like text transformation or single-pass summarization, they struggle with real-world business logic. If a database query returns an unexpected format, or if a third-party API returns a rate-limit error, a linear chain breaks.
To handle dynamic tasks, Xicom’s engineering experts build AI agents that rely on a language model as a decision-making engine. Given a goal, the model evaluates incoming data, selects external tools, inspects execution outputs, and loops dynamically until it completes the assigned task. Therefore, by establishing proper memory structures, strict tool schemas, and persistent state controls, we ensure your software remains scalable, secure, and closely aligned with business goals. Before directly moving towards the steps of building AI agents with LangChain, let us first understand the terminology.

LangChain is an open-source framework designed to simplify the construction of applications powered by large language models (LLMs). A language model, on its own, just takes text in and produces text out. It has no memory of a previous exchange, no way to look something up in a database, and no way beyond generating a response. LangChain exists to close that gap.
It gives developers a set of standard interfaces for connecting a model to documents, APIs, databases, other models, and a defined sequence of steps. Instead of writing custom glue code every time you want a model to pull data from a file before answering, or call an external tool mid-conversation, LangChain provides the abstractions for that. It provides it in a form that works consistently across model providers like OpenAI, Anthropic, or open source models running locally.
The open-source framework is constructed around a fairly simple idea. Most applications don’t always need a single call to a model, they need a sequence of calls, decisions, and data lookups strung together. LangChain calls a fixed sequence like this a chain. A chain might take a user’s question, retrieve relevant documents, insert them into a prompt, and pass that to the model, all as one defined pipeline. LangChain’s expression language (LCEL) does allow some conditional branching inside a chain, but the possible paths are still fixed by the developer ahead of time; the model itself isn’t deciding the route at runtime. That distinction is what separates a chain from an agent, covered below.
Let’s discuss the components of LangChain in detail. The LangChain framework is structured into several core, modular components. Each component below handles one task, and you combine only the ones your application actually needs.
Model Inputs/Outputs: The interface layer that maintains interactions across different LLM providers, such as OpenAI, Anthropic, or Hugging Face, or self-hosted open-source model families like Meta’s Llama. It manages Prompt Templates to structure dynamic inputs, Language Models to process those inputs, and Output Parsers to extract structured formats, like JSON, Pydantic objects, or SQL queries, from raw model responses.
Data Connection (Indexing & Retrieval): It is a suite of utilities built to interface LLMs with enterprise data. This component includes Document Loaders to ingest unstructured data from hundreds of sources, such as PDFs, Slack, databases, S3, Text Splitters to break large documents into semantically meaningful chunks, Vector Stores to manage high-dimensional embeddings, and Retrievers to query and fetch relevant context during inference.
Chains: A chain links multiple steps together into a single callable pipeline. A chain links prompts, models, and data transformers together. This allows developers to move beyond single-prompt interactions and create multi-step processing workflows where the output of one step becomes the input for the next.
Memory: The persistent storage mechanism that addresses the stateless nature of base LLM calls. Memory components store, summarize, and manage interaction histories—allowing applications to maintain context across multi-turn user conversations without overwhelming model token limits.
Agents & Tools: The dynamic orchestration engine of the framework. Unlike deterministic chains that follow a fixed path, Agents use an LLM as a reasoning driver to decide which actions to take and in what order. Tools are the functional capabilities granted to the agent, such as web search APIs, code execution sandboxes, or database connectors. The agent evaluates the current state, selects a tool, parses its output, and loops until it completes the goal. In current LangChain projects, this looping behavior is most often built with LangGraph (covered next) rather than the older standalone agent executor pattern.
Callbacks: Lastly, it is an observability and logging integration layer. Callbacks hook into every stage of the execution lifecycle, allowing developers to trace intermediate steps, track token consumption, measure latency, and debug complex multi-step chains in production environments.
Also Read: How to Build an AI Agent
LangGraph is an open-source library built by the LangChain team to extend the capabilities of language model applications from simple, linear pipelines into stateful, multi-actor, and cyclic graph structures. Standard chains process data in a straight line. An input flows through a prompt, hits an LLM, triggers a tool, and returns a response.
LangGraph processes the data differently. It does this by constructing application workflows as distinct state machines. In a LangGraph architecture, every step in a process is represented as a node, while the transitions between those steps are known as edges. Nodes read from and write to a shared, persistent state object that maintains context across the entire lifecycle of a task.
Cyclic Workflows: Unlike traditional Directed Acyclic Graphs (DAGs) where data can only move forward, LangGraph supports explicit execution loops. A node can execute repeatedly, such as an agent refining code or retrying a failed API call until specific pass/fail conditions are met.
Explicit State Management: All nodes interact with a centralized state schema (often defined using standard Python dictionaries or TypedDict models). Every state modification is transparent, predictable, and fully traceable, making debugging and auditing straightforward.
Built-in Persistence & Checkpointing: LangGraph automatically saves application state at every step using configurable checkpointers. If a server restarts, an external API drops out, or a process times out, execution can resume from the exact node where it stopped without re-running the entire workflow. (The step-by-step guide below includes a working checkpointer example.)
Human-in-the-Loop Interruption: Enterprise operations such as sending customer emails, approving refunds, or updating production databases often require human validation. LangGraph allows developers to set interrupt gates before specific nodes, pausing execution until a human operator reviews the current state, approves the action, or modifies the data before resuming.
Multi-Agent Coordination: Complex workflows can be broken down into specialized agents operating as nodes within a primary graph or as isolated sub-graphs. A supervisor graph can delegate work to sub-agents (such as a database query specialist and a compliance checking specialist) and aggregate their outputs back into a unified state.
To build reliable agents, it helps to understand what separates a linear chain from a dynamic agentic AI system. In early LangChain applications, workflows were organized as linear sequences where the output of one step became the immediate input for the next.
While linear chains are easy to write, they fail when edge cases occur. If an underlying database call times out or returns unexpected formatting, the entire chain halts. Agentic AI built with LangChain and LangGraph replaces hardcoded logic with a reasoning loop. The model acts as an orchestrator that evaluates incoming data against available capabilities before deciding on its next action.
An agentic application relies on four primary components:
The Reasoning Driver: The base LLM configured with structured output capabilities to process goals and decide on actions.
Tool Integrations: Standardized wrappers around external REST APIs, SQL databases, Python sandboxes, or search engines.
State & Memory Layer: Persistent storage that keeps track of past interactions, tool outputs, and execution history across multi-turn workflows.
The Execution Runtime: The surrounding loop engine that manages API calls, executes selected tools, catches runtime errors, and feeds updated data back to the model context.
Also Read: How to Build an Agentic AI Governance Framework
A chain typically runs the same predefined sequence regardless of input. LCEL supports some conditional branching, but every possible path is still defined in advance by the developer. An agent, by contrast, looks at the situation and decides its own path at runtime. It can call a tool, check the result, realize that wasn’t the right approach, and try something else. This is all done without a human directing each move.
That looping, decide-then-act behavior needs to come alive, and that’s what LangGraph provides. It represents an agent as a graph rather than a straight line. A set of connected steps where some paths are fixed, and others depend entirely on what the agent decides at that moment. AI agents in LangGraph can loop back, retry, and pick up exactly where they left off if something interrupts them midway, none of which a simple chain is built to do.
Let’s look at how to build a stateful, tool-calling agent using Python, LangChain, and LangGraph.
Tools allow the agent to interact with external systems. Defining strict Pydantic schemas ensures the language model passes correctly typed parameters.
from langchain_core.tools import tool
from pydantic import BaseModel, Field
class PatientQueryInput(BaseModel):
patient_id: str = Field(description="The unique alphanumeric identifier for the patient.")
category: str = Field(description="Data category to query: 'vitals', 'medications', or 'labs'.")
@tool("fetch_patient_records", args_schema=PatientQueryInput)
def fetch_patient_records(patient_id: str, category: str) -> str:
"""Queries the internal secure EHR database for patient metrics."""
# Simulated secure database lookup
data_store = {
"vitals": "Blood Pressure: 118/78 mmHg, Heart Rate: 70 bpm, Temp: 98.4F",
"medications": "Lisinopril 10mg daily, Metformin 500mg twice daily",
"labs": "HbA1c: 6.1%, Fasting Blood Glucose: 95 mg/dL"
}
return data_store.get(category, "No matching records found for specified category.")
tools = [fetch_patient_records]
The state schema defines the data structures passed between all nodes in the execution graph. Using message append operations ensures execution logs persist throughout the loop.
from typing import Annotated, Sequence
from typing_extensions import TypedDict
from langchain_core.messages import BaseMessage
from langgraph.graph.message import add_messages
class AgentState(TypedDict):
messages: Annotated[Sequence[BaseMessage], add_messages]
Bind the available tools directly to the language model and set up your functional node execution handlers.
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import ToolNode
# Initialize LLM with strict tool binding
llm = ChatOpenAI(model="gpt-4o", temperature=0).bind_tools(tools)
def call_reasoning_model(state: AgentState):
"""Evaluates input state and decides whether to output a final answer or trigger a tool."""
messages = state["messages"]
response = llm.invoke(messages)
return {"messages": [response]}
tool_execution_node = ToolNode(tools)
Construct the execution graph and define routing functions that govern state transitions.
from langgraph.graph import StateGraph, END
def determine_next_step(state: AgentState):
"""Routes execution based on the model's output."""
messages = state["messages"]
last_message = messages[-1]
# If the model requests a tool execution, route to the tool node
if last_message.tool_calls:
return "tools"
# Otherwise, end execution and return response
return END
# Initialize state graph
workflow = StateGraph(AgentState)
# Add operational nodes
workflow.add_node("agent", call_reasoning_model)
workflow.add_node("tools", tool_execution_node)
# Set entry point and conditional routes
workflow.set_entry_point("agent")
workflow.add_conditional_edges("agent", determine_next_step)
workflow.add_edge("tools", "agent")
A graph compiled without a checkpointer keeps no memory of a run once it ends if the process restarts mid-task, that execution is gone. Passing a checkpointer at compile time is what turns on the persistence LangGraph is built for, and it takes only a couple of lines:
from langgraph.checkpoint.memory import MemorySaver
# MemorySaver keeps state in-process — useful for development.
# In production, swap this for a persistent backend such as
# PostgresSaver or RedisSaver so state survives a server restart.
checkpointer = MemorySaver()
app = workflow.compile(checkpointer=checkpointer)
# Every invocation needs a thread_id so LangGraph knows which
# session's state to load and save on each step
config = {"configurable": {"thread_id": "patient-session-1"}}
result = app.invoke(
{"messages": [("user", "What are patient P123's vitals?")]},
config=config
)
# The graph can be resumed later against the same thread_id —
# LangGraph reloads the saved state instead of starting over
print(app.get_state(config).values["messages"])
With the checkpointer in place, calling app.invoke() again with the same thread_id continues the conversation from its last saved state, which is what makes the human-in-the-loop and crash-recovery behavior described above actually work in practice, rather than just in theory.
Also Read: AI Agents Vs Agentic AI
Knowing what an agent can do is one thing. Knowing why it’s worth the engineering investment is a different question. The stateful execution patterns covered above — cyclic workflows, explicit state, checkpointing, and human-in-the-loop gateways — are what LangGraph contributes to production reliability. Layering LangChain on top adds a separate set of practical advantages:
Model-Agnostic Architecture: Because LangChain standardizes the interface to the LLM, switching between OpenAI, Anthropic, or a self-hosted open-source model is a configuration change, not a rewrite of the agent’s core logic.
Reusable, Composable Components: Prompt templates, document loaders, retrievers, and tool wrappers are built once and reused across projects, cutting down the boilerplate that would otherwise go into every new agent.
Standardized Tool Integration: The @tool and args_schema pattern gives every external system a database, a REST API, or a search engine — a consistent, strictly typed interface the model can call reliably.
Production Observability: LangChain’s callback system, paired with LangSmith, traces every step of a run, tracks token consumption, and surfaces latency and failure points before they reach users.
Businesses seeking to deploy resilient applications can explore our specialized AI agent development services to design custom graph architectures suited to their operational workflows.
AI agents built on LangChain are already showing up across industries, and they’re doing a lot more than powering a simple AI chatbot. Here’s a quick breakdown of what these agents are actually doing and how they’re impacting different industries, positively.
| Industry | Application | What It Does |
|---|---|---|
| Healthcare | Patient intake agents, appointment coordination, records lookup | Reduces administrative load while keeping a human in the loop for clinical decisions |
| Finance | Fraud pattern review, document processing, compliance checks | Flags anomalies and routes edge cases to analysts instead of drowning them in routine review |
| Retail and E-commerce | Order status agents, inventory queries, personalized recommendations | Handles high-volume, repetitive customer questions without a growing support team |
| Logistics | Shipment tracking, route exception handling, vendor coordination | Resolves routine delays automatically and escalates only what actually needs a person |
| Software Development | Code review agents, automated testing, internal documentation lookup | Cuts down the time developers spend on repetitive review and search tasks |
| Legal | Contract clause review, compliance monitoring, first-draft flagging | Surfaces risk early and speeds up the first pass before a lawyer reviews it |
Through our engineering work at Xicom, we have highlighted core best practices for building production-grade agentic platforms:
Build Single-Purpose Tools: Define tools with simple, explicit responsibilities. Vague tool definitions increase model confusion and lead to incorrect function execution.
Configure Persistent Checkpoints: Always compile your graph with a checkpointer (see Step 5 above) to preserve graph state across server restarts or temporary API drops.
Build Fallback Routes for Tool Errors: Catch tool execution errors and feed error text back to the state object, giving the model an opportunity to adjust its approach autonomously.
Manage Context Space Efficiently: Avoid appending unparsed historical logs back to the LLM context window on every turn. Summarize long conversations and prune old tool outputs to lower token consumption and maintain execution speed.
Take Human Approval for Critical Mutations: Configure human-in-the-loop nodes before any action that modifies production databases, initiates financial transactions, or triggers customer-facing emails.
While agentic frameworks offer flexibility, deploying them into live software stacks comes with different engineering roadblocks. Here is how development teams address these challenges in practice:
| Technical Challenge | Root Cause | Practical Solution |
|---|---|---|
| Non-Deterministic Tool Execution | Language models pick slightly different tool parameters across similar inputs. | Enforce rigid Pydantic argument schemas and set model temperature parameters to zero. |
| High Latency & Slow Runs | Executing multiple sequential tool calls creates noticeable response delays. | Stream API steps asynchronously, cache frequent query results, and use multi-threading where applicable. |
| Context Degradation & Hallucination | Overloading prompt context windows causes models to lose track of original tasks. | Implement state pruning, summarize conversation history, and isolate complex sub-tasks into dedicated multi-agent graphs. |
| Unpredictable API Spend | Recursive tool loops generate unexpectedly high token volume and API billing. | Set max-iteration limits on graph edges and track token budgets across user sessions. |
Building production-grade AI agents involves far more than hitting an OpenAI API endpoint. While developing a basic prototype takes a few hours, moving an agent into production requires a layered infrastructure covering LLM token inference, vector storage, agent state management, orchestration, safety guardrails, and ongoing engineering overhead.
According to data from Precedence Research, the global AI agents market is projected to expand rapidly from $7.92 billion in 2025 to around $294.66 billion by 2035, growing at a CAGR of 43.57% between 2026 and 2035. This explosive commercial adoption reflects a shift from simple chatbots to autonomous systems that perform multi-step execution.
Below is a breakdown comparing Single-Task Agents, Multi-Agent Systems, and Enterprise-Grade Agentic Platforms:
| Agent Tier & Complexity | Core Offerings & System Features | Estimated Build Cost (USD) | Estimated Project Timeline |
|---|---|---|---|
| Single-Task AI Agent (Basic Automation) | Single LLM reasoning engine with 1–3 static tools; basic RAG pipeline (standard vector search); in-memory session tracking (no state persistence); standard API connections (Slack, CRM, Webhooks) | $15,000 – $45,000 | 4 – 8 Weeks |
| Multi-Agent System (Departmental Automation) | Hierarchical or supervisor multi-agent setup; complex tool integrations (SQL, REST APIs, Web Scraping); stateful graph execution via LangGraph / CrewAI; persistent memory databases (Redis, PostgreSQL); basic observability (LangSmith, OpenTelemetry) | $50,000 – $150,000 | 10 – 16 Weeks |
| Enterprise Agentic Platform (Autonomous Workflows) | Autonomous multi-agent networks with subgraphs; custom enterprise integrations (SAP, Salesforce, ERPs); human-in-the-loop review nodes & approval flows; fine-tuned or self-hosted open-source models; high-grade security (SOC 2, HIPAA, RBAC controls) | $150,000 – $400,000+ | 18 – 32+ Weeks |
Note: The prices mentioned above are tentative. The real cost depends on the company’s requirements.
Also Read: How Much Does It Cost to Build an AI Agent
Building an agent that survives contact with real users, real data, and real compliance requirements is a different problem entirely. Xicom has been building enterprise software for over 20 years, with a 350+ person engineering team that has delivered 1,800+ projects for clients across healthcare, finance, retail, logistics, and education.
And on the AI side specifically, that experience carries over directly. The state architecture that doesn’t fall apart under real traffic, tool boundaries that hold up under scrutiny, and compliance checkpoints built in from day one instead of bolted on after a launch.
Our AI agent development services cover the full path, from scoping what the agent should and shouldn’t be allowed to do, through the build, the testing, and the monitoring that catches problems before your users do.
Moving AI agents out of the lab and into core software stacks requires careful planning around data security, state management, and edge-case handling. At Xicom, our engineering teams partner directly with businesses to design, build, and deploy custom AI solutions and build practical AI agents that perform reliably in all environments.
Whether you are modernizing legacy operations, setting up secure internal retrieval networks, or launching intelligent customer-facing platforms, we provide the deep technicality and execution experience needed to build AI agents safely.
Ready to scale your software infrastructure with custom intelligence? Explore how Xicom’s services can help you build and deploy AI agents tailored to your business goals. To start building stateful autonomous workflows today for your business, connect with us to help you build an AI agent the right way, from architecture through deployment.