Building AI Agents from Scratch | Full Course
By The Neural Maze
Agentic Design Patterns: A Comprehensive Compilation
Key Concepts:
- Agentic AI
- Reflection Pattern
- Tool Use Pattern
- Planning Pattern (React)
- Multi-Agent Pattern
- LLMs (Large Language Models)
- Groq (LLM Provider)
- Agent
- Tool
- Crew
1. Introduction
This video compiles four previous lessons on agentic design patterns into a single, comprehensive open-source course. The goal is to provide a practical starting point for agentic AI development without relying on specific frameworks. The course implements four key patterns from scratch using Python and Groq's LLMs.
2. Course Structure
The course is structured into four modules:
- Module 1: Reflection Pattern: Implementing a reflection agent.
- Module 2: Tool Use Pattern: Implementing tools, tool decorators, and a tool agent.
- Module 3: Planning Pattern (React): Implementing the React pattern and a React agent.
- Module 4: Multi-Agent Pattern: Implementing a multi-agent framework similar to Crew AI and Apache Airflow.
3. Reflection Pattern (Module 1)
3.1. Overview
The reflection pattern allows an LLM to critique and refine its own outputs. It involves a loop with two main blocks:
- Generate: The LLM generates an initial output based on a user prompt.
- Reflect: The LLM critiques the generated output and provides feedback.
The feedback is then used to generate a revised version of the output, and the loop continues.
3.2. Workflow
- User Prompt: A user provides a prompt (e.g., "Generate an essay about Baldi").
- Generate Block: The LLM (e.g., Groq) generates an initial essay.
- Reflect Block: The LLM critiques the essay and provides suggestions.
- Iteration: The generate block uses the feedback to generate a revised essay (V2).
- Stopping Criteria: The loop can be stopped after a fixed number of steps or when a specific stop keyword is generated.
3.3. Implementation
The implementation involves two chat histories: one for the generate block and one for the reflect block. The system prompt for the generate block instructs it to behave as a Python programmer, while the system prompt for the reflect block instructs it to behave as Andrej Karpathy, providing critique and recommendations.
3.4. Example
The example involves generating a Python implementation of the merge sort algorithm. The LLM generates an initial implementation, which is then critiqued by the reflect block. The generate block then uses the feedback to generate a revised implementation with docstrings and additional test cases.
3.5. Python Class Implementation
A ReflectionAgent class is implemented to encapsulate the reflection pattern. It includes generate and reflect methods, as well as a run method that executes the reflection loop. The class uses colorama for colored logging.
4. Tool Use Pattern (Module 2)
4.1. Overview
The tool use pattern allows an LLM to access the outside world by using external tools (e.g., Python functions). A tool is a function that the LLM can call to retrieve relevant information.
4.2. Workflow
- User Prompt: A user provides a prompt that requires external information (e.g., "What's the current temperature in Madrid?").
- Tool Selection: The LLM selects an appropriate tool based on the prompt.
- Tool Execution: The LLM executes the tool with the necessary arguments.
- Observation: The LLM observes the output of the tool.
- Response: The LLM generates a response based on the observation.
4.3. Implementation
The implementation involves defining a system prompt that instructs the LLM to behave as a function calling AI model. The system prompt includes the function signatures of available tools in XML format. The LLM is expected to return the function call in XML format as well.
4.4. Example
The example involves defining a get_current_weather function that returns the temperature in a given location. The LLM is prompted to get the current temperature in Madrid, and it returns the function call with the location and unit arguments. The function call is then parsed, and the get_current_weather function is executed. The output is then used to generate a response.
4.5. Python Class Implementation
A Tool class is implemented to represent a tool. It includes the tool's name, function, and function signature. A tool decorator is implemented to automatically transform a Python function into a Tool object. A ToolAgent class is implemented to encapsulate the tool use pattern. It includes a list of available tools and a run method that executes the tool use loop.
4.6. Hacker News Tool
A more realistic example involves implementing a tool that fetches the top N stories from Hacker News. The fetch_top_hacker_news_stories function is decorated with the tool decorator, and the resulting Tool object is used by the ToolAgent.
5. Planning Pattern (React) (Module 3)
5.1. Overview
The React (Reason and Act) pattern improves the planning and reasoning capabilities of LLMs. It involves a loop with three steps:
- Thought: The LLM thinks about the current state and decides what to do next.
- Action: The LLM executes a tool based on its thought.
- Observation: The LLM observes the output of the tool.
The loop continues until the LLM generates a final response.
5.2. Workflow
- User Prompt: A user provides a complex prompt that requires multiple steps (e.g., "Calculate the sum of 1234 and 5678 and multiply the result by 5, then take the logarithm of this result").
- Thought: The LLM thinks about the prompt and decides on the first step.
- Action: The LLM executes a tool based on its thought (e.g.,
sum_two_elements). - Observation: The LLM observes the output of the tool.
- Iteration: The LLM repeats steps 2-4 until it has completed all the steps.
- Response: The LLM generates a final response.
5.3. Implementation
The implementation involves defining a system prompt that instructs the LLM to operate by running a loop with the thought, action, and observation steps. The system prompt includes the function signatures of available tools in XML format. The LLM is expected to return the thought and function call in XML format as well.
5.4. Example
The example involves defining three tools: sum_two_elements, multiply_two_elements, and compute_log. The LLM is prompted to calculate the sum of 1234 and 5678 and multiply the result by 5, then take the logarithm of this result. The LLM executes the tools in the correct order and generates a final response.
5.5. Python Class Implementation
A ReactAgent class is implemented to encapsulate the React pattern. It includes a list of available tools and a run method that executes the React loop. The run method includes logic for extracting the thought and function call from the LLM's output, executing the tool, and updating the chat history.
6. Multi-Agent Pattern (Module 4)
6.1. Overview
The multi-agent pattern divides a task into smaller subtasks that are executed by different agents. Each agent adopts a specific role and solves a smaller task.
6.2. Approach
The approach involves creating a minimalist version of Crew AI, drawing inspiration from Crew AI's key abstractions (crew and agent) and Airflow's design philosophy (right shift and left shift operators).
6.3. Agent Class
An Agent class is implemented to represent an agent. It includes the agent's name, backstory, task description, task expected output, and a list of tools. The Agent class is based on the ReactAgent class, allowing agents to use tools and reason about their actions.
6.4. Crew Class
A Crew class is implemented to represent a crew of agents. It includes a list of agents and methods for adding agents, defining dependencies between agents, and running the crew. The Crew class uses a topological sort algorithm to determine the order in which the agents should be run.
6.5. Workflow
- Define Agents: Define multiple agents with specific roles and tasks.
- Define Dependencies: Define the dependencies between the agents.
- Create Crew: Create a crew object and add the agents to the crew.
- Run Crew: Run the crew object, which will execute the agents in the correct order based on their dependencies.
6.6. Example
The example involves defining three agents: a poet agent, a poem translator agent, and a writer agent. The poet agent generates a poem in English, the poem translator agent translates the poem into Spanish, and the writer agent writes the Spanish poem to a text file. The dependencies between the agents are defined such that the poem translator agent depends on the poet agent, and the writer agent depends on the poem translator agent.
6.7. Graph Visualization
The implementation includes a function for visualizing the dependencies between the agents using a graph. This allows for a clear understanding of the relationships between the agents.
7. Conclusion
This course provides a comprehensive overview of agentic design patterns, including the reflection pattern, tool use pattern, planning pattern (React), and multi-agent pattern. The course implements these patterns from scratch using Python and Groq's LLMs, providing a practical starting point for agentic AI development. The multi-agent pattern implementation draws inspiration from Crew AI and Apache Airflow, providing a solid foundation for building complex multi-agent applications.
Chat with this Video
AI-PoweredLoad the transcript when you're ready to chat so the initial page stays lighter.
Related Videos

How the hometown humiliation of Putin marks a turning point for Ukraine | DW News
DW News

Shocking video shows moment paramedics are hit by Israel in 'double-tap' strike
Sky News

Inside Jeffrey Epstein's Network of Power
Bloomberg Originals

Putin Xi, To Catch a Castro, Red Carpet Rebellion • FRANCE 24 English
FRANCE 24 English

Trump's supporters furious over Trump smartphone scam.
ABC News In-depth

Nvidia Crushes Earnings again — What Jensen Huang sees next for AI
CGTN America

Throwing out the first pitch for the Rockies for STEM Day!
Sick Science!