AI Agents: How to Build Autonomous AI Workflows in 2026
From simple chains to autonomous agents — tools, frameworks & real examples
AI agents represent the most significant shift in how software gets built since the move to cloud computing. Instead of writing explicit logic for every possible scenario, you give an LLM a goal and a set of tools, and it figures out the steps to achieve that goal. In 2026, agents power everything from automated code review to customer support to scientific research.
This guide covers the theory and practice of AI agents — from understanding the core architectures to building a working agent you can extend for your own use cases.
What You Will Learn
- What AI agents are and how they differ from simple LLM applications
- Core agent architectures: ReAct, tool-use, planning, and multi-agent
- The major frameworks: LangGraph, CrewAI, AutoGen
- How to build a working agent with tool use
- Multi-agent systems and when to use them
- Safety guardrails and production considerations
- Real-world agent examples
For a conceptual overview of agentic AI, see our agentic AI workflows guide.
What Makes an Agent Different
A standard LLM application follows a simple pattern:
Input → LLM → Output (single pass)
An AI agent follows a fundamentally different pattern:
Goal → Plan → Act → Observe → Reason → Act → Observe → ... → Result
The key differences:
| Feature | Standard LLM App | AI Agent | |---------|-----------------|----------| | Steps | Single pass | Multi-step loop | | Tool use | None or predefined | Dynamic tool selection | | Planning | None | Plans and replans | | State | Stateless | Maintains state across steps | | Autonomy | Responds to input | Decides next action | | Error handling | Returns error | Retries with different approach |
Core Agent Architectures
1. ReAct (Reason + Act)
The ReAct pattern is the foundation of most modern agents. The LLM alternates between reasoning about what to do and taking action.
Thought: I need to find the current GDP growth rate of India.
Action: search("India GDP growth rate 2026")
Observation: India's GDP grew 6.8% in FY2025-26 according to RBI data.
Thought: Now I need to compare this with last year's growth.
Action: search("India GDP growth rate 2025")
Observation: India's GDP grew 7.2% in FY2024-25.
Thought: I now have both years' data. Growth has slowed slightly.
Action: respond("India's GDP growth rate decreased from 7.2% in FY2024-25
to 6.8% in FY2025-26, a decline of 0.4 percentage points...")
When to use: General-purpose agents that need to search, calculate, and synthesize information. Works well for research, analysis, and Q&A tasks.
For a deep dive into the ReAct prompting technique, see our advanced prompt engineering guide.
2. Tool-Use Agent
A tool-use agent receives a set of tools with descriptions and decides which tools to call and in what order.
Available tools:
- calculator(expression): Evaluate math
- web_search(query): Search the internet
- send_email(to, subject, body): Send email
- read_file(path): Read file contents
User: "Calculate the GST on Rs 50,000 and email the invoice to [email protected]"
Agent decides:
Step 1: calculator("50000 * 0.18") → 9000
Step 2: calculator("50000 + 9000") → 59000
Step 3: send_email("[email protected]", "Invoice - Rs 59,000",
"Base: Rs 50,000 | GST 18%: Rs 9,000 | Total: Rs 59,000")
When to use: Workflows where you have specific capabilities (APIs, databases, services) the agent should orchestrate. This is how MCP servers integrate with agents.
3. Planning Agent
A planning agent creates a full plan before executing any steps, then follows the plan (adjusting if needed).
Goal: "Create a market analysis report for launching a D2C brand in India"
Plan:
1. Research current D2C market size in India
2. Identify top 10 D2C brands and their strategies
3. Analyze consumer trends (online shopping, tier-2/3 adoption)
4. Identify key challenges (logistics, returns, competition)
5. Draft recommendations
6. Compile into structured report
Executing step 1...
Executing step 2...
[Re-planning]: Step 2 revealed important regulatory changes.
Adding step 2b: Research new FSSAI labeling requirements for food D2C.
Continuing with updated plan...
When to use: Complex, multi-step tasks where the order of operations matters and you want the agent to think before acting.
4. Multi-Agent Systems
Multiple specialized agents collaborate on a task, each with their own role, tools, and expertise.
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Researcher │───→│ Analyst │───→│ Writer │
│ (searches) │ │ (evaluates) │ │ (drafts) │
└──────────────┘ └──────────────┘ └──────────────┘
│
┌──────────────┐
│ Reviewer │
│ (critiques) │
└──────────────┘
When to use: Complex tasks that benefit from different perspectives or specializations. Code review (writer + reviewer), content creation (researcher + writer + editor), analysis (data collector + analyst + presenter).
Framework Comparison
| Framework | Best For | Language | Learning Curve | Flexibility | |-----------|----------|----------|---------------|-------------| | LangGraph | Custom agent architectures | Python, JS | Medium | Very High | | CrewAI | Multi-agent role-based systems | Python | Low | Medium | | AutoGen | Conversational multi-agent | Python | Medium | High | | Smolagents | Simple, lightweight agents | Python | Low | Low | | OpenAI Agents SDK | OpenAI-native agents | Python | Low | Medium |
Building an Agent with LangGraph
Let us build a practical agent that can research Indian companies — searching the web, extracting financial data, and generating analysis.
Step 1: Setup
pip install langgraph langchain langchain-google-genai tavily-python
Get free API keys:
- Google Gemini: aistudio.google.com
- Tavily (web search): tavily.com — 1,000 free searches/month
Step 2: Define Tools
import os
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.tools import tool
from typing import Annotated
load_dotenv()
# Web search tool
search_tool = TavilySearchResults(
max_results=3,
search_depth="basic",
tavily_api_key=os.getenv("TAVILY_API_KEY"),
)
# Custom calculation tool
@tool
def calculate(expression: str) -> str:
"""Evaluate a mathematical expression. Use for financial calculations,
percentage changes, ratios, etc. Input should be a valid Python math
expression like '50000 * 1.18' or '(1500000 / 1200000 - 1) * 100'."""
try:
# Safe evaluation of math expressions
allowed_names = {"abs": abs, "round": round, "min": min, "max": max}
result = eval(expression, {"__builtins__": {}}, allowed_names)
return str(result)
except Exception as e:
return f"Calculation error: {e}"
@tool
def format_indian_currency(amount: float) -> str:
"""Format a number in Indian currency notation with lakhs and crores.
Input: a numeric amount. Output: formatted string like 'Rs 15.2 crore'
or 'Rs 8.5 lakh'."""
if amount >= 1_00_00_000:
return f"Rs {amount / 1_00_00_000:.2f} crore"
elif amount >= 1_00_000:
return f"Rs {amount / 1_00_000:.2f} lakh"
else:
return f"Rs {amount:,.2f}"
tools = [search_tool, calculate, format_indian_currency]
Step 3: Create the Agent
from langgraph.prebuilt import create_react_agent
from langchain_core.messages import HumanMessage
# Initialize the LLM with tool binding
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
google_api_key=os.getenv("GOOGLE_API_KEY"),
temperature=0.3,
)
# Create the ReAct agent
agent = create_react_agent(
model=llm,
tools=tools,
state_modifier=(
"You are a research analyst specializing in Indian businesses and "
"markets. When analyzing companies or markets:\n"
"1. Always search for the most recent data available\n"
"2. Use Indian currency notation (lakhs, crores)\n"
"3. Cite your sources\n"
"4. If data seems outdated, note the date and caveat your analysis\n"
"5. Present financial figures using the format_indian_currency tool\n"
"6. Use the calculate tool for any numerical analysis\n"
"7. Be specific about which fiscal year (Indian FY: April-March) "
"data refers to"
),
)
Step 4: Run the Agent
def run_agent(question: str):
"""Run the agent with a question and print results."""
print(f"\nQuestion: {question}\n")
print("-" * 60)
result = agent.invoke({
"messages": [HumanMessage(content=question)]
})
# Print the final response
final_message = result["messages"][-1]
print(f"\nAnswer:\n{final_message.content}")
# Print tool usage summary
tool_calls = [
msg for msg in result["messages"]
if hasattr(msg, "tool_calls") and msg.tool_calls
]
if tool_calls:
print(f"\nTools used: {len(tool_calls)} calls")
for msg in tool_calls:
for tc in msg.tool_calls:
print(f" - {tc['name']}({tc['args']})")
# Example queries
run_agent(
"What is the current market cap of Reliance Industries? "
"How does it compare to TCS? Express both in Indian crores."
)
run_agent(
"Analyze the Indian EV market in 2026. What are the top 5 "
"companies and their market share?"
)
Building a Multi-Agent System with CrewAI
For tasks that benefit from multiple perspectives, CrewAI lets you define a team of specialized agents.
from crewai import Agent, Task, Crew, Process
# Define specialized agents
researcher = Agent(
role="Market Researcher",
goal="Find accurate, up-to-date information about Indian markets",
backstory="You are an experienced market researcher who has covered "
"the Indian economy for 10 years. You know where to find reliable "
"data from sources like RBI, SEBI, and industry reports.",
tools=[search_tool],
verbose=True,
)
analyst = Agent(
role="Financial Analyst",
goal="Analyze data and identify trends, risks, and opportunities",
backstory="You are a CFA-qualified analyst at a Mumbai-based "
"investment firm. You specialize in Indian equity markets and "
"can read financial statements in your sleep.",
tools=[calculate, format_indian_currency],
verbose=True,
)
writer = Agent(
role="Report Writer",
goal="Create clear, actionable reports from research and analysis",
backstory="You write investment reports for HNI clients in India. "
"Your reports are concise, data-driven, and always include "
"specific recommendations with risk caveats.",
verbose=True,
)
# Define tasks
research_task = Task(
description="Research {company}'s recent financial performance, "
"market position, and competitive landscape in India. Focus on "
"FY2025-26 data.",
expected_output="A structured summary of financial data, market "
"share, and competitive positioning with specific numbers.",
agent=researcher,
)
analysis_task = Task(
description="Analyze the research data. Calculate key financial "
"ratios, growth rates, and compare with industry benchmarks. "
"Identify 3 strengths and 3 risks.",
expected_output="Financial analysis with calculated metrics, "
"trend analysis, and risk assessment.",
agent=analyst,
)
report_task = Task(
description="Write a 500-word investment analysis report combining "
"the research and analysis. Include an executive summary, key "
"metrics table, and recommendation (buy/hold/sell with rationale).",
expected_output="A professional investment analysis report in "
"markdown format.",
agent=writer,
)
# Create and run the crew
crew = Crew(
agents=[researcher, analyst, writer],
tasks=[research_task, analysis_task, report_task],
process=Process.sequential, # Tasks execute in order
verbose=True,
)
result = crew.kickoff(inputs={"company": "Infosys"})
print(result)
Agent Safety Guardrails
Autonomous agents need guardrails. Without them, an agent can loop indefinitely, make expensive API calls, or take unintended actions.
Essential Guardrails
# 1. Maximum iterations
agent = create_react_agent(
model=llm,
tools=tools,
state_modifier="...",
)
# Use recursion_limit when invoking
config = {"recursion_limit": 15} # Stop after 15 reasoning steps
result = agent.invoke({"messages": [HumanMessage(content=question)]}, config)
# 2. Human-in-the-loop for critical actions
@tool
def send_email(to: str, subject: str, body: str) -> str:
"""Send an email. Requires human confirmation before sending."""
print(f"\n--- EMAIL CONFIRMATION ---")
print(f"To: {to}")
print(f"Subject: {subject}")
print(f"Body: {body[:200]}...")
confirm = input("Send this email? (y/n): ")
if confirm.lower() == "y":
# actual send logic
return "Email sent successfully"
return "Email cancelled by user"
# 3. Cost monitoring
class CostTracker:
def __init__(self, max_cost_inr: float = 10.0):
self.total_cost = 0.0
self.max_cost = max_cost_inr
def add_cost(self, tokens_in: int, tokens_out: int):
# Approximate cost for Gemini Flash
cost = (tokens_in * 0.000075 + tokens_out * 0.0003) / 1000
cost_inr = cost * 83 # USD to INR
self.total_cost += cost_inr
if self.total_cost > self.max_cost:
raise Exception(
f"Cost limit exceeded: Rs {self.total_cost:.2f} > Rs {self.max_cost}"
)
Guardrail Checklist
| Guardrail | Why | Implementation |
|-----------|-----|----------------|
| Max iterations | Prevent infinite loops | recursion_limit in config |
| Cost limits | Prevent bill shock | Token counting and budget tracking |
| Action confirmation | Prevent unintended actions | Human approval for write operations |
| Output validation | Ensure quality | Check agent output against expected format |
| Timeout | Prevent hanging | Set timeout on API calls and tool execution |
| Logging | Debug and audit | Log every reasoning step and tool call |
| Scope constraints | Prevent scope creep | System prompt with explicit boundaries |
Real-World Agent Patterns in India
Pattern 1: Customer Support Escalation Agent
An agent that handles customer queries, searches the knowledge base (RAG), attempts to resolve issues, and escalates to a human agent when confidence is low. Used by Flipkart, Swiggy, and banking apps.
Pattern 2: Compliance Document Processor
An agent that reads regulatory documents (RBI circulars, SEBI notifications), extracts relevant changes, maps them to existing company policies, and generates compliance action items. Used by financial services firms.
Pattern 3: Code Review Agent
An agent that reviews pull requests, checks for security vulnerabilities, ensures coding standards compliance, and suggests improvements. Claude Code and GitHub Copilot use agent architectures for this. See our AI code review guide.
Pattern 4: Research Synthesis Agent
A multi-agent system where one agent searches academic papers, another searches news, a third analyzes the findings, and a fourth writes a synthesis report. Used in consulting firms and research labs.
Where to Go From Here
- Start simple — Build a single-tool ReAct agent before attempting multi-agent systems
- Add MCP tools — Connect your agent to real services using MCP servers for standardized tool integration
- Learn prompt engineering for agents — Agent system prompts require specific techniques covered in our advanced prompt engineering guide
- Explore RAG-powered agents — Combine agents with RAG for knowledge-grounded decision making
- Study production deployments — Read case studies from Indian AI companies to understand real-world agent architectures
- Join the community — The AI agents ecosystem is evolving rapidly. Follow LangChain, CrewAI, and Anthropic documentation for the latest patterns
AI agents are the next layer of software abstraction. Just as web frameworks abstracted away HTTP handling, agent frameworks are abstracting away the orchestration of AI-powered workflows. Learning to build agents now puts you at the forefront of the most consequential shift in software development.
Community Questions
0No questions yet. Be the first to ask!