Extending Tools & Agents
Relevant source files
The following files were used as context for generating this wiki page:
The DeepCode system utilizes a multi-agent orchestration framework designed to bridge the gap between academic research papers and production-ready code. This system relies on specialized agents—such as the ConciseMemoryAgent and CodeImplementationAgent—coordinated via Model Context Protocol (MCP) servers to execute complex, multi-step implementation workflows.
Extending the system involves defining new prompt templates for specialized logic, configuring additional MCP tools for environment manipulation, and integrating these into the iterative development cycle. The architecture emphasizes a "clean slate" memory approach, where agent context is pruned after critical actions like file writes to maintain focus and efficiency during long-running implementation sessions.
Sources: workflows/code_implementation_workflow.py:12-25, workflows/agents/memory_agent_concise.py:17-30
Agent Architecture and Interaction
The system employs a modular agent design where each agent is defined by a specific set of instructions (prompts) and tool access. The primary workflow involves a CodeImplementationAgent supported by a ConciseMemoryAgent to manage long-term implementation context without overwhelming the LLM's context window.
Multi-Agent Orchestration Flow
The following diagram illustrates how the Workflow Manager coordinates between different specialized agents and MCP tools.
The Workflow Manager acts as the orchestrator, initializing the agents and directing the implementation loop based on the reproduction plan.
Sources: workflows/code_implementation_workflow.py:177-210, workflows/agents/memory_agent_concise.py:440-480
Core Agent Types
| Agent Type | Primary Responsibility | Key Files |
|---|---|---|
| Structure Generator | Analyzes plans to generate shell commands for directory structures. | prompts/code_prompts.py |
| Code Implementation | Transforms technical requirements into production-ready code. | workflows/code_implementation_workflow.py |
| Concise Memory | Manages context by pruning history and maintaining a Knowledge Base of implemented code. | workflows/agents/memory_agent_concise.py |
| Paper Analyzer | Specialized in extracting algorithms and concepts from academic documents. | prompts/code_prompts.py |
Sources: prompts/code_prompts.py:461-480, workflows/agents/memory_agent_concise.py:32-45
Extending Agent Capabilities via Prompts
Agents are extended by defining new structured prompts in prompts/code_prompts.py. These prompts use a structured format (often YAML or JSON) to ensure the LLM returns executable or parsable data.
Prompt Customization Patterns
The system uses several prompt archetypes for different stages of the lifecycle:
- Analysis Prompts: Focus on extraction (e.g.,
PAPER_ALGORITHM_ANALYSIS_PROMPT). - Planning Prompts: Focus on structure and dependencies (e.g.,
CHAT_AGENT_PLANNING_PROMPT). - Implementation Prompts: Focus on zero-placeholder, production-grade code (e.g.,
GENERAL_CODE_IMPLEMENTATION_SYSTEM_PROMPT).
# Example of a planning prompt structure
CHAT_AGENT_PLANNING_PROMPT = """
# 🎯 OBJECTIVE
Transform user requirements into a clear, actionable implementation plan...
# 📋 OUTPUT FORMAT
```yaml
project_plan:
title: "[Project Name]"
file_structure: |
# ACTUAL file tree here
implementation_steps:
1. "[Step 1]"
"""
Sources: [prompts/code_prompts.py:848-890](https://github.com/LoveLogicAILLC/DeepCode/blob/HEAD/prompts/code_prompts.py#L848-L890), [prompts/code_prompts.py:100-120](https://github.com/LoveLogicAILLC/DeepCode/blob/HEAD/prompts/code_prompts.py#L100-L120)
## Tool Integration via MCP
The Model Context Protocol (MCP) is the backbone for tool extension. Tools are organized into essential categories that the agents call during the iterative implementation loop.
### Essential Tool Categories
Agents are configured to track and utilize specific "Essential Tools" for state management and execution:
1. **File Operations**: `read_file`, `write_file`, `read_code_mem`.
2. **Code Analysis**: `search_code`, `search_reference_code`, `get_file_structure`.
3. **Execution**: `execute_python`, `execute_bash`.
Sources: [workflows/agents/memory_agent_concise.py:46-55](https://github.com/LoveLogicAILLC/DeepCode/blob/HEAD/workflows/agents/memory_agent_concise.py#L46-L55), [workflows/code_implementation_workflow.py:228-245](https://github.com/LoveLogicAILLC/DeepCode/blob/HEAD/workflows/code_implementation_workflow.py#L228-L245)
### Tool Execution Sequence
The following sequence diagram shows the typical "Development Cycle" for a new file implementation.
Memory Management and State
To prevent token overflow and role confusion, the ConciseMemoryAgent implements a "Clean Slate" strategy.
Implementation Tracking
The memory agent maintains a code_summary_path (typically implement_code_summary.md) which acts as the persistent knowledge base.
- Trigger: Memory clearing is triggered immediately after a
write_filecall is detected. - Retention: Only the System Prompt, Initial Plan, and the LATEST tool results are kept in the active context window.
- Next Steps: The agent identifies the single next file to implement based on logical dependencies and the unimplemented file list.
Sources: workflows/agents/memory_agent_concise.py:60-85, workflows/agents/memory_agent_concise.py:650-675
Memory Optimization Statistics
The system tracks several metrics to evaluate agent performance during extension:
| Statistic | Description |
|---|---|
implementation_progress_percent | Percentage of files from the initial plan successfully written to disk. |
compression_ratio | The reduction in message history size after applying concise optimization. |
essential_tools_recorded | Log of critical tool calls (e.g., execute_bash) preserved for the next round. |
Sources: workflows/agents/memory_agent_concise.py:615-640, workflows/code_implementation_workflow.py:460-475
Conclusion
Extending DeepCode requires a dual approach: refining the LLM's behavioral boundaries through specialized prompts in prompts/code_prompts.py and expanding the operational capabilities via MCP-compliant tools. The iterative nature of the CodeImplementationWorkflow ensures that as new tools or agents are added, they benefit from the structured memory pruning and dependency-aware implementation logic already established in the core architecture.
Sources: workflows/code_implementation_workflow.py:480-505, workflows/agents/memory_agent_concise.py:740-755