Build an AI Agent in 200 Lines of Python: A Deep Dive into Five Core Modules

Core architecture and module breakdown for building an AI Agent in 200 lines of Python
This article provides a detailed breakdown of a tutorial project that builds a simple AI Agent in just 200 lines of Python. The project decomposes an Agent into five core modules: Prompt (defining role and behavior), Memory (enabling multi-turn conversation context), Tool Use (interacting with the external world), RAG (answering questions based on external knowledge), and Skill (dynamically extending capabilities). Through a progressive development approach that layers each module incrementally, it helps developers understand Agent architecture from the ground up.
Why Build an AI Agent from Scratch?
In 2025, AI Agents have become one of the hottest directions in artificial intelligence. The concept of AI Agents originated from early AI research on "intelligent agents," but gained an entirely new implementation path with the rise of Large Language Models (LLMs). The core idea behind modern AI Agents is: enabling language models not just to "think" but also to "act" — by perceiving the environment, planning steps, invoking tools, and executing tasks, forming a closed-loop autonomous decision-making system. Unlike traditional Q&A-style AI, Agents possess goal-oriented behavior and multi-step reasoning capabilities, able to decompose complex tasks into executable sub-task sequences. Whether it's AutoGPT, MetaGPT, or various commercial Agent platforms, the underlying logic is actually not that complex. Rather than only knowing how to call pre-packaged frameworks built by others, it's better to understand the core Agent architecture from the ground up.
A beginner-friendly tutorial has recently gained widespread attention: building a fully functional simple AI Agent in just 200 lines of Python code. This project starts from the most basic prompt and progressively adds memory, tool calling, RAG (Retrieval-Augmented Generation), and Skill modules, ultimately constructing a working intelligent agent. This article provides an in-depth breakdown of the project's core architecture and key modules.

Breaking Down the Five Core Modules of an AI Agent
The greatest value of this project lies in decomposing the Agent concept into five clear modules, each corresponding to a key capability. Once you understand these five modules, you'll understand the underlying design philosophy of the vast majority of Agent frameworks on the market.

Prompt — The Agent's "Soul"
The prompt is the Agent's starting point and also the most easily underestimated component. A well-crafted System Prompt determines the Agent's role positioning, behavioral boundaries, and output style. In this project, the prompt module is the first component to be implemented, defining "who the Agent is" and "how it should behave."
Prompt Engineering has evolved into an independent engineering discipline. The System Prompt doesn't just define a role — it serves as the Agent's "constitution" — systematically influencing the model's reasoning path through techniques like few-shot examples, Chain-of-Thought guidance, and output format constraints. Research shows that structured prompt design can improve model performance on specific tasks by over 30%. A good prompt isn't just a descriptive paragraph but a structured instruction system encompassing role definition, task constraints, output format requirements, and multiple other dimensions. For beginners, mastering prompt engineering is the first step into Agent development.
Memory — Enabling the Agent to "Remember" Context
Large language models are inherently stateless — each conversation is independent. This characteristic stems from the Transformer architecture: each inference is an independent forward propagation process that retains no session state. The memory module's purpose is to compensate for this architectural limitation at the application layer, giving the Agent context awareness — the ability to remember previous conversation content and thus achieve coherent multi-turn interactions.
In practice, memory is typically divided into two categories:
- Short-term memory: The conversation history of the current session, usually implemented through "context window concatenation," but limited by the model's Context Length (e.g., GPT-4's 128K tokens)
- Long-term memory: Key information persisted across sessions, requiring vector databases (such as Pinecone, Chroma) or structured storage, retrieved on demand through retrieval mechanisms — this is also one of the foundational principles behind RAG technology
In this 200-line code project, the memory module implementation is relatively concise but sufficient to demonstrate the core principle — concatenating conversation history into the Prompt so the model can "see" previous interaction records during each inference.
Tool Use — Enabling the Agent to "Take Action"
A pure language model can only generate text, while tool calling gives the Agent the ability to interact with the external world. By defining a set of callable functions (such as search engines, calculators, database queries, etc.), the Agent can autonomously select and execute appropriate tools based on user needs.
The standardization of tool-calling capabilities has been an important milestone in the Agent field over the past two years. OpenAI introduced the Function Calling specification in 2023, allowing developers to describe function interfaces in JSON Schema format, with the model autonomously determining when to call them and what parameters to pass. This specification was subsequently adopted by major model providers including Anthropic and Google, gradually forming an industry standard. Frameworks like LangChain further abstract on this foundation, providing a unified Tool interface layer that enables the same Agent code to seamlessly switch between different underlying models. In code implementation, the key lies in how to make the model understand available tool descriptions and correctly generate tool-calling parameters.
RAG (Retrieval-Augmented Generation) — Enabling the Agent to "Look Things Up"
RAG (Retrieval-Augmented Generation) was formally proposed by Meta AI Research in 2020 in the paper Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks, addressing the problems of LLM knowledge cutoff and hallucination. Its core process consists of three steps: first, external documents are split into semantic chunks and converted into high-dimensional vectors via an Embedding model, then stored in a vector database; during query time, the user's question is similarly vectorized, and the most relevant document fragments are retrieved through algorithms like cosine similarity; finally, the retrieved results are injected as context into the Prompt, guiding the model to generate answers based on real data. By combining external knowledge bases with model reasoning, Agents can answer questions based on real data, effectively mitigating knowledge staleness caused by LLM training data cutoff dates, as well as the hallucination problem where models tend to "confidently fabricate" information when uncertain.

