|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +PraisonAI Agents is a hierarchical AI agent framework for completing complex tasks with self-reflection capabilities. It supports multi-agent collaboration, tool integration, and various execution patterns (sequential, hierarchical, parallel). |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +### Installation and Setup |
| 12 | +```bash |
| 13 | +# Install core package |
| 14 | +pip install -e . |
| 15 | + |
| 16 | +# Install with specific features |
| 17 | +pip install -e .[all] # All features |
| 18 | +pip install -e .[memory] # Memory capabilities |
| 19 | +pip install -e .[knowledge] # Document processing |
| 20 | +pip install -e .[mcp] # MCP server support |
| 21 | +pip install -e .[llm] # Extended LLM support |
| 22 | +pip install -e .[api] # API server capabilities |
| 23 | +``` |
| 24 | + |
| 25 | +### Testing |
| 26 | +```bash |
| 27 | +# Run individual test examples (no formal test runner configured) |
| 28 | +python tests/basic-agents.py |
| 29 | +python tests/async_example.py |
| 30 | +python tests/knowledge-agents.py |
| 31 | + |
| 32 | +# Test specific features |
| 33 | +python tests/mcp-agents.py # MCP integration |
| 34 | +python tests/memory_example.py # Memory functionality |
| 35 | +python tests/tools_example.py # Tool system |
| 36 | +``` |
| 37 | + |
| 38 | +### Running Examples |
| 39 | +```bash |
| 40 | +# Basic agent usage |
| 41 | +python tests/single-agent.py |
| 42 | + |
| 43 | +# Multi-agent workflows |
| 44 | +python tests/multi-agents-api.py |
| 45 | + |
| 46 | +# Async operations |
| 47 | +python tests/async_example_full.py |
| 48 | + |
| 49 | +# MCP server examples |
| 50 | +python tests/mcp-sse-direct-server.py # Start MCP server |
| 51 | +python tests/mcp-sse-direct-client.py # Connect to server |
| 52 | +``` |
| 53 | + |
| 54 | +## Core Architecture |
| 55 | + |
| 56 | +### Agent System (`praisonaiagents/agent/`) |
| 57 | +- **Agent**: Core agent class with LLM integration, tool calling, and self-reflection |
| 58 | +- **ImageAgent**: Specialized multimodal agent for image processing |
| 59 | +- Self-reflection with configurable min/max iterations (default: 1-3) |
| 60 | +- Delegation support for hierarchical agent structures |
| 61 | + |
| 62 | +### Multi-Agent Orchestration (`praisonaiagents/agents/`) |
| 63 | +- **PraisonAIAgents**: Main orchestrator for managing multiple agents and tasks |
| 64 | +- **AutoAgents**: Automatic agent creation and management |
| 65 | +- Process types: `sequential`, `hierarchical`, `parallel` |
| 66 | +- Context passing between agents and task dependency management |
| 67 | + |
| 68 | +### Task System (`praisonaiagents/task/`) |
| 69 | +- **Task**: Core task definition with context, callbacks, and output specifications |
| 70 | +- Supports file output, JSON/Pydantic structured output, async execution |
| 71 | +- Conditional logic with `condition` parameter for task flow control |
| 72 | +- Context passing via `context` parameter for task dependencies |
| 73 | +- **Guardrails**: Built-in validation and safety mechanisms for task outputs |
| 74 | + - Function-based guardrails for custom validation logic |
| 75 | + - LLM-based guardrails using natural language descriptions |
| 76 | + - Automatic retry with configurable `max_retries` parameter |
| 77 | + - Compatible with CrewAI guardrail patterns |
| 78 | + |
| 79 | +### LLM Integration (`praisonaiagents/llm/`) |
| 80 | +- Unified wrapper for multiple LLM providers via LiteLLM |
| 81 | +- Supports OpenAI, Anthropic, Gemini, DeepSeek, local models (Ollama) |
| 82 | +- Context length management and tool calling capabilities |
| 83 | +- Set via `llm` parameter on agents or global `OPENAI_API_KEY`/`ANTHROPIC_API_KEY` |
| 84 | + |
| 85 | +### Tool System (`praisonaiagents/tools/`) |
| 86 | +Two implementation patterns: |
| 87 | +1. **Function-based**: Simple tools using `@tool` decorator |
| 88 | +2. **Class-based**: Complex tools inheriting from `BaseTool` |
| 89 | + |
| 90 | +Built-in tools include: DuckDuckGo search, file operations, calculator, Wikipedia, arXiv, data analysis tools, shell execution. |
| 91 | + |
| 92 | +### Memory & Knowledge Systems |
| 93 | +- **Memory** (`praisonaiagents/memory/`): Multi-layered memory with RAG support |
| 94 | + - Types: short-term, long-term, entity, user memory |
| 95 | + - Providers: ChromaDB, Mem0, custom implementations |
| 96 | +- **Knowledge** (`praisonaiagents/knowledge/`): Document processing with chunking |
| 97 | + - Chunking strategies via `chonkie` library |
| 98 | + - Embedding and retrieval capabilities |
| 99 | + |
| 100 | +### MCP (Model Context Protocol) Integration |
| 101 | +- **MCP Server**: Server-side tool protocol for distributed execution |
| 102 | +- **SSE Support**: Server-sent events for real-time communication |
| 103 | +- Tool discovery and dynamic registration |
| 104 | + |
| 105 | +## Development Patterns |
| 106 | + |
| 107 | +### Agent Creation |
| 108 | +```python |
| 109 | +agent = Agent( |
| 110 | + name="Agent Name", |
| 111 | + role="Agent Role", |
| 112 | + goal="Agent Goal", |
| 113 | + backstory="Agent Background", |
| 114 | + llm="gpt-4o-mini", # or other LLM |
| 115 | + self_reflect=True, # Enable self-reflection |
| 116 | + min_reflect=1, # Minimum reflection iterations |
| 117 | + max_reflect=3, # Maximum reflection iterations |
| 118 | + tools=[tool1, tool2] # Optional tools |
| 119 | +) |
| 120 | +``` |
| 121 | + |
| 122 | +### Task Definition |
| 123 | +```python |
| 124 | +task = Task( |
| 125 | + name="task_name", |
| 126 | + description="Task description", |
| 127 | + expected_output="Expected output format", |
| 128 | + agent=agent, |
| 129 | + context=[previous_task], # Task dependencies |
| 130 | + output_pydantic=ResponseModel, # Structured output |
| 131 | + condition="condition_function" # Conditional execution |
| 132 | +) |
| 133 | +``` |
| 134 | + |
| 135 | +### Guardrails Usage |
| 136 | +```python |
| 137 | +from typing import Tuple, Any |
| 138 | + |
| 139 | +# Function-based guardrail |
| 140 | +def validate_output(task_output: TaskOutput) -> Tuple[bool, Any]: |
| 141 | + """Custom validation function.""" |
| 142 | + if "error" in task_output.raw.lower(): |
| 143 | + return False, "Output contains errors" |
| 144 | + if len(task_output.raw) < 10: |
| 145 | + return False, "Output is too short" |
| 146 | + return True, task_output |
| 147 | + |
| 148 | +task = Task( |
| 149 | + description="Write a professional email", |
| 150 | + expected_output="A well-formatted email", |
| 151 | + agent=agent, |
| 152 | + guardrail=validate_output, # Function-based guardrail |
| 153 | + max_retries=3 # Retry up to 3 times if guardrail fails |
| 154 | +) |
| 155 | + |
| 156 | +# LLM-based guardrail |
| 157 | +task = Task( |
| 158 | + description="Generate marketing copy", |
| 159 | + expected_output="Professional marketing content", |
| 160 | + agent=agent, |
| 161 | + guardrail="Ensure the content is professional, engaging, and free of errors", # String description |
| 162 | + max_retries=2 |
| 163 | +) |
| 164 | +``` |
| 165 | + |
| 166 | +### Multi-Agent Workflow |
| 167 | +```python |
| 168 | +workflow = PraisonAIAgents( |
| 169 | + agents=[agent1, agent2], |
| 170 | + tasks=[task1, task2], |
| 171 | + process="sequential", # or "hierarchical", "parallel" |
| 172 | + verbose=True, |
| 173 | + manager_agent=manager_agent # For hierarchical process |
| 174 | +) |
| 175 | +result = workflow.start() |
| 176 | +``` |
| 177 | + |
| 178 | +### Async Support |
| 179 | +All major components support async execution: |
| 180 | +```python |
| 181 | +result = await workflow.astart() |
| 182 | +result = await agent.aexecute(task) |
| 183 | +``` |
| 184 | + |
| 185 | +## Key Dependencies |
| 186 | + |
| 187 | +- **Core**: `pydantic`, `rich`, `openai`, `mcp` |
| 188 | +- **Memory**: `chromadb`, `mem0ai` |
| 189 | +- **Knowledge**: `markitdown`, `chonkie` |
| 190 | +- **LLM**: `litellm` for unified provider access |
| 191 | +- **API**: `fastapi`, `uvicorn` for server capabilities |
| 192 | + |
| 193 | +## Error Handling |
| 194 | + |
| 195 | +- Global error logging via `error_logs` list |
| 196 | +- Callback system for real-time error reporting |
| 197 | +- Context length exception handling with automatic retry |
| 198 | +- Graceful degradation for optional dependencies |
| 199 | + |
| 200 | +## Testing Strategy |
| 201 | + |
| 202 | +The project uses example-driven testing with 100+ test files in `tests/` directory. Each test file demonstrates specific usage patterns and serves as both test and documentation. Run individual examples to test functionality rather than using a formal test runner. |
| 203 | + |
| 204 | +Use conda activate praisonai-agents to activate the environment. |
0 commit comments