agents.baseΒΆ
Base Agent Module - Foundation for all Haive AI Agents.
This module provides the core abstractions and base classes for building sophisticated AI agents in the Haive framework. It includes the fundamental Agent class along with essential mixins, hooks, and utilities that enable dynamic, self-modifying, and highly capable agent systems.
The base agent system is designed around several key principles: - Dynamic Adaptation: Agents can modify their behavior at runtime - Composable Architecture: Mix and match capabilities through mixins - Hook-Based Extensions: Comprehensive lifecycle hooks for customization - State Management: Sophisticated state tracking and persistence - Graph-Based Workflows: LangGraph integration for complex agent flows
- Core Architecture:
The base agent system consists of several key components working together:
- Base Agent Class:
Foundation class that all Haive agents inherit from
Provides core functionality like execution, state management, recompilation
Integrates with LangGraph for workflow orchestration
Supports dynamic tool addition and configuration changes
- Mixin System:
ExecutionMixin: Core execution capabilities and lifecycle management
StateMixin: State tracking, persistence, and recovery
PersistenceMixin: Database integration and checkpoint management
SerializationMixin: Agent serialization and deserialization
PrePostAgentMixin: Pre/post processing with agent transformation
- Hook System:
Comprehensive lifecycle hooks for every stage of agent execution
Pre/post processing hooks with message transformation
Reflection and grading hooks for quality control
Custom hook registration for specialized behaviors
- Workflow Integration:
Seamless LangGraph integration for complex workflows
Dynamic graph compilation and recompilation
State-aware graph construction
Multi-agent coordination support
- Key Components:
- Core Classes:
Agent: The foundational agent class with all base capabilities
Workflow: Base workflow class for pure orchestration (no LLM)
HookContext: Context object passed to hook functions
AgentState: Base state schema for agent data
- Capability Mixins:
ExecutionMixin: Execution lifecycle and error handling
StateMixin: State management and persistence
PersistenceMixin: Database and checkpoint integration
SerializationMixin: Agent serialization and recovery
PrePostAgentMixin: Pre/post processing with agent chaining
- Hook System:
HookEvent: Enumeration of all available hook points
HookFunction: Type definition for hook callbacks
HookContext: Execution context and metadata for hooks
- Type Definitions:
AgentInput: Standardized input format for agent execution
AgentOutput: Standardized output format with metadata
AgentState: Base state schema with common fields
- Agent Capabilities:
- Dynamic Behavior Modification:
Runtime tool addition and removal
Configuration changes that trigger automatic recompilation
Behavior adaptation based on execution feedback
Self-modification capabilities through mixins
- Comprehensive Hook System:
PRE_RUN / POST_RUN: Execution lifecycle hooks
PRE_PROCESS / POST_PROCESS: Message processing hooks
PRE_REFLECTION / POST_REFLECTION: Quality control hooks
CUSTOM: User-defined hook points for specialized behaviors
- State Management:
Automatic state persistence and recovery
Checkpoint creation and restoration
State migration and versioning
Cross-agent state sharing
- Multi-Agent Coordination:
Agent-as-tool pattern for composition
Retriever tool creation from any agent
Shared state management across agent teams
Dynamic agent creation and management
Examples
Basic agent creation and execution:
from haive.agents.base import Agent
from haive.core.engine.aug_llm import AugLLMConfig
class CustomAgent(Agent):
def setup_agent(self):
"""Optional setup method for custom initialization."""
self.custom_data = {"initialized": True}
def build_graph(self) -> BaseGraph:
"""Build the agent's workflow graph."""
from haive.core.graph.state_graph.base_graph2 import BaseGraph
graph = BaseGraph()
# Add nodes and edges for agent workflow
graph.add_node("process", self.process_node)
graph.set_entry_point("process")
graph.set_finish_point("process")
return graph
def process_node(self, state):
"""Example processing node."""
return {"result": "Processed input"}
# Create and configure agent
agent = CustomAgent(
name="custom_agent",
engine=AugLLMConfig(temperature=0.7),
)
# Execute agent
result = await agent.arun({"input": "Hello, agent!"})
print(f"Result: {result}")
Agent with hooks for monitoring and enhancement:
from haive.agents.base import Agent, HookEvent
agent = Agent(name="monitored_agent", engine=config)
# Add execution monitoring hooks
@agent.add_hook(HookEvent.PRE_RUN)
def log_execution_start(context):
print(f"Starting execution for {context.agent_name}")
context.start_time = time.time()
@agent.add_hook(HookEvent.POST_RUN)
def log_execution_end(context):
duration = time.time() - context.start_time
print(f"Execution completed in {duration:.2f}s")
# Add reflection hook for quality control
@agent.add_hook(HookEvent.POST_REFLECTION)
def quality_check(context):
if context.reflection_score < 0.7:
print("Quality check failed, requesting improvement")
context.request_improvement = True
# Execute with monitoring
result = await agent.arun("Complex task requiring quality control")
Pre/post processing with agent transformation:
from haive.agents.base import Agent, PrePostAgentMixin
from haive.agents.simple import SimpleAgent
# Create main agent with processing capabilities
class ProcessingAgent(Agent, PrePostAgentMixin):
pass
main_agent = ProcessingAgent(name="main", engine=main_config)
# Add pre-processing agent
prep_agent = SimpleAgent(name="preprocessor", engine=prep_config)
main_agent.pre_agent = prep_agent
main_agent.use_pre_transform = True
# Add post-processing agent
review_agent = SimpleAgent(name="reviewer", engine=review_config)
main_agent.post_agent = review_agent
main_agent.use_post_transform = True
main_agent.post_transform_type = "reflection"
# Execute with automatic pre/post processing
result = await main_agent.arun("Complex input requiring processing")
Dynamic tool management and recompilation:
from haive.agents.simple import SimpleAgent
from langchain_core.tools import tool
@tool
def calculator(expression: str) -> str:
"""Calculate mathematical expressions."""
return str(eval(expression))
@tool
def web_search(query: str) -> str:
"""Search the web for information."""
return f"Search results for: {query}"
# Create agent with initial tools
agent = SimpleAgent(
name="dynamic_agent",
engine=AugLLMConfig(tools=[calculator])
)
# Execute with initial tools
result1 = await agent.arun("Calculate 15 * 23")
# Add new tool (triggers automatic recompilation)
agent.engine.add_tool(web_search)
# Execute with expanded capabilities
result2 = await agent.arun("Calculate 15 * 23 and search for math tutorials")
Agent-as-tool pattern for composition:
from haive.agents.simple import SimpleAgent
from haive.agents.react import ReactAgent
# Create specialized agents
research_agent = SimpleAgent(name="researcher", engine=research_config)
analysis_agent = SimpleAgent(name="analyst", engine=analysis_config)
# Convert to tools
research_tool = research_agent.as_tool(
name="research_assistant",
description="Research topics and gather information"
)
analysis_tool = analysis_agent.as_tool(
name="data_analyst",
description="Analyze data and create insights"
)
# Use in coordinator agent
coordinator = ReactAgent(
name="coordinator",
engine=AugLLMConfig(tools=[research_tool, analysis_tool])
)
# Coordinator can now use other agents as tools
result = await coordinator.arun(
"Research AI trends and analyze the market data"
)
- Integration Patterns:
- Standalone Usage:
Single agents handling specific tasks
Direct integration with existing systems
Simple request-response patterns
- Multi-Agent Systems:
Coordinated teams of specialized agents
Shared state and memory systems
Dynamic agent creation and management
- Workflow Integration:
LangGraph pipeline components
State-aware workflow steps
Complex branching and conditional logic
- External System Integration:
API endpoint agents
Database integration agents
File processing and analysis agents
- Advanced Features:
- Dynamic Recompilation:
Automatic graph rebuilding when configuration changes
Tool addition/removal without restart
State migration during recompilation
- Hook-Based Extensions:
Pre/post processing at every execution stage
Quality control and reflection hooks
Custom monitoring and logging
- State Management:
Automatic persistence and recovery
Cross-agent state sharing
Version control and migration
- Agent Composition:
Agent-as-tool pattern for building complex systems
Hierarchical agent structures
Dynamic team formation and coordination
- Performance Characteristics:
- Execution Speed:
Base agent overhead: <5ms per execution
Hook processing: <1ms per hook
State persistence: <10ms for typical states
Recompilation: 50-200ms depending on graph complexity
- Memory Usage:
Base agent: ~1MB memory footprint
State storage: Proportional to state size
Graph compilation: ~5-10MB during compilation
Hook storage: Minimal overhead per hook
- Scalability:
Concurrent execution: 100+ agents per process
State management: Millions of state records supported
Hook processing: Minimal overhead for large numbers of hooks
- Best Practices:
- Agent Design:
Keep agents focused on specific capabilities
Use composition over inheritance for complex behaviors
Implement proper error handling in custom nodes
- Hook Usage:
Use hooks for cross-cutting concerns (logging, monitoring)
Keep hook functions lightweight and fast
Handle exceptions in hooks to avoid breaking execution
- State Management:
Design state schemas for forward compatibility
Use appropriate persistence strategies for different use cases
Implement proper state validation and migration
- Performance:
Cache compiled graphs when possible
Use async patterns for I/O operations
Monitor memory usage in long-running agents
See also
haive.agents.base.agent
: Main Agent implementation with full documentation
haive.agents.base.hooks
: Comprehensive hook system for agent lifecycle
haive.agents.base.mixins
: Core capability mixins for agent enhancement
haive.agents.base.pre_post_agent_mixin
: Pre/post processing with agent chaining
haive.agents.simple
: SimpleAgent implementation example
haive.agents.react
: ReactAgent implementation example
haive.agents.multi
: Multi-agent coordination examples
SubmodulesΒΆ
- agents.base.agent
- agents.base.agent_structured_output_mixin
- agents.base.agent_with_token_tracking
- agents.base.compiled_agent
- agents.base.debug_utils
- agents.base.enhanced_init
- agents.base.hooks
- agents.base.mixins
- agents.base.pre_post_agent_mixin
- agents.base.serialization_mixin
- agents.base.smart_output_parsing
- agents.base.structured_output_handler
- agents.base.typed_agent
- agents.base.types
- agents.base.universal_agent
- agents.base.workflow