Agent Interface
All agents inherit fromBaseAgent and implement these core methods:
Quick Start
Create a simple custom agent:from pathlib import Path
import asyncio
from harbor.agents.base import BaseAgent
from harbor.environments.base import BaseEnvironment
from harbor.models.agent.context import AgentContext
class SimpleAgent(BaseAgent):
@staticmethod
def name() -> str:
return "simple-agent"
def version(self) -> str | None:
return "0.1.0"
async def setup(self, environment: BaseEnvironment) -> None:
# No setup needed for this simple agent
pass
async def run(
self,
instruction: str,
environment: BaseEnvironment,
context: AgentContext,
) -> None:
# Simple agent that creates a hello.txt file
result = await environment.exec(
'echo "Hello, world!" > /app/hello.txt'
)
# Log execution details
self.logger.info(f"Executed command, return code: {result.return_code}")
# Update context with execution details
context.commands_executed = 1
BaseAgent Methods
name() (Required)
Return a unique identifier for your agent:version() (Required)
Return the agent version for reproducibility:setup() (Required)
Install and configure the agent in the environment:run() (Required)
Execute the agent on the task:Using the Environment
TheBaseEnvironment provides methods to interact with the containerized environment:
Executing Commands
File Operations
Populating Context
TheAgentContext tracks execution metrics and results:
AgentContext Fields
commands_executed- Number of commands runexit_code- Final exit coden_input_tokens- LLM input tokens usedn_output_tokens- LLM output tokens usedn_cache_tokens- Cached tokens usedcost_usd- Total cost in USDerror_message- Error description if failed
Installed Agents Pattern
For agents that require installation, extendBaseInstalledAgent:
install-my-agent.sh.j2):
Agent Configuration
Accept configuration through constructor:MCP Server Support
Access MCP servers provided by tasks:Skills Support
Access skills provided by tasks:Trajectory Support (ATIF)
For advanced agents, implement trajectory tracking:Error Handling
Implement robust error handling:Testing Your Agent
Unit Tests
tests/test_my_agent.py
Integration Tests
Real-World Example
Here’s a complete example of a custom agent that uses OpenAI’s API:openai_agent.py
Best Practices
- Handle timeouts gracefully: Agents may be interrupted
- Log extensively: Use
self.loggerfor debugging - Populate context: Track metrics for analysis
- Save artifacts: Download important files to
self.logs_dir - Use async properly: All environment operations are async
- Test thoroughly: Unit test logic, integration test with real tasks
- Version your agent: Update version when behavior changes
- Document configuration: Explain all constructor parameters
Next Steps
Running Evaluations
Run evaluations with your custom agent
Creating Tasks
Build tasks to test your agent’s capabilities
Benchmark Adapters
Evaluate your agent on standard benchmarks