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 retrievalRAG
: Retrieval-augmented generationBIOINFORMATICS
: Biological data analysisEXECUTOR
: Tool execution and workflow managementEVALUATOR
: 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
Attributes¶
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
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
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
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
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
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,
},
)
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,
)
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
execute_code_only async
¶
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
generate_code_only async
¶
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
get_config ¶
get_supported_environments ¶
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
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
|
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
update_config ¶
Update orchestrator configuration.
Source code in DeepResearch/src/agents/code_execution_orchestrator.py
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¶
- Type Safety: Use proper type annotations for all agent methods
- Error Handling: Implement comprehensive error handling and recovery
- Configuration: Use configuration files for agent parameters
- Testing: Write both unit and integration tests for agents
- Documentation: Document agent capabilities and usage patterns
- Performance: Monitor and optimize agent execution performance
- Security: Validate inputs and handle sensitive data appropriately
Related Documentation¶
- Tool Registry - Tool management and execution
- Workflow Documentation - State machine workflows
- Configuration Guide - Agent configuration
- Testing Guide - Agent testing patterns