Skip to content

Agents API

This page provides comprehensive documentation for the DeepCritical agent system, including specialized agents for different research tasks.

Agent Framework

Agent Types

The AgentType enum defines the different types of agents available in the system:

  • SEARCH: Web search and information retrieval
  • RAG: Retrieval-augmented generation
  • BIOINFORMATICS: Biological data analysis
  • EXECUTOR: Tool execution and workflow management
  • EVALUATOR: Result evaluation and quality assessment

Agent Dependencies

AgentDependencies provides the configuration and context needed for agent execution, including model settings, API keys, and tool configurations.

Specialized Agents

Code Execution Agents

CodeGenerationAgent

The CodeGenerationAgent uses AI models to generate code from natural language descriptions, supporting multiple programming languages including Python and Bash.

CodeExecutionAgent

The CodeExecutionAgent safely executes generated code in isolated environments with comprehensive error handling and resource management.

CodeExecutionAgentSystem

The CodeExecutionAgentSystem coordinates code generation and execution workflows with integrated error recovery and improvement capabilities.

Code Improvement Agent

The Code Improvement Agent provides intelligent error analysis and code enhancement capabilities for automatic error correction and code optimization.

CodeImprovementAgent

Agent that analyzes errors and improves code/scripts.

Initialize the code improvement agent.

Parameters:

Name Type Description Default
model_name str

The model to use for code improvement

'anthropic:claude-sonnet-4-0'
max_improvement_attempts int

Maximum number of improvement attempts

3
timeout float

Timeout for improvement operations

60.0

Methods:

Name Description
analyze_error

Analyze an execution error and provide insights.

create_improved_code_block

Create a CodeBlock from improvement results.

improve_code

Improve code based on error analysis.

iterative_improve

Iteratively improve code until it works or max iterations reached.

Attributes:

Name Type Description
analysis_agent
improvement_agent
max_improvement_attempts
model_name
optimization_agent
timeout
Source code in DeepResearch/src/agents/code_improvement_agent.py
def __init__(
    self,
    model_name: str = "anthropic:claude-sonnet-4-0",
    max_improvement_attempts: int = 3,
    timeout: float = 60.0,
):
    """Initialize the code improvement agent.

    Args:
        model_name: The model to use for code improvement
        max_improvement_attempts: Maximum number of improvement attempts
        timeout: Timeout for improvement operations
    """
    self.model_name = model_name
    self.max_improvement_attempts = max_improvement_attempts
    self.timeout = timeout

    # Initialize Pydantic AI agents
    self.improvement_agent = self._create_improvement_agent()
    self.analysis_agent = self._create_analysis_agent()
    self.optimization_agent = self._create_optimization_agent()

Attributes

analysis_agent instance-attribute

analysis_agent = _create_analysis_agent()

improvement_agent instance-attribute

improvement_agent = _create_improvement_agent()

max_improvement_attempts instance-attribute

max_improvement_attempts = max_improvement_attempts

model_name instance-attribute

model_name = model_name

optimization_agent instance-attribute

optimization_agent = _create_optimization_agent()

timeout instance-attribute

timeout = timeout

Functions

analyze_error async

analyze_error(
    code: str,
    error_message: str,
    language: str,
    context: dict[str, Any] | None = None,
) -> dict[str, Any]

Analyze an execution error and provide insights.

Parameters:

Name Type Description Default
code str

The code that failed

required
error_message str

The error message from execution

required
language str

The language of the code

required
context dict[str, Any] | None

Additional context about the execution

None

Returns:

Type Description
dict[str, Any]

Dictionary with error analysis

