AI agents are easy to demonstrate and yet surprisingly difficult to productionize for consistent value.
A basic AI agent can receive a prompt, call an LLM, use a tool, and return a response. That is enough for a prototype.
Production systems are different.
A production AI agent needs to:
This is where LangGraph becomes useful.
This article explores how to design production-ready AI agents with LangGraph, including architecture, state management, tool execution, conditional workflows, error handling, and deployment considerations.
A simple agent typically performs like this:
User
β
LLM
β
Tool
β
LLM
β
Response
This works well for simple tasks.
Real-world applications often require a more controlled workflow:
User Request
β
Input Validation
β
Intent Detection
β
State Management
β
Tool Selection
β
Tool Execution
β
Result Validation
β
Decision
β β
Retry Human Review
β
Final Response
For example, an AI agent responsible for handling customer support requests may need to:
Managing everything inside one LLM prompt quickly becomes difficult to maintain.
A graph-based architecture makes the workflow explicit and easier to control.
State is one of the most important concepts when building a production agent.
Instead of passing every piece of information manually between functions, the workflow maintains a shared state object.
A simplified state could contain:
from typing import TypedDict
class AgentState(TypedDict):
user_input: str
intent: str
tool_result: str
response: str
Each node can read information from the state and return updates to it.
For example:
def analyze_request(state: AgentState):
user_input = state["user_input"]
intent = classify_intent(user_input)
return {
"intent": intent
}
Another node can consume that information:
def generate_response(state: AgentState):
intent = state["intent"]
tool_result = state.get("tool_result", "")
response = generate_answer(intent, tool_result)
return {
"response": response
}
This separation makes complex workflows easier to reason about and maintain.
A common mistake is creating one enormous agent function:
def agent():
As the application grows, this becomes difficult to test and modify.
Instead, separate responsibilities into individual nodes:
START
β
classify_request
β
retrieve_context
β
select_tool
β
execute_tool
β
validate_result
β
generate_response
β
END
Each node should ideally have one clear responsibility.
def retrieve_context(state):
context = search_knowledge_base(
state["user_input"]
)
return {
"context": context
}
This architecture allows individual components to be tested independently and modified more easily.
Once the nodes are defined, the graph controls how execution moves between them.
A simple workflow can be created using StateGraph:
from langgraph.graph import StateGraph, START, END
builder = StateGraph(AgentState)
builder.add_node("analyze", analyze_request)
builder.add_node("retrieve", retrieve_context)
builder.add_node("respond", generate_response)
builder.add_edge(START, "analyze")
builder.add_edge("analyze", "retrieve")
builder.add_edge("retrieve", "respond")
builder.add_edge("respond", END)
graph = builder.compile()
The resulting workflow is:
START
β
Analyze
β
Retrieve
β
Respond
β
END
The benefit is that developers can see exactly how the agent is expected to execute.
Production agents rarely follow only one path.
The next step may depend on the current state or detected intent.
Analyze Request
β
Determine Intent
β β
Knowledge API Tool
Search Execution
β β
Validate
β
Respond
A routing function can determine where the workflow should go next:
def route_request(state):
intent = state["intent"]
if intent == "knowledge":
return "retrieve"
if intent == "account":
return "account_tool"
return "respond"
The graph can then use that decision to select the next node.
This is more predictable than asking an LLM to control every part of the application's execution.
Tools allow an agent to interact with external systems.
Common examples include:
A production agent should not blindly execute every tool requested by an LLM.
Instead, introduce validation around tool execution.
A safer flow is:
LLM Decision
β
Tool Validation
β
Permission Check
β
Tool Execution
β
Result Validation
β
Update State
python
def execute_tool(state):
tool_name = state["selected_tool"]
if not is_allowed_tool(tool_name):
return {
"error": "Tool execution not permitted"
}
result = tools[tool_name].invoke(
state["tool_input"]
)
return {
"tool_result": result
}
The important principle is:
The LLM should make decisions only within boundaries defined by the application.
LLM applications can fail for many reasons:
A production workflow needs to account for these cases.
Instead of:
Tool
β
Failure
β
Agent stops
Use a recovery flow:
Tool
β
Validate
β
Success?
β β
Yes No
β β
Continue Retry / Recover
β
Still failing?
β
Human Review /
Error Response
The state can contain error and retry information:
class AgentState(TypedDict):
user_input: str
tool_result: str
error: str
retry_count: int
A routing function can determine whether another attempt should be made:
def handle_tool_result(state):
if not state.get("error"):
return "respond"
if state["retry_count"] < 2:
return "retry"
return "human_review"
This prevents the agent from entering an uncontrolled retry loop.
Not every decision should be fully autonomous.
For sensitive operations, a human approval step may be required.
Examples include:
A production architecture can include:
Agent Decision
β
Sensitive Action?
β β
No Yes
β β
Execute Human Approval
β
Approved?
β β
Yes No
β β
Execute Stop
LangGraph can therefore provide a controlled boundary between autonomous reasoning and business-critical actions.
Some agents complete their work in a few seconds.
Others may require minutes, hours, or human intervention.
Customer Request
β
Agent Analysis
β
Document Review
β
Human Approval
β
External API
β
Final Response
In these cases, the application needs to preserve relevant state throughout the workflow.
This is one reason stateful agent architectures are important for production systems.
Instead of thinking only about:
"What should the LLM answer?"
Developers also need to think about:
"What state does the application need to preserve while the workflow executes?"
One of the biggest differences between a demo and a production AI system is observability.
When a traditional API fails, developers can inspect logs to identify the request, service, response, and error.
Agentic systems introduce additional execution steps:
User Input
β
LLM Decision
β
Tool Selection
β
Tool Input
β
Tool Response
β
Conditional Decision
β
Final Output
Every important step should be observable.
Useful information to capture includes:
Without this information, debugging an agent can become extremely difficult and time-consuming.
A strong system prompt is useful, but it should not be the only control mechanism.
For production agents, combine model instructions with application-level controls:
LLM
β
Output Validation
β
Business Rules
β
Permission Check
β
Tool Execution
Suppose an agent is allowed to issue refunds based on certain parameters.
Instead of allowing the LLM to directly execute:
refund(amount)
the application can enforce a rule:
if amount > MAX_REFUND:
require_human_approval()
This creates a stronger safety boundary because the rule exists outside the model.
Testing an agent requires more than checking whether the final response looks correct.
Test individual nodes as well as complete workflows.
Test functions such as:
classify_request()
retrieve_context()
validate_tool_input()
route_request()
Test complete execution paths:
Normal Request
β
Expected Nodes
β
Expected Final State
Simulate:
Verify that sensitive operations cannot bypass the approval step.
The goal is to test not only what the agent does when everything works, but also what happens when things go wrong.
A production LangGraph application can be structured into several layers:
βββββββββββββββββββββββββββββββ
β API / UI Layer β
ββββββββββββββββ¬βββββββββββββββ
β
βββββββββββββββββββββββββββββββ
β Agent Entry Point β
ββββββββββββββββ¬βββββββββββββββ
β
βββββββββββββββββββββββββββββββ
β LangGraph β
β β
β Analyze β Retrieve β Tool β
β β β β
β Route β Validate β
β β β
β Response β
ββββββββββββββββ¬βββββββββββββββ
β
βββββββββββββββββββββββββββββββ
β Tools / APIs / DBs β
ββββββββββββββββ¬βββββββββββββββ
β
βββββββββββββββββββββββββββββββ
β Observability / Persistence β
βββββββββββββββββββββββββββββββ
Keeping these responsibilities separated makes the system easier to scale and maintain.
Building a basic AI agent is not difficult.
Building an AI agent that can reliably operate inside a real production environment is a different engineering problem.
The important shift is from:
Prompt β LLM β Response
to:
State
β
Decision
β
Controlled Action
β
Validation
β
Recovery
β
Human Intervention
β
Final Outcome
LangGraph provides a useful architecture for making these workflows explicit.
The real value is not simply adding an LLM to an application.
It is designing a system where:
That is the foundation of a production-ready AI agent.
Looking to build a production-ready AI agent for your business? Explore Ciphernutz AI Agent Development to learn more.