DEV Community

Muhammad Zulqarnain Akram
Muhammad Zulqarnain Akram

Posted on

Building AI Agents in 2025: From ChatGPT to Multi-Agent Systems

AI agents are revolutionizing how we build applications. From simple chatbots to complex multi-agent systems, the landscape has evolved dramatically. Here's everything you need to know to build production-ready AI agents in 2025.

What Are AI Agents?

An AI agent is an autonomous system that can perceive its environment, make decisions, and take actions to achieve specific goals. Unlike traditional software, AI agents can adapt and learn from interactions.

Types of AI Agents

  1. Simple Reflex Agents: React to current state
  2. Model-Based Agents: Maintain internal state
  3. Goal-Based Agents: Work towards specific objectives
  4. Utility-Based Agents: Optimize for best outcomes
  5. Learning Agents: Improve performance over time

Building Your First AI Agent

Using LangChain

from langchain.agents import create_openai_functions_agent
from langchain.tools import Tool
from langchain_openai import ChatOpenAI

# Define tools
def search_web(query: str) -> str:
    """Search the web for information"""
    # Implementation here
    return f"Results for: {query}"

def calculate(expression: str) -> str:
    """Calculate mathematical expressions"""
    return str(eval(expression))

tools = [
    Tool(name="Search", func=search_web, description="Search the web"),
    Tool(name="Calculator", func=calculate, description="Calculate math")
]

# Create agent
llm = ChatOpenAI(model="gpt-4", temperature=0)
agent = create_openai_functions_agent(llm, tools)

# Run agent
result = agent.run("What's 15% of 1200?")
print(result)
Enter fullscreen mode Exit fullscreen mode

Advanced: Multi-Agent Systems

Multi-agent systems involve multiple AI agents working together, each with specialized roles.

AutoGen Framework

import autogen

config_list = [{
    "model": "gpt-4",
    "api_key": "your-key"
}]

# Define agents
user_proxy = autogen.UserProxyAgent(
    name="user",
    human_input_mode="TERMINATE",
    max_consecutive_auto_reply=10
)

coder = autogen.AssistantAgent(
    name="coder",
    llm_config={"config_list": config_list},
    system_message="You write Python code to solve tasks."
)

reviewer = autogen.AssistantAgent(
    name="reviewer",
    llm_config={"config_list": config_list},
    system_message="You review code for bugs and improvements."
)

# Group chat
groupchat = autogen.GroupChat(
    agents=[user_proxy, coder, reviewer],
    messages=[],
    max_round=12
)

manager = autogen.GroupChatManager(
    groupchat=groupchat,
    llm_config={"config_list": config_list}
)

# Start conversation
user_proxy.initiate_chat(
    manager,
    message="Build a web scraper for product prices"
)
Enter fullscreen mode Exit fullscreen mode

Agent Memory Systems

Memory is crucial for context-aware agents:

from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain

# Short-term memory
memory = ConversationBufferMemory()

# Long-term memory with vector store
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings

vectorstore = FAISS.from_texts(
    ["User likes React", "User prefers TypeScript"],
    OpenAIEmbeddings()
)

# Retrieve relevant memories
relevant = vectorstore.similarity_search("What framework?")
print(relevant)
Enter fullscreen mode Exit fullscreen mode

Tool Creation for Agents

from langchain.tools import BaseTool
from pydantic import BaseModel, Field

class DatabaseQueryInput(BaseModel):
    query: str = Field(description="SQL query to execute")

class DatabaseTool(BaseTool):
    name = "database"
    description = "Query the database"
    args_schema = DatabaseQueryInput

    def _run(self, query: str) -> str:
        # Execute query safely
        # Return results
        return f"Query results: {query}"

    async def _arun(self, query: str) -> str:
        # Async implementation
        return self._run(query)
Enter fullscreen mode Exit fullscreen mode

ReACT Pattern (Reasoning + Acting)

from langchain.agents import load_tools, initialize_agent
from langchain.agents import AgentType

tools = load_tools(["serpapi", "llm-math"], llm=llm)

agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

result = agent.run(
    "What's the population of Tokyo and what's 20% of it?"
)
Enter fullscreen mode Exit fullscreen mode

Production Considerations

1. Rate Limiting

from functools import wraps
import time

def rate_limit(calls_per_minute=10):
    min_interval = 60.0 / calls_per_minute
    last_called = [0.0]

    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            elapsed = time.time() - last_called[0]
            if elapsed < min_interval:
                time.sleep(min_interval - elapsed)
            last_called[0] = time.time()
            return func(*args, **kwargs)
        return wrapper
    return decorator

@rate_limit(calls_per_minute=5)
def call_llm(prompt):
    return llm.invoke(prompt)
Enter fullscreen mode Exit fullscreen mode

2. Error Handling

from tenacity import retry, stop_after_attempt, wait_exponential

@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential(multiplier=1, min=4, max=10)
)
def resilient_agent_call(agent, input_text):
    try:
        return agent.run(input_text)
    except Exception as e:
        logger.error(f"Agent error: {e}")
        raise
Enter fullscreen mode Exit fullscreen mode

3. Cost Monitoring

from langchain.callbacks import get_openai_callback

with get_openai_callback() as cb:
    result = agent.run("Complex query")
    print(f"Total Tokens: {cb.total_tokens}")
    print(f"Total Cost: ${cb.total_cost}")
Enter fullscreen mode Exit fullscreen mode

Real-World Use Cases

1. Customer Support Agent

tools = [
    search_knowledge_base,
    create_ticket,
    escalate_to_human,
    check_order_status
]

support_agent = create_agent(
    tools=tools,
    system_message="You're a helpful customer support agent"
)
Enter fullscreen mode Exit fullscreen mode

2. Code Review Agent

code_reviewer = create_agent(
    tools=[analyze_code, suggest_improvements, check_security],
    system_message="Review code for bugs, performance, and security"
)
Enter fullscreen mode Exit fullscreen mode

3. Research Assistant

research_agent = create_agent(
    tools=[search_papers, summarize_content, cite_sources],
    system_message="Help with academic research"
)
Enter fullscreen mode Exit fullscreen mode

Future Trends

  1. Agentic Workflows: Chains of agents working together
  2. Self-Healing Systems: Agents that fix their own errors
  3. Hybrid Intelligence: Humans and agents collaborating
  4. Specialized Agent Marketplaces: Pre-built agents for specific tasks
  5. Edge AI Agents: Running locally for privacy

Best Practices

  1. Start Simple: Begin with single-purpose agents
  2. Clear Boundaries: Define what agents can and cannot do
  3. Human Oversight: Keep humans in the loop for critical decisions
  4. Monitor Performance: Track success rates and costs
  5. Version Control: Track agent prompts and configurations
  6. Test Extensively: Edge cases can cause unexpected behavior

Conclusion

AI agents are transforming how we build software. Whether you're creating a simple chatbot or a complex multi-agent system, the key is starting small and iterating based on real-world usage.

The future of software development will increasingly involve orchestrating AI agents to handle complex tasks autonomously.

What AI agents are you building? Share your experiences!


Exploring AI integration in production systems. Follow for more cutting-edge tech insights!

Top comments (0)