{"id":2833,"date":"2026-03-27T09:24:42","date_gmt":"2026-03-27T09:24:42","guid":{"rendered":"https:\/\/www.mhtechin.com\/support\/?p=2833"},"modified":"2026-03-27T09:24:42","modified_gmt":"2026-03-27T09:24:42","slug":"how-to-build-an-autonomous-ai-agent-from-scratch-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/www.mhtechin.com\/support\/how-to-build-an-autonomous-ai-agent-from-scratch-a-step-by-step-guide\/","title":{"rendered":"How to Build an Autonomous AI Agent from Scratch: A Step-by-Step Guide"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine an AI that doesn\u2019t just answer questions but actively works toward goals\u2014researching topics, performing calculations, updating databases, and coordinating with other systems\u2014all with minimal human supervision. This is the promise of autonomous AI agents, and building one is more accessible than you might think.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An&nbsp;<strong>autonomous AI agent<\/strong>&nbsp;is a system that can make decisions and take actions on its own to achieve a goal\u2014typically by using large language models (LLMs), various tools, and memory to reason through tasks&nbsp;<a href=\"https:\/\/www.freecodecamp.org\/news\/build-autonomous-agents-using-prompt-chaining-with-ai-primitives\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>. Unlike traditional chatbots that wait for prompts, autonomous agents initiate actions, adapt to changing conditions, and learn from outcomes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this comprehensive guide, you\u2019ll learn how to build an autonomous AI agent from scratch. We\u2019ll cover everything from understanding core concepts to implementing memory systems, integrating tools, and deploying production-ready agents. Whether you\u2019re a developer new to AI agents or an experienced engineer looking for a structured approach, this guide provides actionable, step-by-step instructions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What You\u2019ll Learn:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The fundamental architecture of autonomous AI agents<\/li>\n\n\n\n<li>How to build a basic agent with a simple conversation loop<\/li>\n\n\n\n<li>Techniques for adding memory and context management<\/li>\n\n\n\n<li>Tool integration for real-world actions<\/li>\n\n\n\n<li>Advanced patterns like ReAct and multi-agent systems<\/li>\n\n\n\n<li>Production deployment considerations<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s start building.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Part 1: Understanding Autonomous AI Agents<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">What Makes an Agent \u201cAutonomous\u201d?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">An AI agent is considered autonomous when it can interpret a task, decide the next step, and take action without direct human instruction&nbsp;<a href=\"https:\/\/www.omdena.com\/blog\/autonomous-ai-agents\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>. This is achieved through three core capabilities:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"129\" src=\"https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image1-1-1024x129.png\" alt=\"\" class=\"wp-image-2843\" srcset=\"https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image1-1-1024x129.png 1024w, https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image1-1-300x38.png 300w, https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image1-1-768x97.png 768w, https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image1-1-1536x193.png 1536w, https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image1-1-2048x257.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Figure 1: The agentic loop\u2014perception, reasoning, action, and continuous feedback<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The Three Pillars of Autonomous Agents:<\/strong><\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Autonomy<\/strong>: Navigate tasks without direct human supervision, making decisions based on available tools and contextual information<\/li>\n\n\n\n<li><strong>Advanced Capabilities<\/strong>: Investigate, synthesize, compare, calculate, and plan\u2014not just generate responses<\/li>\n\n\n\n<li><strong>Interconnectivity<\/strong>: Integrate with APIs, databases, and external software to take real-world actions\u00a0<a href=\"https:\/\/www.omdena.com\/blog\/autonomous-ai-agents\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Agents vs. Chatbots: The Critical Difference<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Aspect<\/th><th class=\"has-text-align-left\" data-align=\"left\">Traditional Chatbot<\/th><th class=\"has-text-align-left\" data-align=\"left\">Autonomous AI Agent<\/th><\/tr><\/thead><tbody><tr><td><strong>Behavior<\/strong><\/td><td>Reactive\u2014waits for prompts<\/td><td>Proactive\u2014pursues goals<\/td><\/tr><tr><td><strong>Actions<\/strong><\/td><td>Generates text only<\/td><td>Calls tools, APIs, executes code<\/td><\/tr><tr><td><strong>Memory<\/strong><\/td><td>Session-based<\/td><td>Persistent across interactions<\/td><\/tr><tr><td><strong>Decision-Making<\/strong><\/td><td>Predefined rules<\/td><td>Dynamic reasoning<\/td><\/tr><tr><td><strong>Adaptability<\/strong><\/td><td>Fixed responses<\/td><td>Learns and improves<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Core Components of an AI Agent<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">A complete autonomous agent requires these building blocks:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Perception Module<\/strong>: Processes input from users, APIs, or sensors<\/li>\n\n\n\n<li><strong>Reasoning Engine (LLM)<\/strong>: Makes decisions based on context and goals<\/li>\n\n\n\n<li><strong>Memory System<\/strong>: Stores conversation history and learned information<\/li>\n\n\n\n<li><strong>Tool Integration<\/strong>: Connects to external APIs, databases, and functions<\/li>\n\n\n\n<li><strong>Orchestration Layer<\/strong>: Coordinates the agent loop and multi-step workflows\u00a0<a href=\"https:\/\/www.omdena.com\/blog\/autonomous-ai-agents\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Part 2: Prerequisites and Environment Setup<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Required Skills and Knowledge<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving in, ensure you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Python 3.8+<\/strong>\u00a0proficiency (functions, classes, type hints)<\/li>\n\n\n\n<li>Basic understanding of\u00a0<strong>APIs and JSON<\/strong><\/li>\n\n\n\n<li>Familiarity with\u00a0<strong>environment variables<\/strong>\u00a0and command line<\/li>\n\n\n\n<li>(Optional) Basic\u00a0<strong>machine learning concepts<\/strong>\u00a0<a href=\"https:\/\/github.com\/niket-sharma\/AI-Agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Development Environment Setup<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Install Python and Package Manager<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># For macOS\/Linux\u2014install UV (fast Python package manager)\ncurl -LsSf https:\/\/astral.sh\/uv\/install.sh | sh\n\n# For Windows\npowershell -c \"irm https:\/\/astral.sh\/uv\/install.ps1 | iex\"<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Create Project Structure<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir my-ai-agent &amp;&amp; cd my-ai-agent\nmkdir -p src\/agent src\/memory src\/tools\ntouch src\/__init__.py\ntouch .env requirements.txt<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Set Up Virtual Environment and Install Dependencies<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">uv venv\nsource .venv\/bin\/activate  # On Windows: .venv\\Scripts\\activate\n\npip install openai anthropic python-dotenv requests pydantic<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 4: Get Your API Key<\/h4>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Sign up at\u00a0<a href=\"https:\/\/platform.openai.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">OpenAI<\/a>\u00a0or\u00a0<a href=\"https:\/\/console.anthropic.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Anthropic<\/a><\/li>\n\n\n\n<li>Generate an API key<\/li>\n\n\n\n<li>Add to your\u00a0<code>.env<\/code>\u00a0file:<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OPENAI_API_KEY=your-key-here\nANTHROPIC_API_KEY=your-key-here  # if using Claude<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 5: Verify Setup<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create a test file&nbsp;<code>test_setup.py<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import os\nfrom dotenv import load_dotenv\nfrom openai import OpenAI\n\nload_dotenv()\nclient = OpenAI(api_key=os.getenv(\"OPENAI_API_KEY\"))\n\nresponse = client.chat.completions.create(\n    model=\"gpt-4o-mini\",\n    messages=[{\"role\": \"user\", \"content\": \"Say hello!\"}]\n)\nprint(response.choices[0].message.content)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run with:&nbsp;<code>python test_setup.py<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Part 3: Building Your First Basic Agent<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">The Agent Loop Pattern<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Every autonomous agent operates on a&nbsp;<strong>continuous loop<\/strong>: accept input \u2192 reason \u2192 decide \u2192 act \u2192 observe \u2192 repeat&nbsp;<a href=\"https:\/\/github.com\/the-ai-engineer\/ai-agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>. Let\u2019s implement this from scratch.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Create the Basic Agent Class<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<code>src\/agent\/basic_agent.py<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import os<br>from typing import List, Dict, Any<br>from openai import OpenAI<br>from dotenv import load_dotenv<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">load_dotenv()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class BasicAgent:<br>&#8220;&#8221;&#8221;A simple autonomous agent with a conversation loop.&#8221;&#8221;&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def __init__(self, name: str, system_prompt: str = None):\n    self.name = name\n    self.client = OpenAI(api_key=os.getenv(\"OPENAI_API_KEY\"))\n    self.messages: List&#91;Dict&#91;str, str]] = &#91;]\n\n    # Set system prompt\n    if system_prompt:\n        self.messages.append({\n            \"role\": \"system\",\n            \"content\": system_prompt\n        })\n\ndef think(self, user_input: str) -&gt; str:\n    \"\"\"Process input and generate a response.\"\"\"\n    # Add user message to history\n    self.messages.append({\n        \"role\": \"user\",\n        \"content\": user_input\n    })\n\n    # Call LLM\n    response = self.client.chat.completions.create(\n        model=\"gpt-4o-mini\",\n        messages=self.messages\n    )\n\n    # Extract and store response\n    assistant_response = response.choices&#91;0].message.content\n    self.messages.append({\n        \"role\": \"assistant\",\n        \"content\": assistant_response\n    })\n\n    return assistant_response\n\ndef reset_conversation(self):\n    \"\"\"Clear conversation history.\"\"\"\n    self.messages = &#91;self.messages&#91;0]] if self.messages else &#91;]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Run Your First Agent<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<code>run_agent.py<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">from src.agent.basic_agent import BasicAgent<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">#Create agent with a purpose<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">agent = BasicAgent(<br>name=&#8221;ResearchBot&#8221;,<br>system_prompt=&#8221;You are a research assistant. Answer questions concisely and provide sources when possible.&#8221;<br>)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">#Interactive loop<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">print(f&#8221;Agent {agent.name} is ready. Type &#8216;quit&#8217; to exit.\\n&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">while True:<br>user_input = input(&#8220;\\nYou: &#8220;)<br>if user_input.lower() == &#8216;quit&#8217;:<br>break<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>response = agent.think(user_input)\nprint(f\"\\n{agent.name}: {response}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What\u2019s happening under the hood?<\/strong><\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>The agent stores conversation history in\u00a0<code>self.messages<\/code><\/li>\n\n\n\n<li>Each turn appends user input and assistant response<\/li>\n\n\n\n<li>The LLM uses full history for context\u00a0<a href=\"https:\/\/github.com\/niket-sharma\/AI-Agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Part 4: Adding Memory Systems<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Why Memory Matters<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Without memory, agents can\u2019t maintain context across conversations or learn from past interactions. Production agents need both&nbsp;<strong>short-term memory<\/strong>&nbsp;(conversation context) and&nbsp;<strong>long-term memory<\/strong>&nbsp;(persistent knowledge)&nbsp;<a href=\"https:\/\/github.com\/the-ai-engineer\/ai-agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><a href=\"https:\/\/github.com\/niket-sharma\/AI-Agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Implementing Conversation Memory<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1 : Create Memory Manager<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<code>src\/memory\/memory_manager.py<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">from typing import List, Dict, Any<br>import json<br>from datetime import datetime<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class ConversationMemory:<br>&#8220;&#8221;&#8221;Manages conversation history with token limits.&#8221;&#8221;&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def __init__(self, max_tokens: int = 4000):\n    self.history: List&#91;Dict&#91;str, Any]] = &#91;]\n    self.max_tokens = max_tokens\n\ndef add_message(self, role: str, content: str):\n    \"\"\"Add a message to history.\"\"\"\n    self.history.append({\n        \"role\": role,\n        \"content\": content,\n        \"timestamp\": datetime.now().isoformat()\n    })\n    self._trim_if_needed()\n\ndef get_context(self, last_n: int = None) -> List&#91;Dict&#91;str, str]]:\n    \"\"\"Get recent messages for LLM context.\"\"\"\n    if last_n:\n        recent = self.history&#91;-last_n:]\n    else:\n        recent = self.history\n\n    return &#91;{\"role\": m&#91;\"role\"], \"content\": m&#91;\"content\"]} \n            for m in recent]\n\ndef _trim_if_needed(self):\n    \"\"\"Simple token estimation and trimming.\"\"\"\n    # Rough estimate: ~4 chars per token\n    total_chars = sum(len(m&#91;\"content\"]) for m in self.history)\n    estimated_tokens = total_chars \/ 4\n\n    if estimated_tokens > self.max_tokens:\n        # Keep system message + last 10 exchanges\n        self.history = self.history&#91;:1] + self.history&#91;-10:]\n\ndef summarize(self) -> str:\n    \"\"\"Generate a summary of conversation.\"\"\"\n    if not self.history:\n        return \"\"\n\n    summary = f\"Conversation from {self.history&#91;0]&#91;'timestamp']}\\n\"\n    for m in self.history:\n        summary += f\"{m&#91;'role']}: {m&#91;'content']&#91;:100]}...\\n\"\n    return summary\n\n<strong>Step 2: Enhanced Agent with Memory<\/strong>\nUpdate src\/agent\/memory_agent.py:\npython\nfrom src.agent.basic_agent import BasicAgent\nfrom src.memory.memory_manager import ConversationMemory\n\nclass MemoryAgent(BasicAgent):\n    \"\"\"Agent with persistent memory capabilities.\"\"\"\n    \n    def __init__(self, name: str, system_prompt: str = None):\n        super().__init__(name, system_prompt)\n        self.memory = ConversationMemory()\n        \n        # Initialize memory with system prompt\n        if system_prompt:\n            self.memory.add_message(\"system\", system_prompt)\n    \n    def think(self, user_input: str) -> str:\n        \"\"\"Process input with memory context.\"\"\"\n        # Add user input to memory\n        self.memory.add_message(\"user\", user_input)\n        \n        # Get context from memory\n        context = self.memory.get_context(last_n=20)\n        \n        # Call LLM with context\n        response = self.client.chat.completions.create(\n            model=\"gpt-4o-mini\",\n            messages=context\n        )\n        \n        assistant_response = response.choices&#91;0].message.content\n        \n        # Add to memory\n        self.memory.add_message(\"assistant\", assistant_response)\n        \n        return assistant_response\n    \n    def get_conversation_summary(self) -> str:\n        \"\"\"Return summary of all interactions.\"\"\"\n        return self.memory.summarize()\nLong-Term Memory with Vector Databases\nFor advanced memory, use vector databases like ChromaDB to store and retrieve semantically similar past interactions .\n\npython\n# Install chromadb first: pip install chromadb\n\nimport chromadb\nfrom chromadb.utils import embedding_functions\n\nclass VectorMemory:\n    \"\"\"Long-term memory using vector embeddings.\"\"\"\n    \n    def __init__(self, collection_name: str = \"agent_memory\"):\n        self.client = chromadb.Client()\n        self.collection = self.client.create_collection(\n            name=collection_name,\n            embedding_function=embedding_functions.DefaultEmbeddingFunction()\n        )\n    \n    def add_memory(self, text: str, metadata: Dict = None):\n        \"\"\"Store a memory with embedding.\"\"\"\n        self.collection.add(\n            documents=&#91;text],\n            metadatas=&#91;metadata or {}],\n            ids=&#91;str(hash(text))]\n        )\n    \n    def search_memory(self, query: str, n_results: int = 3) -> List&#91;str]:\n        \"\"\"Retrieve relevant memories.\"\"\"\n        results = self.collection.query(\n            query_texts=&#91;query],\n            n_results=n_results\n        )\n        return results&#91;'documents']&#91;0] if results&#91;'documents'] else &#91;]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Part 5: Tool Integration and Function Calling<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Giving Agents the Ability to Act<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Tools are what transform a language model into an autonomous agent. A tool can be any Python function, API call, database query, or external service that the agent can invoke to accomplish tasks\u00a0<a href=\"https:\/\/docs.ag2.ai\/latest\/docs\/home\/quickstart\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><a href=\"https:\/\/www.omdena.com\/blog\/autonomous-ai-agents\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Tool System<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Define Tool Schema<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<code>src\/tools\/base_tool.py<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from typing import Any, Callable, Dict, Optional\nfrom pydantic import BaseModel\n\nclass ToolParameter(BaseModel):\n    \"\"\"Parameter definition for a tool.\"\"\"\n    name: str\n    type: str\n    description: str\n    required: bool = True\n\nclass Tool(BaseModel):\n    \"\"\"Base class for agent tools.\"\"\"\n    name: str\n    description: str\n    parameters: list[ToolParameter]\n    function: Callable\n    \n    def execute(self, **kwargs) -&gt; Any:\n        \"\"\"Execute the tool with given parameters.\"\"\"\n        try:\n            return self.function(**kwargs)\n        except Exception as e:\n            return f\"Error executing tool: {str(e)}\"<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Implement Example Tools<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<code>src\/tools\/calculator.py<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from .base_tool import Tool, ToolParameter\n\ndef calculate_average(numbers: list) -&gt; float:\n    \"\"\"Calculate the average of a list of numbers.\"\"\"\n    if not numbers:\n        return 0\n    return sum(numbers) \/ len(numbers)\n\ndef web_search(query: str) -&gt; str:\n    \"\"\"Simulate web search (replace with actual API).\"\"\"\n    # In production, use SerpAPI, Tavily, or Brave Search\n    return f\"Search results for: {query}\"\n\ndef get_current_time() -&gt; str:\n    \"\"\"Get current date and time.\"\"\"\n    from datetime import datetime\n    return datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")\n\n# Define tools with schemas\ntools = [\n    Tool(\n        name=\"calculate_average\",\n        description=\"Calculate the average of a list of numbers\",\n        parameters=[\n            ToolParameter(name=\"numbers\", type=\"list\", \n                         description=\"List of numbers to average\")\n        ],\n        function=calculate_average\n    ),\n    Tool(\n        name=\"web_search\",\n        description=\"Search the web for information\",\n        parameters=[\n            ToolParameter(name=\"query\", type=\"string\", \n                         description=\"Search query\")\n        ],\n        function=web_search\n    ),\n    Tool(\n        name=\"get_current_time\",\n        description=\"Get the current date and time\",\n        parameters=[],\n        function=get_current_time\n    )\n]<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Agent with Tool Calling<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<code>src\/agent\/tool_agent.py<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import json\nfrom typing import List, Dict, Any\nfrom src.agent.basic_agent import BasicAgent\nfrom src.tools.calculator import tools\n\nclass ToolAgent(BasicAgent):\n    \"\"\"Agent that can use tools to accomplish tasks.\"\"\"\n    \n    def __init__(self, name: str, system_prompt: str = None):\n        super().__init__(name, system_prompt)\n        self.tools = {tool.name: tool for tool in tools}\n        self.tool_schemas = self._prepare_tool_schemas()\n    \n    def _prepare_tool_schemas(self) -&gt; List[Dict]:\n        \"\"\"Convert tools to OpenAI function calling format.\"\"\"\n        schemas = []\n        for tool in self.tools.values():\n            properties = {}\n            required = []\n            \n            for param in tool.parameters:\n                properties[param.name] = {\n                    \"type\": param.type,\n                    \"description\": param.description\n                }\n                if param.required:\n                    required.append(param.name)\n            \n            schemas.append({\n                \"type\": \"function\",\n                \"function\": {\n                    \"name\": tool.name,\n                    \"description\": tool.description,\n                    \"parameters\": {\n                        \"type\": \"object\",\n                        \"properties\": properties,\n                        \"required\": required\n                    }\n                }\n            })\n        return schemas\n    \n    def think(self, user_input: str) -&gt; str:\n        \"\"\"Process input with tool calling capability.\"\"\"\n        self.messages.append({\"role\": \"user\", \"content\": user_input})\n        \n        # First call to determine if tools are needed\n        response = self.client.chat.completions.create(\n            model=\"gpt-4o-mini\",\n            messages=self.messages,\n            tools=self.tool_schemas,\n            tool_choice=\"auto\"\n        )\n        \n        message = response.choices[0].message\n        \n        # Check if the model wants to call a tool\n        if message.tool_calls:\n            # Execute tools\n            for tool_call in message.tool_calls:\n                tool_name = tool_call.function.name\n                arguments = json.loads(tool_call.function.arguments)\n                \n                # Execute tool\n                tool = self.tools.get(tool_name)\n                if tool:\n                    result = tool.execute(**arguments)\n                    \n                    # Add tool result to conversation\n                    self.messages.append({\n                        \"role\": \"tool\",\n                        \"tool_call_id\": tool_call.id,\n                        \"content\": str(result)\n                    })\n            \n            # Second call to generate final response\n            final_response = self.client.chat.completions.create(\n                model=\"gpt-4o-mini\",\n                messages=self.messages\n            )\n            assistant_response = final_response.choices[0].message.content\n        else:\n            assistant_response = message.content\n        \n        self.messages.append({\"role\": \"assistant\", \"content\": assistant_response})\n        return assistant_response<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using MCP Servers for Standardized Tool Access<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>Model Context Protocol (MCP)<\/strong>&nbsp;provides a standardized way for agents to discover and use tools. Here\u2019s a basic MCP server implementation&nbsp;<a href=\"https:\/\/github.com\/niket-sharma\/AI-Agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># mcp_server.py\nfrom mcp import Server, Tool\n\nserver = Server(\"my-agent-server\")\n\n@server.tool()\ndef get_weather(city: str) -&gt; str:\n    \"\"\"Get current weather for a city.\"\"\"\n    return f\"Weather in {city}: 72\u00b0F, sunny\"\n\n# Run with: python mcp_server.py<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Part 6: Advanced Agent Architectures<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">The ReAct Pattern (Reason + Act)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The ReAct pattern interleaves reasoning traces with task-specific actions, allowing agents to think step-by-step before acting&nbsp;<a href=\"https:\/\/developer.baidu.com\/article\/detail.html?id=5532388\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"1024\" src=\"https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image2-1-663x1024.png\" alt=\"\" class=\"wp-image-2856\" style=\"aspect-ratio:0.6474686071793051;width:299px;height:auto\" srcset=\"https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image2-1-663x1024.png 663w, https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image2-1-194x300.png 194w, https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image2-1-768x1186.png 768w, https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image2-1-995x1536.png 995w, https:\/\/www.mhtechin.com\/support\/wp-content\/uploads\/2026\/03\/A3-image2-1.png 1003w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Figure 2: The ReAct cycle\u2014Thought, Action, Observation<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Implementing ReAct Pattern<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<code>src\/agent\/react_agent.py<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class ReActAgent(ToolAgent):\n    \"\"\"Agent using ReAct (Reason + Act) pattern.\"\"\"\n    \n    REACT_PROMPT = \"\"\"You are an autonomous agent. Follow this format:\n\nQuestion: {input}\nThought: You should think about what to do next\nAction: The action to take (one of: {tools})\nAction Input: The input to the action\nObservation: The result of the action\n... (repeat Thought\/Action\/Observation as needed)\nThought: I now know the final answer\nFinal Answer: The final answer to the original question\n\nAvailable tools: {tool_list}\n\"\"\"\n    \n    def think(self, user_input: str) -&gt; str:\n        \"\"\"Process using ReAct pattern.\"\"\"\n        tool_names = \", \".join(self.tools.keys())\n        \n        # Build prompt with available tools\n        prompt = self.REACT_PROMPT.format(\n            input=user_input,\n            tools=tool_names,\n            tool_list=tool_names\n        )\n        \n        # Iterative reasoning loop\n        max_iterations = 5\n        current_prompt = prompt\n        \n        for i in range(max_iterations):\n            response = self.client.chat.completions.create(\n                model=\"gpt-4o-mini\",\n                messages=[{\"role\": \"user\", \"content\": current_prompt}]\n            )\n            \n            content = response.choices[0].message.content\n            \n            # Check for final answer\n            if \"Final Answer:\" in content:\n                return content.split(\"Final Answer:\")[-1].strip()\n            \n            # Parse and execute action\n            if \"Action:\" in content and \"Action Input:\" in content:\n                action_line = [l for l in content.split(\"\\n\") if \"Action:\" in l][0]\n                input_line = [l for l in content.split(\"\\n\") if \"Action Input:\" in l][0]\n                \n                action = action_line.replace(\"Action:\", \"\").strip()\n                action_input = input_line.replace(\"Action Input:\", \"\").strip()\n                \n                # Execute tool\n                tool = self.tools.get(action)\n                if tool:\n                    result = tool.execute(**{\"query\": action_input} if \"query\" in str(tool.parameters) else {})\n                    \n                    # Add observation to prompt\n                    current_prompt = content + f\"\\nObservation: {result}\\n\"\n        \n        return \"Unable to complete task within iterations\"<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Multi-Agent Systems<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For complex tasks, specialized agents can collaborate. AG2 (formerly AutoGen) provides excellent patterns for multi-agent orchestration&nbsp;<a href=\"https:\/\/pypi.org\/project\/ag2\/0.9.6\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><a href=\"https:\/\/docs.ag2.ai\/latest\/docs\/home\/quickstart\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Basic Multi-Agent Setup with AG2<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from autogen import ConversableAgent, GroupChat, GroupChatManager, LLMConfig\nimport os\n\n# Configure LLM\nllm_config = LLMConfig(\n    api_type=\"openai\",\n    model=\"gpt-4o-mini\",\n    api_key=os.environ[\"OPENAI_API_KEY\"]\n)\n\n# Create specialized agents\nresearcher = ConversableAgent(\n    name=\"Researcher\",\n    system_message=\"You research topics and find information.\",\n    llm_config=llm_config\n)\n\nwriter = ConversableAgent(\n    name=\"Writer\",\n    system_message=\"You synthesize information into clear content.\",\n    llm_config=llm_config\n)\n\nreviewer = ConversableAgent(\n    name=\"Reviewer\",\n    system_message=\"You review content for accuracy and clarity.\",\n    llm_config=llm_config\n)\n\n# Create group chat\ngroupchat = GroupChat(\n    agents=[researcher, writer, reviewer],\n    messages=[],\n    speaker_selection_method=\"auto\"\n)\n\nmanager = GroupChatManager(\n    groupchat=groupchat,\n    llm_config=llm_config\n)\n\n# Start collaboration\nresearcher.initiate_chat(\n    manager,\n    message=\"Research and write a summary of recent AI agent developments.\"\n)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Orchestration Patterns<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Five fundamental orchestration patterns for AI agents&nbsp;<a href=\"https:\/\/github.com\/the-ai-engineer\/ai-agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Pattern<\/th><th class=\"has-text-align-left\" data-align=\"left\">Description<\/th><th class=\"has-text-align-left\" data-align=\"left\">Use Case<\/th><\/tr><\/thead><tbody><tr><td><strong>Chaining<\/strong><\/td><td>Sequential execution<\/td><td>Document processing pipelines<\/td><\/tr><tr><td><strong>Routing<\/strong><\/td><td>Conditional branching<\/td><td>Customer support triage<\/td><\/tr><tr><td><strong>Parallelization<\/strong><\/td><td>Concurrent execution<\/td><td>Data processing, web scraping<\/td><\/tr><tr><td><strong>Orchestrator-Workers<\/strong><\/td><td>Central controller with workers<\/td><td>Complex task decomposition<\/td><\/tr><tr><td><strong>Evaluator-Optimizer<\/strong><\/td><td>Iterative refinement<\/td><td>Content generation, code review<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Part 7: Deploying Your Agent with FastAPI<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Production API<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use FastAPI to expose your agent as a service&nbsp;<a href=\"https:\/\/www.omdena.com\/blog\/autonomous-ai-agents\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<code>main.py<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from fastapi import FastAPI, HTTPException\nfrom pydantic import BaseModel\nfrom src.agent.tool_agent import ToolAgent\nimport uvicorn\n\napp = FastAPI(title=\"AI Agent API\", version=\"1.0.0\")\n\n# Initialize agent once\nagent = ToolAgent(\n    name=\"ProductionAgent\",\n    system_prompt=\"You are a helpful assistant that can use tools to help users.\"\n)\n\nclass QueryRequest(BaseModel):\n    question: str\n    session_id: str = None\n\nclass QueryResponse(BaseModel):\n    response: str\n    session_id: str\n\n@app.post(\"\/ask\", response_model=QueryResponse)\nasync def ask_agent(request: QueryRequest):\n    \"\"\"Send a question to the AI agent.\"\"\"\n    try:\n        response = agent.think(request.question)\n        return QueryResponse(\n            response=response,\n            session_id=request.session_id or \"default\"\n        )\n    except Exception as e:\n        raise HTTPException(status_code=500, detail=str(e))\n\n@app.get(\"\/health\")\nasync def health_check():\n    \"\"\"Health check endpoint.\"\"\"\n    return {\"status\": \"healthy\"}\n\n@app.get(\"\/tools\")\nasync def list_tools():\n    \"\"\"List available tools.\"\"\"\n    return {\"tools\": list(agent.tools.keys())}\n\nif __name__ == \"__main__\":\n    uvicorn.run(app, host=\"0.0.0.0\", port=8000)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Running Your Agent Service<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Install FastAPI and Uvicorn\npip install fastapi uvicorn\n\n# Run the server\nuvicorn main:app --reload --host 0.0.0.0 --port 8000<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Your agent API is now available at&nbsp;<code>http:\/\/localhost:8000\/docs<\/code>&nbsp;with automatic OpenAPI documentation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adding Guardrails<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prevent misuse and ensure safety with input validation&nbsp;<a href=\"https:\/\/www.omdena.com\/blog\/autonomous-ai-agents\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from fastapi import FastAPI, HTTPException, Depends\nimport re\n\nclass Guardrails:\n    \"\"\"Input validation and safety checks.\"\"\"\n    \n    FORBIDDEN_PATTERNS = [\n        r\"(?i)hack|exploit|bypass|illegal\",\n        r\"(?i)ignore previous instructions\",\n    ]\n    \n    @classmethod\n    def validate_input(cls, text: str) -&gt; bool:\n        \"\"\"Check if input passes safety checks.\"\"\"\n        for pattern in cls.FORBIDDEN_PATTERNS:\n            if re.search(pattern, text):\n                return False\n        return True\n\n@app.post(\"\/ask\")\nasync def ask_agent(request: QueryRequest):\n    # Check input safety\n    if not Guardrails.validate_input(request.question):\n        raise HTTPException(status_code=400, detail=\"Input blocked by safety policy\")\n    \n    response = agent.think(request.question)\n    return {\"response\": response}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Part 8: Testing and Evaluation<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Testing Your Agent<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<code>tests\/test_agent.py<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pytest\nfrom src.agent.basic_agent import BasicAgent\nfrom src.agent.tool_agent import ToolAgent\n\n@pytest.fixture\ndef basic_agent():\n    return BasicAgent(\"TestBot\")\n\ndef test_basic_response(basic_agent):\n    response = basic_agent.think(\"Say hello\")\n    assert response is not None\n    assert len(response) &gt; 0\n\ndef test_tool_calculation():\n    agent = ToolAgent(\"CalcBot\")\n    response = agent.think(\"What is the average of 10, 20, and 30?\")\n    assert \"20\" in response or \"average\" in response.lower()\n\ndef test_memory_persistence():\n    agent = BasicAgent(\"MemoryBot\")\n    agent.think(\"My name is Alice\")\n    response = agent.think(\"What is my name?\")\n    assert \"Alice\" in response<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Evaluation Metrics<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Metric<\/th><th class=\"has-text-align-left\" data-align=\"left\">Description<\/th><th class=\"has-text-align-left\" data-align=\"left\">Target<\/th><\/tr><\/thead><tbody><tr><td><strong>Task Success Rate<\/strong><\/td><td>% of tasks completed successfully<\/td><td>&gt;80%<\/td><\/tr><tr><td><strong>Tool Call Accuracy<\/strong><\/td><td>% of tool calls that were appropriate<\/td><td>&gt;90%<\/td><\/tr><tr><td><strong>Response Latency<\/strong><\/td><td>Time from input to output<\/td><td>&lt;3 seconds<\/td><\/tr><tr><td><strong>Hallucination Rate<\/strong><\/td><td>% of responses with false information<\/td><td>&lt;5%<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Part 9: Best Practices and Common Pitfalls<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Architecture Best Practices<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Modular Design<\/strong>: Separate perception, reasoning, and execution layers\u00a0<a href=\"https:\/\/developer.baidu.com\/article\/detail.html?id=5532388\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n\n\n\n<li><strong>Observability<\/strong>: Log every decision, tool call, and outcome\u00a0<a href=\"https:\/\/docs.ag2.ai\/latest\/docs\/home\/quickstart\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n\n\n\n<li><strong>Human-in-the-Loop<\/strong>: Add approval steps for critical actions\u00a0<a href=\"https:\/\/pypi.org\/project\/ag2\/0.9.6\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n\n\n\n<li><strong>Error Handling<\/strong>: Gracefully handle tool failures and API errors<\/li>\n\n\n\n<li><strong>Rate Limiting<\/strong>: Respect API limits with queuing and backoff<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Common Pitfalls to Avoid<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Pitfall<\/th><th class=\"has-text-align-left\" data-align=\"left\">Solution<\/th><\/tr><\/thead><tbody><tr><td><strong>Over-reliance on LLMs<\/strong><\/td><td>Use lightweight models for simple tasks<\/td><\/tr><tr><td><strong>No Evaluation Framework<\/strong><\/td><td>Define and track success metrics<\/td><\/tr><tr><td><strong>Infinite Loops<\/strong><\/td><td>Set maximum iteration limits<\/td><\/tr><tr><td><strong>Context Overflow<\/strong><\/td><td>Implement memory trimming strategies<\/td><\/tr><tr><td><strong>Security Vulnerabilities<\/strong><\/td><td>Validate tool inputs, use least privilege&nbsp;<a href=\"https:\/\/www.mhtechin.com\/support\/mhtechin-technologies-transforming-global-enterprises-with-cutting-edge-ai-solutions\/#respond\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Cost Optimization<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class CostTracker:\n    \"\"\"Track and optimize API costs.\"\"\"\n    \n    def __init__(self):\n        self.total_tokens = 0\n        self.total_cost = 0\n    \n    def add_usage(self, prompt_tokens: int, completion_tokens: int):\n        self.total_tokens += prompt_tokens + completion_tokens\n        # GPT-4o-mini pricing: $0.15\/1M input, $0.60\/1M output\n        cost = (prompt_tokens * 0.15 \/ 1_000_000) + (completion_tokens * 0.60 \/ 1_000_000)\n        self.total_cost += cost\n    \n    def report(self):\n        return {\n            \"total_tokens\": self.total_tokens,\n            \"total_cost_usd\": round(self.total_cost, 4)\n        }<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Part 10: MHTECHIN\u2019s Expertise in AI Agent Development<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">At&nbsp;<strong>MHTECHIN<\/strong>, we specialize in building cutting-edge AI solutions that empower businesses to harness the full potential of autonomous agents&nbsp;<a href=\"https:\/\/www.mhtechin.com\/support\/ai-for-conversational-agents-with-mhtechin\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>. Our expertise spans:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Conversational AI<\/strong>: Building intelligent chatbots and virtual assistants with natural language understanding<\/li>\n\n\n\n<li><strong>Multi-Agent Systems<\/strong>: Developing teams of specialized agents that collaborate on complex tasks<\/li>\n\n\n\n<li><strong>Reinforcement Learning<\/strong>: Creating agents that learn and optimize through experience<\/li>\n\n\n\n<li><strong>Enterprise Integration<\/strong>: Deploying secure, scalable AI agents that integrate with existing systems<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">MHTECHIN\u2019s AI solutions leverage state-of-the-art technologies including transformer models, reinforcement learning algorithms, and custom multi-agent frameworks to deliver production-ready autonomous systems&nbsp;<a href=\"https:\/\/www.mhtechin.com\/support\/mhtechin-technologies-transforming-global-enterprises-with-cutting-edge-ai-solutions\/#respond\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Building an autonomous AI agent from scratch is an achievable goal with the right approach. You\u2019ve learned how to:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Understand the fundamentals<\/strong>: What makes an agent autonomous and the core components required<\/li>\n\n\n\n<li><strong>Set up your environment<\/strong>: Python, API keys, and project structure<\/li>\n\n\n\n<li><strong>Build a basic agent<\/strong>: The agent loop with conversation management<\/li>\n\n\n\n<li><strong>Add memory systems<\/strong>: Short-term context and long-term vector storage<\/li>\n\n\n\n<li><strong>Integrate tools<\/strong>: Function calling and MCP servers for real-world actions<\/li>\n\n\n\n<li><strong>Implement advanced patterns<\/strong>: ReAct reasoning and multi-agent collaboration<\/li>\n\n\n\n<li><strong>Deploy with FastAPI<\/strong>: Production-ready APIs with safety guardrails<\/li>\n\n\n\n<li><strong>Test and optimize<\/strong>: Evaluation frameworks and cost management<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The journey from a simple chatbot to a fully autonomous agent is one of progressive enhancement. Start with the basic loop, add memory, integrate tools, and gradually increase autonomy with human oversight.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Next Steps:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Explore the open-source resources at\u00a0<a href=\"https:\/\/github.com\/niket-sharma\/AI-Agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\">AI-Agents-from-scratch GitHub<\/a>\u00a0for hands-on tutorials\u00a0<a href=\"https:\/\/github.com\/niket-sharma\/AI-Agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n\n\n\n<li>Check out AG2\u2019s documentation for advanced multi-agent patterns\u00a0<a href=\"https:\/\/docs.ag2.ai\/latest\/docs\/home\/quickstart\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/li>\n\n\n\n<li>Experiment with different tool integrations and reasoning strategies<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Frequently Asked Questions (FAQ)<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Q1: What is an autonomous AI agent?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">An autonomous AI agent is a system that can interpret tasks, make decisions, and take actions without direct human instruction. It uses LLMs for reasoning, tools for actions, and memory for context&nbsp;<a href=\"https:\/\/www.omdena.com\/blog\/autonomous-ai-agents\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Q2: Do I need a paid API key to build agents?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Free tier APIs from OpenAI or Anthropic are sufficient for learning and building basic agents. Production deployments will require paid access&nbsp;<a href=\"https:\/\/github.com\/niket-sharma\/AI-Agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Q3: What\u2019s the difference between LangChain and building from scratch?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Building from scratch teaches you fundamentals\u2014you\u2019ll understand what frameworks like LangChain do under the hood. Frameworks accelerate development but abstract away implementation details&nbsp;<a href=\"https:\/\/github.com\/the-ai-engineer\/ai-agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Q4: How do I prevent agents from making harmful decisions?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Implement guardrails for input validation, use human-in-the-loop for critical actions, set clear autonomy boundaries, and maintain audit trails of all decisions&nbsp;<a href=\"https:\/\/docs.ag2.ai\/latest\/docs\/home\/quickstart\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><a href=\"https:\/\/www.omdena.com\/blog\/autonomous-ai-agents\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Q5: Can I use open-source LLMs instead of OpenAI?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. AG2 supports multiple providers including Anthropic, local models via Ollama, and open-source models through Hugging Face&nbsp;<a href=\"https:\/\/pypi.org\/project\/ag2\/0.9.6\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><a href=\"https:\/\/docs.ag2.ai\/latest\/docs\/home\/quickstart\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Q6: How much does it cost to run an AI agent?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Costs vary based on usage. With GPT-4o-mini, simple conversations cost fractions of a cent. Heavy tool usage increases costs. Implement caching and model routing to optimize&nbsp;<a href=\"https:\/\/github.com\/the-ai-engineer\/ai-agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Q7: How do I test my agent\u2019s performance?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Use structured tests for tool calling accuracy, task completion rates, and response quality. Evaluate with metrics like success rate, latency, and hallucination detection&nbsp;<a href=\"https:\/\/github.com\/niket-sharma\/AI-Agents-from-scratch\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Q8: What\u2019s the best framework for multi-agent systems?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">AG2 (formerly AutoGen) is excellent for multi-agent workflows with built-in patterns for group chat, swarms, and nested conversations&nbsp;<a href=\"https:\/\/pypi.org\/project\/ag2\/0.9.6\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><a href=\"https:\/\/docs.ag2.ai\/latest\/docs\/home\/quickstart\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Imagine an AI that doesn\u2019t just answer questions but actively works toward goals\u2014researching topics, performing calculations, updating databases, and coordinating with other systems\u2014all with minimal human supervision. This is the promise of autonomous AI agents, and building one is more accessible than you might think. An&nbsp;autonomous AI agent&nbsp;is a system that can make decisions [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2833","post","type-post","status-publish","format-standard","hentry","category-support"],"_links":{"self":[{"href":"https:\/\/www.mhtechin.com\/support\/wp-json\/wp\/v2\/posts\/2833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mhtechin.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mhtechin.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mhtechin.com\/support\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mhtechin.com\/support\/wp-json\/wp\/v2\/comments?post=2833"}],"version-history":[{"count":4,"href":"https:\/\/www.mhtechin.com\/support\/wp-json\/wp\/v2\/posts\/2833\/revisions"}],"predecessor-version":[{"id":2859,"href":"https:\/\/www.mhtechin.com\/support\/wp-json\/wp\/v2\/posts\/2833\/revisions\/2859"}],"wp:attachment":[{"href":"https:\/\/www.mhtechin.com\/support\/wp-json\/wp\/v2\/media?parent=2833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mhtechin.com\/support\/wp-json\/wp\/v2\/categories?post=2833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mhtechin.com\/support\/wp-json\/wp\/v2\/tags?post=2833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}