In this project, the RAG module implementation demonstrates the basic workflow from document chunking to vector storage to similarity retrieval. Although it's a simplified version, it fully covers RAG's core pipeline.
Skill Module — Enabling the Agent to "Learn" New Capabilities
Skill is a trending concept in the recent Agent field. Unlike tool calling, Skill places more emphasis on the Agent's extensibility and modularity — you can dynamically add new capability modules to an Agent, much like adding "skill points" to a character.
This design philosophy makes the Agent's capabilities no longer hard-coded but flexibly combinable and extensible based on scenario requirements, significantly improving the Agent's versatility and reusability.
Progressive Development: The 200-Line Code Development Workflow
The most valuable aspect of this project for learning is its progressive development methodology. Rather than writing all 200 lines of code at once, each module is added step by step:
- First, build the most basic LLM calling framework
- Add the prompt module to define the Agent's role
- Layer on memory functionality to enable multi-turn dialogue
- Integrate tool calling to extend interaction capabilities
- Introduce the RAG module to enhance knowledge retrieval
- Finally, add the Skill module to enable capability extension

Each step builds incrementally on the previous one. This approach not only lowers the learning barrier but also allows developers to clearly understand the independent role of each module and their interrelationships.
Getting Started with Agent Development: Learning Tips and Advanced Paths
For those looking to get started with AI Agent development, this project provides an excellent starting point. Here are some practical suggestions:
- Understand concepts before reading code: Don't rush to copy and run the code — first clarify what problem each module solves
- Basic Python is sufficient: The project doesn't require advanced programming skills — basic Python syntax is enough
- From simple version to production-grade: The 200 lines of code represent a minimum viable version for learning; after understanding the principles, you can gradually introduce mature frameworks like LangChain and LlamaIndex
- Focus on Agent design patterns: ReAct, Plan-and-Execute, Multi-Agent, and other patterns represent advanced directions
On the advanced path, several mainstream Agent design patterns are worth focusing on: ReAct (Reasoning + Acting) is currently the most prevalent Agent reasoning paradigm, proposed by Google Research in 2022. Its core idea is to have the model alternate between "thinking" (Thought) and "acting" (Action).
Related articles
TutorialsChatGPT Plus Subscription Guide: Are GPT-5.5, image-2, and Codex Worth the Upgrade?
A detailed look at ChatGPT Plus features — GPT-5.5, image-2, and Codex — with a Plus vs Pro comparison and a complete step-by-step subscription guide for users outside the US.
TutorialsHarness AI Engineering in Practice: Using Claude Code to Master Enterprise-Level E-Commerce Development
Deep dive into Harness AI Engineering: master enterprise e-commerce development with Claude Code using the Rules, Skills, Wiki, and Changes framework.
TutorialsCursor + Codex Dual-IDE Collaboration: A Practical Methodology for Open-Source Project Customization
A complete methodology for open-source project customization based on real-world experience, detailing the Cursor+Codex dual-IDE workflow, seven-stage process, MVP validation, and AI source code reading techniques.