Source code in DeepResearch/src/agents/code_improvement_agent.py
    async def analyze_error(
        self,
        code: str,
        error_message: str,
        language: str,
        context: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Analyze an execution error and provide insights.

        Args:
            code: The code that failed
            error_message: The error message from execution
            language: The language of the code
            context: Additional context about the execution

        Returns:
            Dictionary with error analysis
        """
        context_info = context or {}
        execution_context = f"""
Execution Context:
- Language: {language}
- Working Directory: {context_info.get("working_directory", "unknown")}
- Environment: {context_info.get("environment", "unknown")}
- Timeout: {context_info.get("timeout", "unknown")}
"""

        analysis_prompt = f"""
Please analyze this code execution error:

ORIGINAL CODE:
```python
{code}
```

ERROR MESSAGE:
```
{error_message}
```

{execution_context}

Provide a detailed analysis of what went wrong and how to fix it.
"""

        result = await self.analysis_agent.run(analysis_prompt)
        analysis_response = str(result.data).strip()

        # Parse the structured response
        analysis = self._parse_analysis_response(analysis_response)

        return {
            "error_type": analysis.get("error_type", "unknown"),
            "root_cause": analysis.get("root_cause", "Unable to determine"),
            "impact": analysis.get("impact", "Prevents code execution"),
            "recommendations": analysis.get("recommendations", []),
            "prevention": analysis.get("prevention", "Add proper error handling"),
            "raw_analysis": analysis_response,
        }

create_improved_code_block

create_improved_code_block(
    improvement_result: dict[str, Any],
) -> CodeBlock

Create a CodeBlock from improvement results.

Parameters:

Name Type Description Default
improvement_result dict[str, Any]

Result from improve_code method

required

Returns:

Type Description
CodeBlock

CodeBlock instance

Source code in DeepResearch/src/agents/code_improvement_agent.py
def create_improved_code_block(
    self, improvement_result: dict[str, Any]
) -> CodeBlock:
    """Create a CodeBlock from improvement results.

    Args:
        improvement_result: Result from improve_code method

    Returns:
        CodeBlock instance
    """
    return CodeBlock(
        code=improvement_result["improved_code"],
        language=improvement_result["language"],
    )

improve_code async

improve_code(
    original_code: str,
    error_message: str,
    language: str,
    context: dict[str, Any] | None = None,
    improvement_focus: str = "fix_errors",
) -> dict[str, Any]

Improve code based on error analysis.

Parameters:

Name Type Description Default
original_code str

The original code that failed

required
error_message str

The error message from execution

required
language str

The language of the code

required
context dict[str, Any] | None

Additional execution context

None
improvement_focus str

Focus of improvement ("fix_errors", "optimize", "robustness")

'fix_errors'

Returns:

Type Description
dict[str, Any]

Dictionary with improved code and analysis

Source code in DeepResearch/src/agents/code_improvement_agent.py
async def improve_code(
    self,
    original_code: str,
    error_message: str,
    language: str,
    context: dict[str, Any] | None = None,
    improvement_focus: str = "fix_errors",
) -> dict[str, Any]:
    """Improve code based on error analysis.

    Args:
        original_code: The original code that failed
        error_message: The error message from execution
        language: The language of the code
        context: Additional execution context
        improvement_focus: Focus of improvement ("fix_errors", "optimize", "robustness")

    Returns:
        Dictionary with improved code and analysis
    """
    context_info = context or {}

    if improvement_focus == "fix_errors":
        improvement_prompt = self._create_error_fix_prompt(
            original_code, error_message, language, context_info
        )
        agent = self.improvement_agent
    elif improvement_focus == "optimize":
        improvement_prompt = self._create_optimization_prompt(
            original_code, language, context_info
        )
        agent = self.optimization_agent
    else:  # robustness
        improvement_prompt = self._create_robustness_prompt(
            original_code, language, context_info
        )
        agent = self.improvement_agent

    result = await agent.run(improvement_prompt)
    improvement_response = str(result.data).strip()

    # Parse the improvement response
    improved_code = self._extract_improved_code(improvement_response)
    explanation = self._extract_explanation(improvement_response)

    return {
        "original_code": original_code,
        "improved_code": improved_code,
        "language": language,
        "improvement_focus": improvement_focus,
        "explanation": explanation,
        "raw_response": improvement_response,
    }

iterative_improve async

iterative_improve(
    code: str,
    language: str,
    test_function: Any,
    max_iterations: int = 3,
    context: dict[str, Any] | None = None,
) -> dict[str, Any]

Iteratively improve code until it works or max iterations reached.

Parameters:

Name Type Description Default
code str

Initial code to improve

required
language str

Language of the code

required
test_function Any

Function to test code execution (should return error message or None)

required
max_iterations int

Maximum improvement iterations

3
context dict[str, Any] | None

Additional context

None

Returns:

Type Description
dict[str, Any]

Dictionary with final result and improvement history

Source code in DeepResearch/src/agents/code_improvement_agent.py
async def iterative_improve(
    self,
    code: str,
    language: str,
    test_function: Any,
    max_iterations: int = 3,
    context: dict[str, Any] | None = None,
) -> dict[str, Any]:
    """Iteratively improve code until it works or max iterations reached.

    Args:
        code: Initial code to improve
        language: Language of the code
        test_function: Function to test code execution (should return error message or None)
        max_iterations: Maximum improvement iterations
        context: Additional context

    Returns:
        Dictionary with final result and improvement history
    """
    improvement_history = []
    current_code = code

    for iteration in range(max_iterations):
        # Test current code
        error_message = await test_function(current_code, language)

        if error_message is None:
            # Code works successfully
            return {
                "success": True,
                "final_code": current_code,
                "iterations_used": iteration,
                "improvement_history": improvement_history,
                "error_message": None,
            }

        # Analyze the error
        analysis = await self.analyze_error(
            current_code, error_message, language, context
        )

        # Improve the code
        improvement = await self.improve_code(
            current_code, error_message, language, context, "fix_errors"
        )

        # Update for next iteration
        current_code = improvement["improved_code"]

        improvement_history.append(
            {
                "iteration": iteration + 1,
                "original_code": improvement["original_code"],
                "error_message": error_message,
                "analysis": analysis,
                "improvement": improvement,
            }
        )

    # Max iterations reached, return best attempt
    final_error = await test_function(current_code, language)

    return {
        "success": final_error is None,
        "final_code": current_code,
        "iterations_used": max_iterations,
        "improvement_history": improvement_history,
        "error_message": final_error,
    }

The CodeImprovementAgent analyzes execution errors and provides intelligent code corrections and optimizations with multi-step improvement tracking.

Key Capabilities: - Intelligent Error Analysis: Analyzes execution errors and identifies root causes - Automatic Code Correction: Generates corrected code based on error analysis - Iterative Improvement: Multi-step improvement process with configurable retry logic - Multi-Language Support: Support for Python, Bash, and other programming languages - Performance Optimization: Code efficiency and resource usage improvements - Robustness Enhancement: Error handling and input validation improvements

Usage:

from DeepResearch.src.agents.code_improvement_agent import CodeImprovementAgent

# Initialize agent
agent = CodeImprovementAgent(
    model_name="anthropic:claude-sonnet-4-0",
    max_improvement_attempts=3
)

# Analyze error
analysis = await agent.analyze_error(
    code="print(undefined_var)",
    error_message="NameError: name 'undefined_var' is not defined",
    language="python"
)
print(f"Error type: {analysis['error_type']}")
print(f"Root cause: {analysis['root_cause']}")

# Improve code
improvement = await agent.improve_code(
    original_code="print(undefined_var)",
    error_message="NameError: name 'undefined_var' is not defined",
    language="python",
    improvement_focus="fix_errors"
)
print(f"Improved code: {improvement['improved_code']}")

# Iterative improvement
result = await agent.iterative_improve(
    code="def divide(a, b): return a / b\nresult = divide(10, 0)",
    language="python",
    test_function=my_execution_test,
    max_iterations=3
)
if result["success"]:
    print(f"Final working code: {result['final_code']}")

Error Analysis Methods

analyze_error() - Analyzes execution errors and provides detailed insights - Returns error type, root cause, impact assessment, and recommendations

improve_code() - Generates improved code based on error analysis - Supports different improvement focuses (error fixing, optimization, robustness)

iterative_improve() - Performs multi-step improvement until code works or max attempts reached - Includes comprehensive improvement history tracking

Multi-Agent Orchestrator

AgentOrchestrator

The AgentOrchestrator provides coordination for multiple specialized agents in complex workflows.

Code Execution Orchestrator

The Code Execution Orchestrator provides high-level coordination for code generation, execution, and improvement workflows.

CodeExecutionOrchestrator

Orchestrator for code generation and execution workflows.

Initialize the code execution orchestrator.

Parameters:

Name Type Description Default
config CodeExecutionConfig | None

Configuration for the orchestrator

None

Methods:

Name Description
analyze_and_improve_code

Analyze an error and improve the code.

execute_code_only

Execute code without generating it.

generate_code_only

Generate code without executing it.

get_config

Get current configuration.

get_supported_environments

Get list of supported execution environments.

iterative_improve_and_execute

Iteratively improve and execute code until it works.

process_request

Process a user request for code generation and execution.

update_config

Update orchestrator configuration.

Attributes:

Name Type Description
agent_system
config
execution_agent
generation_agent
improvement_agent
workflow
Source code in DeepResearch/src/agents/code_execution_orchestrator.py
def __init__(self, config: CodeExecutionConfig | None = None):
    """Initialize the code execution orchestrator.

    Args:
        config: Configuration for the orchestrator
    """
    self.config = config or CodeExecutionConfig()

    # Initialize agents
    self.generation_agent = CodeGenerationAgent(
        model_name=self.config.generation_model,
        max_retries=self.config.max_retries,
        timeout=self.config.generation_timeout,
    )

    self.execution_agent = CodeExecutionAgent(
        model_name=self.config.generation_model,
        use_docker=self.config.use_docker,
        use_jupyter=self.config.use_jupyter,
        jupyter_config=self.config.jupyter_config,
        max_retries=self.config.max_retries,
        timeout=self.config.execution_timeout,
    )

    # Initialize improvement agent
    from DeepResearch.src.agents.code_improvement_agent import CodeImprovementAgent

    self.improvement_agent = CodeImprovementAgent(
        model_name=self.config.generation_model,
        max_improvement_attempts=self.config.max_improvement_attempts,
    )

    self.agent_system = CodeExecutionAgentSystem(
        generation_model=self.config.generation_model,
        execution_config={
            "use_docker": self.config.use_docker,
            "use_jupyter": self.config.use_jupyter,
            "jupyter_config": self.config.jupyter_config,
            "max_retries": self.config.max_retries,
            "timeout": self.config.execution_timeout,
        },
    )

    # Initialize workflow
    self.workflow = CodeExecutionWorkflow() if self.config.use_workflow else None

Attributes

agent_system instance-attribute

agent_system = CodeExecutionAgentSystem(
    generation_model=generation_model,
    execution_config={
        "use_docker": use_docker,
        "use_jupyter": use_jupyter,
        "jupyter_config": jupyter_config,
        "max_retries": max_retries,
        "timeout": execution_timeout,
    },
)

config instance-attribute

config = config or CodeExecutionConfig()

execution_agent instance-attribute

execution_agent = CodeExecutionAgent(
    model_name=generation_model,
    use_docker=use_docker,
    use_jupyter=use_jupyter,
    jupyter_config=jupyter_config,
    max_retries=max_retries,
    timeout=execution_timeout,
)

generation_agent instance-attribute

generation_agent = CodeGenerationAgent(
    model_name=generation_model,
    max_retries=max_retries,
    timeout=generation_timeout,
)

improvement_agent instance-attribute

improvement_agent = CodeImprovementAgent(
    model_name=generation_model,
    max_improvement_attempts=max_improvement_attempts,
)

workflow instance-attribute

workflow = CodeExecutionWorkflow() if use_workflow else None

Functions

analyze_and_improve_code async

analyze_and_improve_code(
    code: str,
    error_message: str,
    language: str,
    context: dict[str, Any] | None = None,
) -> dict[str, Any]

Analyze an error and improve the code.

Parameters:

Name Type Description Default
code str

The code that failed

required
error_message str

The error message from execution

required
language str

Language of the code

required
context dict[str, Any] | None

Additional context

None

Returns:

Type Description
dict[str, Any]

Improvement results with analysis and improved code

Source code in DeepResearch/src/agents/code_execution_orchestrator.py
async def analyze_and_improve_code(
    self,
    code: str,
    error_message: str,
    language: str,
    context: dict[str, Any] | None = None,
) -> dict[str, Any]:
    """Analyze an error and improve the code.

    Args:
        code: The code that failed
        error_message: The error message from execution
        language: Language of the code
        context: Additional context

    Returns:
        Improvement results with analysis and improved code
    """
    # Analyze the error
    analysis = await self.improvement_agent.analyze_error(
        code=code, error_message=error_message, language=language, context=context
    )

    # Improve the code
    improvement = await self.improvement_agent.improve_code(
        original_code=code,
        error_message=error_message,
        language=language,
        context=context,
        improvement_focus="fix_errors",
    )

    return {
        "analysis": analysis,
        "improvement": improvement,
        "original_code": code,
        "improved_code": improvement["improved_code"],
        "language": language,
    }

execute_code_only async

execute_code_only(
    code: str, language: str, **kwargs
) -> dict[str, Any]

Execute code without generating it.

Parameters:

Name Type Description Default
code str

Code to execute

required
language str

Language of the code

required
**kwargs

Execution parameters

{}

Returns:

Type Description
dict[str, Any]

Execution results dictionary

Source code in DeepResearch/src/agents/code_execution_orchestrator.py
async def execute_code_only(
    self, code: str, language: str, **kwargs
) -> dict[str, Any]:
    """Execute code without generating it.

    Args:
        code: Code to execute
        language: Language of the code
        **kwargs: Execution parameters

    Returns:
        Execution results dictionary
    """
    return await self.execution_agent.execute_code(code, language)

generate_code_only async

generate_code_only(
    user_message: str, code_type: str | None = None
) -> tuple[str, str]

Generate code without executing it.

Parameters:

Name Type Description Default
user_message str

Natural language description

required
code_type str | None

Optional code type specification

None

Returns:

Type Description
tuple[str, str]

Tuple of (detected_code_type, generated_code)

Source code in DeepResearch/src/agents/code_execution_orchestrator.py
async def generate_code_only(
    self, user_message: str, code_type: str | None = None
) -> tuple[str, str]:
    """Generate code without executing it.

    Args:
        user_message: Natural language description
        code_type: Optional code type specification

    Returns:
        Tuple of (detected_code_type, generated_code)
    """
    return await self.generation_agent.generate_code(user_message, code_type)

get_config

get_config() -> dict[str, Any]

Get current configuration.

Source code in DeepResearch/src/agents/code_execution_orchestrator.py
def get_config(self) -> dict[str, Any]:
    """Get current configuration."""
    return self.config.dict()

get_supported_environments

get_supported_environments() -> list[str]

Get list of supported execution environments.

Source code in DeepResearch/src/agents/code_execution_orchestrator.py
def get_supported_environments(self) -> list[str]:
    """Get list of supported execution environments."""
    return self.config.supported_environments.copy()

iterative_improve_and_execute async

iterative_improve_and_execute(
    user_message: str,
    code_type: str | None = None,
    max_iterations: int = 3,
    **kwargs,
) -> AgentResult

Iteratively improve and execute code until it works.

Parameters:

Name Type Description Default
user_message str

Natural language description

required
code_type str | None

Optional code type specification

None
max_iterations int

Maximum improvement iterations

3
**kwargs

Additional execution parameters

{}

Returns:

Type Description
AgentResult

AgentResult with final successful execution or last attempt

Source code in DeepResearch/src/agents/code_execution_orchestrator.py
async def iterative_improve_and_execute(
    self,
    user_message: str,
    code_type: str | None = None,
    max_iterations: int = 3,
    **kwargs,
) -> AgentResult:
    """Iteratively improve and execute code until it works.

    Args:
        user_message: Natural language description
        code_type: Optional code type specification
        max_iterations: Maximum improvement iterations
        **kwargs: Additional execution parameters

    Returns:
        AgentResult with final successful execution or last attempt
    """
    # Generate initial code
    detected_type, generated_code = await self.generation_agent.generate_code(
        user_message, code_type
    )

    # Create test function that executes code and returns error or None
    async def test_execution(code: str, language: str) -> str | None:
        result = await self.execution_agent.execute_code(code, language)
        return result.get("error") if not result.get("success") else None

    # Iteratively improve the code
    improvement_result = await self.improvement_agent.iterative_improve(
        code=generated_code,
        language=detected_type,
        test_function=test_execution,
        max_iterations=max_iterations,
        context={
            "user_request": user_message,
            "code_type": detected_type,
        },
    )

    # Execute the final code one more time to get the result
    final_result = await test_execution(
        improvement_result["final_code"], detected_type
    )

    # Format the response
    messages = []
    from DeepResearch.src.datatypes.agent_framework_content import TextContent
    from DeepResearch.src.datatypes.agent_framework_types import ChatMessage, Role

    # Code message
    code_content = f"**Final {detected_type.upper()} Code:**\n\n```python\n{improvement_result['final_code']}\n```"
    messages.append(
        ChatMessage(role=Role.ASSISTANT, contents=[TextContent(text=code_content)])
    )

    # Result message
    if improvement_result["success"]:
        result_content = f"**✅ Success after {improvement_result['iterations_used']} iterations!**\n\n"
        result_content += f"**Execution Result:**\n```\n{final_result or 'Code executed successfully'}\n```"
    else:
        result_content = (
            f"**❌ Failed after {max_iterations} improvement attempts**\n\n"
        )
        result_content += (
            f"**Final Error:**\n```\n{final_result or 'Unknown error'}\n```"
        )

    # Add improvement summary
    if improvement_result["improvement_history"]:
        result_content += f"\n\n**Improvement Summary:** {len(improvement_result['improvement_history'])} fixes applied"

    messages.append(
        ChatMessage(
            role=Role.ASSISTANT, contents=[TextContent(text=result_content)]
        )
    )

    # Add detailed improvement history
    if improvement_result["improvement_history"]:
        history_content = "**Improvement History:**\n\n"
        for i, hist in enumerate(improvement_result["improvement_history"], 1):
            history_content += f"**Attempt {i}:**\n"
            history_content += f"- **Error:** {hist['error_message'][:100]}{'...' if len(hist['error_message']) > 100 else ''}\n"
            history_content += f"- **Fix:** {hist['improvement']['explanation'][:150]}{'...' if len(hist['improvement']['explanation']) > 150 else ''}\n\n"

        messages.append(
            ChatMessage(
                role=Role.ASSISTANT, contents=[TextContent(text=history_content)]
            )
        )

    from DeepResearch.src.datatypes.agent_framework_types import AgentRunResponse

    return AgentResult(
        success=improvement_result["success"],
        data={
            "response": AgentRunResponse(messages=messages),
            "improvement_history": improvement_result["improvement_history"],
            "iterations_used": improvement_result["iterations_used"],
            "final_code": improvement_result["final_code"],
            "code_type": detected_type,
        },
        metadata={
            "orchestrator": "code_execution_improvement",
            "improvement_iterations": improvement_result["iterations_used"],
            "success": improvement_result["success"],
        },
        error=None if improvement_result["success"] else final_result,
        execution_time=0.0,  # Would need to track actual timing
        agent_type=AgentType.EXECUTOR,
    )

process_request async

process_request(
    user_message: str,
    code_type: str | None = None,
    use_workflow: bool | None = None,
    **kwargs,
) -> AgentResult

Process a user request for code generation and execution.

Parameters:

Name Type Description Default
user_message str

Natural language description of desired operation

required
code_type str | None

Optional code type specification ("bash", "python", or None for auto)

None
use_workflow bool | None

Whether to use the state machine workflow (overrides config)

None
**kwargs

Additional execution parameters

{}

Returns:

Type Description
AgentResult

AgentResult with execution outcome

Source code in DeepResearch/src/agents/code_execution_orchestrator.py
async def process_request(
    self,
    user_message: str,
    code_type: str | None = None,
    use_workflow: bool | None = None,
    **kwargs,
) -> AgentResult:
    """Process a user request for code generation and execution.

    Args:
        user_message: Natural language description of desired operation
        code_type: Optional code type specification ("bash", "python", or None for auto)
        use_workflow: Whether to use the state machine workflow (overrides config)
        **kwargs: Additional execution parameters

    Returns:
        AgentResult with execution outcome
    """
    start_time = time.time()

    try:
        # Determine whether to use workflow
        use_workflow_mode = (
            use_workflow if use_workflow is not None else self.config.use_workflow
        )

        if use_workflow_mode and self.workflow:
            # Use state machine workflow
            result = await self._execute_workflow(user_message, code_type, **kwargs)
        else:
            # Use direct agent system
            result = await self._execute_direct(user_message, code_type, **kwargs)

        execution_time = time.time() - start_time

        return AgentResult(
            success=result is not None,
            data={
                "response": result,
                "execution_time": execution_time,
                "code_type": code_type,
                "workflow_used": use_workflow_mode,
            }
            if result
            else {},
            metadata={
                "orchestrator": "code_execution",
                "generation_model": self.config.generation_model,
                "execution_config": self.config.dict(),
            },
            error=None,
            execution_time=execution_time,
            agent_type=AgentType.EXECUTOR,
        )

    except Exception as e:
        execution_time = time.time() - start_time
        return AgentResult(
            success=False,
            data={},
            error=f"Orchestration failed: {e!s}",
            execution_time=execution_time,
            agent_type=AgentType.EXECUTOR,
        )

update_config

update_config(**kwargs) -> None

Update orchestrator configuration.

Source code in DeepResearch/src/agents/code_execution_orchestrator.py
def update_config(self, **kwargs) -> None:
    """Update orchestrator configuration."""
    for key, value in kwargs.items():
        if hasattr(self.config, key):
            setattr(self.config, key, value)

    # Reinitialize agents if necessary
    if any(
        key in kwargs
        for key in ["generation_model", "max_retries", "generation_timeout"]
    ):
        self.generation_agent = CodeGenerationAgent(
            model_name=self.config.generation_model,
            max_retries=self.config.max_retries,
            timeout=self.config.generation_timeout,
        )

    if any(
        key in kwargs
        for key in [
            "use_docker",
            "use_jupyter",
            "jupyter_config",
            "max_retries",
            "execution_timeout",
        ]
    ):
        self.execution_agent = CodeExecutionAgent(
            model_name=self.config.generation_model,
            use_docker=self.config.use_docker,
            use_jupyter=self.config.use_jupyter,
            jupyter_config=self.config.jupyter_config,
            max_retries=self.config.max_retries,
            timeout=self.config.execution_timeout,
        )

The CodeExecutionOrchestrator provides high-level coordination for complete code generation, execution, and improvement workflows with automatic error recovery and intelligent retry logic.

Key Methods:

analyze_and_improve_code() - Single-step error analysis and code improvement - Returns analysis results and improved code with detailed explanations - Supports contextual error information and language-specific fixes

iterative_improve_and_execute() - Full iterative improvement workflow with automatic error correction - Generates → Tests → Improves → Retries cycle with configurable limits - Includes comprehensive improvement history and performance tracking - Supports multiple programming languages (Python, Bash, etc.)

process_message_to_command_log() - End-to-end natural language to executable code conversion - Automatic error detection and correction during execution - Returns detailed execution logs and improvement summaries

PRIME Agents

ParserAgent

The ParserAgent analyzes research questions and extracts key scientific intent and requirements for optimal tool selection and workflow planning.

PlannerAgent

The PlannerAgent creates detailed execution plans based on parsed research queries and available tools.

ExecutorAgent

The ExecutorAgent executes planned research workflows and coordinates tool interactions.

Research Agents

SearchAgent

The SearchAgent provides web search and information retrieval capabilities for research tasks.

RAGAgent

The RAGAgent implements Retrieval-Augmented Generation for knowledge-intensive tasks.

EvaluatorAgent

The EvaluatorAgent provides result evaluation and quality assessment capabilities.

Bioinformatics Agents

BioinformaticsAgent

The BioinformaticsAgent specializes in biological data analysis and multi-source data fusion.

DeepSearch Agents

DeepSearchAgent

The DeepSearchAgent provides advanced web research with reflection and iterative search strategies.

Agent Configuration

Agent Dependencies Configuration

from DeepResearch.src.datatypes.agents import AgentDependencies

# Configure agent dependencies
deps = AgentDependencies(
    model_name="anthropic:claude-sonnet-4-0",
    api_keys={
        "anthropic": "your-api-key",
        "openai": "your-openai-key"
    },
    config={
        "temperature": 0.7,
        "max_tokens": 2000
    }
)

Code Execution Configuration

from DeepResearch.src.agents.code_execution_orchestrator import CodeExecutionConfig

# Configure code execution orchestrator
config = CodeExecutionConfig(
    generation_model="anthropic:claude-sonnet-4-0",
    use_docker=True,
    max_retries=3,
    max_improvement_attempts=3,
    enable_improvement=True,
    execution_timeout=60.0
)

Agent Execution Patterns

Basic Agent Execution

# Execute agent directly
result = await agent.execute(
    input_data="Analyze this research question",
    deps=agent_dependencies
)

if result.success:
    print(f"Result: {result.data}")
else:
    print(f"Error: {result.error}")

Multi-Agent Workflow

from DeepResearch.agents import AgentOrchestrator

# Create orchestrator
orchestrator = AgentOrchestrator()

# Add agents to workflow
orchestrator.add_agent("parser", ParserAgent())
orchestrator.add_agent("planner", PlannerAgent())
orchestrator.add_agent("executor", ExecutorAgent())

# Execute workflow
result = await orchestrator.execute_workflow(
    initial_query="Complex research task",
    workflow_sequence=[
        {"agent": "parser", "task": "Parse query"},
        {"agent": "planner", "task": "Create plan"},
        {"agent": "executor", "task": "Execute plan"}
    ]
)

Code Improvement Workflow

from DeepResearch.src.agents.code_execution_orchestrator import CodeExecutionOrchestrator

# Initialize orchestrator
orchestrator = CodeExecutionOrchestrator()

# Execute with automatic error correction
result = await orchestrator.iterative_improve_and_execute(
    user_message="Write a Python function that calculates factorial",
    max_iterations=3
)

print(f"Final successful code: {result.data['final_code']}")
print(f"Improvement attempts: {result.data['iterations_used']}")

Error Handling

Agent Error Types

  • ExecutionError: Agent execution failed
  • DependencyError: Required dependencies not available
  • TimeoutError: Agent execution timed out
  • ValidationError: Input validation failed
  • ModelError: AI model API errors

Error Recovery

# Configure error recovery
agent_config = {
    "max_retries": 3,
    "retry_delay": 1.0,
    "fallback_agents": ["backup_agent"],
    "error_logging": True
}

# Execute with error recovery
result = await agent.execute_with_recovery(
    input_data="Task that might fail",
    deps=deps,
    recovery_config=agent_config
)

Performance Optimization

Agent Pooling

# Create agent pool for high-throughput tasks
agent_pool = AgentPool(
    agent_class=SearchAgent,
    pool_size=10,
    preload_models=True
)

# Execute multiple tasks concurrently
results = await agent_pool.execute_batch([
    "Query 1", "Query 2", "Query 3"
])

Caching and Memoization

# Enable result caching
agent.enable_caching(
    cache_backend="redis",
    ttl_seconds=3600
)

# Execute with caching
result = await agent.execute_cached(
    input_data="Frequently asked question",
    cache_key="faq_1"
)

Testing Agents

Unit Testing Agents

import pytest
from unittest.mock import AsyncMock

def test_agent_execution():
    agent = SearchAgent()
    mock_deps = AgentDependencies()

    # Mock external dependencies
    with patch('agent.external_api_call') as mock_api:
        mock_api.return_value = {"results": "mock data"}

        result = await agent.execute("test query", mock_deps)

        assert result.success
        assert result.data == {"results": "mock data"}

Integration Testing

@pytest.mark.integration
async def test_agent_integration():
    agent = BioinformaticsAgent()

    # Test with real dependencies
    result = await agent.execute(
        "Analyze TP53 gene function",
        deps=real_dependencies
    )

    assert result.success
    assert "gene_function" in result.data

Best Practices

  1. Type Safety: Use proper type annotations for all agent methods
  2. Error Handling: Implement comprehensive error handling and recovery
  3. Configuration: Use configuration files for agent parameters
  4. Testing: Write both unit and integration tests for agents
  5. Documentation: Document agent capabilities and usage patterns
  6. Performance: Monitor and optimize agent execution performance
  7. Security: Validate inputs and handle sensitive data appropriately