DeepSeek Agent Development Tutorial
Build intelligent Agents from scratch. Master ReAct reasoning patterns, Function Calling tool invocation, multi-Agent collaboration, memory management, planning and execution. Complete Python code, ready to use.
Start LearningWhat is an AI Agent? Why do we need it?
An AI Agent is an AI system that can autonomously perceive its environment, make plans, call tools, and execute tasks. Unlike traditional Chatbots, Agents do not just answer questions but can actively think and act to complete complex workflows.
Agent Overview
Understanding the core concepts, architecture components, and essential differences between AI Agents and traditional Chatbots is the theoretical foundation for building intelligent agents.
Agent vs Traditional Chatbot
| Comparison Dimension | Traditional Chatbot | AI Agent |
|---|---|---|
| Interaction Mode | Single-turn Q&A, one question one answer | Multi-step reasoning, autonomously executes task chains |
| Capability Boundary | Only generates text based on training data | Can call external tools, APIs, databases |
| Memory Capability | Temporary memory within context window | Short-term memory + long-term memory (vector storage) |
| Task Complexity | Simple conversation, information retrieval | Multi-step tasks, workflow automation |
| Typical Scenarios | Customer service Q&A, chit-chat, content generation | Data analysis, automated reports, code execution, process orchestration |
Four Core Components of Agent
| Component | English | Function | Technical Implementation |
|---|---|---|---|
| Large Language Model | LLM | The brain of the Agent, responsible for reasoning and decision-making | DeepSeek-Chat / DeepSeek-Reasoner |
| Tools | Tools | The hands and feet of the Agent, executing specific operations | Function Calling / API / Code Executor |
| Memory | Memory | Stores and retrieves historical information | Conversation history / Vector database / Knowledge graph |
| Planning | Planning | Decomposes tasks, formulates execution strategies | ReAct / Plan-and-Execute / Tree-of-Thought |
Agent Workflow
- Perception: Receive user input, understand task intent and goals
- Thinking: Analyze the task, formulate an execution plan, decide next actions
- Action: Call tools to execute specific operations (search, calculation, API calls, etc.)
- Observation: Obtain tool execution results, evaluate whether the goal is achieved
- Loop: Decide whether to continue execution or output the final answer based on observations
This "Thinking-Action-Observation" loop is the core working pattern of an Agent. For more basic knowledge about DeepSeek models, please see DeepSeek Model Architecture Details.
ReAct Mode
ReAct (Reasoning + Acting) is the core reasoning mode of an Agent. It alternates reasoning and acting, allowing the model to call tools while thinking and verify reasoning through actions.
ReAct Loop: Thought - Action - Observation
| Step | Description | Example |
|---|---|---|
| Thought | Analyze the current state and decide what to do next | "I need to check today's weather, then decide whether to suggest bringing an umbrella" |
| Action | Execute a specific tool call | Call the get_weather("Beijing") function |
| Observation | Get the tool result and evaluate whether the task is complete | "Beijing is sunny today, 25 degrees, no need for an umbrella" |
DeepSeek + ReAct Complete Implementation
The following code shows a complete ReAct Agent that uses the DeepSeek model for reasoning and calls search and calculation tools:
Key Points of ReAct Mode
- Execute only one Action at a time, wait for Observation before continuing
- Thought and Action must strictly follow the format for easy regex parsing
- Set max_steps to prevent infinite loops, usually 5-10 steps is enough
- Tool functions need proper error handling and return meaningful results
- DeepSeek's reasoning ability is very suitable for ReAct mode
Function Calling Tool Invocation
DeepSeek supports native Function Calling capability, allowing the model to automatically identify when to call tools and generate structured function call parameters. This is more reliable and efficient than manually parsing the ReAct format.
Define Tool Schema
Define tools using the OpenAI-compatible Function Calling format:
Function Calling Agent Main Loop
Advantages of Function Calling
- The model automatically decides when to call tools, no need to manually parse formats
- Parameters are passed as structured JSON, avoiding regex parsing errors
- Supports parallel calling of multiple tools (set tool_choice to "auto")
- DeepSeek API is fully compatible with OpenAI Function Calling format
- Can be used in a mixed way: the model can call tools and generate text simultaneously in one response
LangChain Agent Integration
LangChain provides high-level Agent abstractions that encapsulate the ReAct loop and tool management. Using LangChain makes Agent development more concise and maintainable, while supporting multiple Agent types and tool combinations.
Install Dependencies
Create LangChain Agent
AgentExecutor Parameter Description
| Parameter | Description | Recommended Value |
|---|---|---|
| verbose | Whether to print detailed execution process | True during development, False in production |
| handle_parsing_errors | Automatically handle model output format errors | True |
| max_iterations | Maximum number of iterations to prevent infinite loops | 5-10 |
| early_stopping_method | Handling method after reaching maximum iterations | "generate" (generate final answer) |
Multi-Agent Collaboration
A single Agent has limited capabilities; complex tasks require multiple Agents to collaborate with division of labor. CrewAI is currently the most popular multi-agent framework, supporting the definition of Agents with different roles and collaboration in a process to complete tasks.
CrewAI Multi-Agent Architecture
- Agent: Defines role, goal, backstory, and available tools
- Task: Defines the specific work to be done, including description, expected output, and assigned Agent
- Crew: Organizes multiple Agents to collaborate sequentially or hierarchically to complete a set of tasks
- Process: Controls the execution order of tasks, supporting sequential and hierarchical
CrewAI Complete Example
Best Practices for Multi-Agent Collaboration
- Each Agent should only be responsible for one clear role to avoid overlapping responsibilities
- Task descriptions should be specific, including a clear expected_output format
- Research agents can be configured with search tools, and execution agents can be configured with code tools
- Hierarchical process is suitable for complex projects, but requires an additional manager Agent
- Set allow_delegation=False to prevent Agents from shifting tasks to each other
Agent Memory Management
Memory is the key capability that distinguishes Agents from ordinary Chatbots. Proper memory management allows Agents to remember historical conversations, user preferences, and long-term knowledge, thereby providing a more personalized and coherent interactive experience.
Three Types of Memory
| Memory Type | Storage Method | Lifecycle | Applicable Scenarios |
|---|---|---|---|
| Short-term Memory | Message list (context window) | Single session | Current conversation context, multi-turn interactions |
| Long-term Memory | Vector database / Knowledge graph | Persistent across sessions | User preferences, historical knowledge, project background |
| Working Memory | Structured data storage | Single task | Intermediate task results, execution status |
Short-term Memory: Conversation History Management
Long-term Memory: Vector Store Implementation
Hybrid Memory Architecture
Recommended memory architecture combination:
- Short-term memory: Use ConversationBufferWindowMemory (k=10) to retain recent conversations
- Long-term memory: Use ChromaDB vector store to store user preferences and project knowledge
- Working memory: Use Python dictionary to pass intermediate results within a single task
- Memory retrieval: Automatically retrieve relevant long-term memories before each conversation and inject them into the System Prompt
Planning and Execution
For complex tasks, an Agent needs to first decompose the task into executable subtasks, then execute them step by step. The Plan-and-Execute pattern separates planning and execution, enabling the Agent to handle more complex multi-step tasks.
Plan-and-Execute Pattern
- Plan (Planning Phase): The LLM analyzes the task and generates a detailed execution plan (list of steps)
- Execute (Execution Phase): The Agent executes step by step according to the plan, observing results at each step
- Replan (Replanning): If a step fails or the result does not meet expectations, dynamically adjust the subsequent plan
- Finalize (Completion): Aggregate the results of all steps and output the final answer
Complete Plan-and-Execute Implementation
Dynamic Plan Adjustment
When a step fails, you can have the Agent re-plan the remaining steps. After execute_step returns failure, call create_plan with the history of completed steps to let the model generate a new execution plan. This adaptive capability is a hallmark of advanced Agents.
RAG Agent
RAG Agent combines retrieval-augmented generation (RAG) with the agent's tool-calling capabilities, allowing the agent to both retrieve information from a knowledge base and call external tools. This is the most common architecture pattern for enterprise-grade agents.
RAG Agent Architecture
RAG Agent integrates two capabilities:
- Knowledge Retrieval: Retrieve relevant documents from a vector database to provide accurate contextual information
- Tool Calling: Call external APIs, execute code, query databases, and more
- Hybrid Decision-Making: The agent autonomously determines whether to retrieve from the knowledge base or call a tool
Complete RAG Agent Implementation
RAG Agent Decision Flow
| User Question Type | Agent Decision | Tool Used |
|---|---|---|
| "How to call the API?" | Knowledge-based question, search knowledge base | Knowledge Base Search |
| "Help me calculate the cost for 1 million tokens" | First search prices, then calculate | Knowledge Base Search + Python Execution |
| "Compare DeepSeek and GPT-4" | Knowledge-based question, search information on both models | Knowledge Base Search (multiple calls) |
For more on building a RAG knowledge base, please see DeepSeek RAG Knowledge Base Building Tutorial.
Safety and Guardrails
Agents have tool-calling capabilities, which means they may execute dangerous operations. Safety guardrails are an essential component of Agent systems, ensuring that Agents operate within controlled boundaries.
Four Layers of Agent Safety
| Protection Layer | Purpose | Implementation |
|---|---|---|
| Input Validation | Filter malicious inputs to prevent injection attacks | Regex filtering + content moderation API |
| Tool Permission Control | Restrict the tools and parameter ranges an Agent can invoke | Whitelist + parameter validation + rate limiting |
| Output Filtering | Filter sensitive information to prevent data leakage | Regex redaction + sensitive word filtering |
| Audit Logging | Record all Agent operations for traceability | Structured logs + database storage |
Code Implementation of Safety Guardrails
Security Best Practices
- Never give the Agent direct access to the file system or database; use restricted APIs instead
- Code execution tools must use a sandbox environment (Docker container or restricted Python)
- All external API calls must go through a proxy, limiting rate and access scope
- Regularly audit Agent logs to detect abnormal behavior promptly
- Use System Prompt to clearly inform the Agent of security boundaries and prohibited behaviors
Production Deployment
Moving an Agent from development to production requires a comprehensive engineering approach. This chapter introduces how to build an Agent service using FastAPI, deploy with Docker containers, and set up monitoring and logging.
FastAPI Agent Service
Docker Containerized Deployment
Monitoring and Logging
Production Environment Checklist
| Category | Check Item | Tool/Solution |
|---|---|---|
| Security | Input validation, output filtering, tool permissions | SafeAgent wrapper |
| Monitoring | Request volume, latency, error rate, tool calls | Prometheus + Grafana |
| Logging | Request logs, tool call logs, error logs | ELK / Loki / Structured logs |
| Rate Limiting | API rate limiting, concurrency control | slowapi / Redis + token bucket |
| Fault Tolerance | Retry mechanism, degradation strategy, health checks | tenacity + /health endpoint |
| Deployment | Containerization, rolling updates, auto-scaling | Docker + K8s + HPA |
Deployment Recommendations
For small-scale applications, a single-machine Docker Compose deployment is sufficient. For production-grade applications, it is recommended to use a K8s cluster deployment with Prometheus + Grafana monitoring and ELK log collection. The Agent service is stateless and can be scaled horizontally, but attention must be paid to the concurrency limits of the LLM API.
DeepSeek Agent Development FAQ
DeepSeek Related Tutorials
Learn more about using, deploying, and ecosystem tools for DeepSeek models.
How to Use DeepSeek Models
Four usage methods, zero-basics tutorial.
DeepSeek RAG Knowledge Base
Complete tutorial on document loading, vector embedding, and retrieval-augmented generation.
DeepSeek LangChain Development
Introduction to LangChain integration, Chain development, and tool calling.
DeepSeek Prompt Engineering
System Prompt design, few-shot, and chain-of-thought techniques.
DeepSeek Ecosystem Tools
WebUI, IDE plugins, Agent frameworks, RAG platforms.
DeepSeek Deployment Tutorial
Ollama, Docker, vLLM, K8s deployment solutions.