|
| 1 | +# CLAUDE.md - Redis Agent Memory Server Project Context |
| 2 | + |
| 3 | +## Frequently Used Commands |
| 4 | +Get started in a new environment by installing `uv`: |
| 5 | +```bash |
| 6 | +pip install uv |
| 7 | +``` |
| 8 | + |
| 9 | +```bash |
| 10 | +# Development workflow |
| 11 | +uv install # Install dependencies |
| 12 | +uv run ruff check # Run linting |
| 13 | +uv run ruff format # Format code |
| 14 | +uv run pytest # Run tests |
| 15 | +uv run pytest tests/ # Run specific test directory |
| 16 | + |
| 17 | +# Server commands |
| 18 | +uv run agent-memory api # Start REST API server (default port 8000) |
| 19 | +uv run agent-memory mcp # Start MCP server (stdio mode) |
| 20 | +uv run agent-memory mcp --mode sse --port 9000 # Start MCP server (SSE mode) |
| 21 | + |
| 22 | +# Database/Redis operations |
| 23 | +uv run agent-memory rebuild-index # Rebuild Redis search index |
| 24 | +uv run agent-memory migrate-memories # Run memory migrations |
| 25 | + |
| 26 | +# Background task management |
| 27 | +uv run agent-memory task-worker # Start background task worker |
| 28 | +uv run agent-memory schedule-task "agent_memory_server.long_term_memory.compact_long_term_memories" |
| 29 | + |
| 30 | +# Docker development |
| 31 | +docker-compose up # Start full stack (API, MCP, Redis) |
| 32 | +docker-compose up redis # Start only Redis Stack |
| 33 | +docker-compose down # Stop all services |
| 34 | +``` |
| 35 | + |
| 36 | +IMPORTANT: This project uses `pre-commit`. You should run `pre-commit` |
| 37 | +before committing: |
| 38 | +```bash |
| 39 | +uv run pre-commit run --all-files |
| 40 | +``` |
| 41 | + |
| 42 | +## Important Architectural Patterns |
| 43 | + |
| 44 | +### Dual Interface Design (REST + MCP) |
| 45 | +- **REST API**: Traditional HTTP endpoints for web applications (`api.py`) |
| 46 | +- **MCP Server**: Model Context Protocol for AI agent integration (`mcp.py`) |
| 47 | +- Both interfaces share the same core memory management logic |
| 48 | + |
| 49 | +### Memory Architecture |
| 50 | +```python |
| 51 | +# Two-tier memory system |
| 52 | +Working Memory (Session-scoped) → Long-term Memory (Persistent) |
| 53 | + ↓ ↓ |
| 54 | +- Messages - Semantic search |
| 55 | +- Context - Topic modeling |
| 56 | +- Structured memories - Entity recognition |
| 57 | +- Metadata - Deduplication |
| 58 | +``` |
| 59 | + |
| 60 | +### RedisVL Integration |
| 61 | +**CRITICAL**: Always use RedisVL query types instead of direct redis-py client access for searches: |
| 62 | +```python |
| 63 | +# Correct - Use RedisVL queries |
| 64 | +from redisvl.query import VectorQuery, FilterQuery |
| 65 | +query = VectorQuery(vector=embedding, vector_field_name="embedding", return_fields=["text"]) |
| 66 | + |
| 67 | +# Avoid - Direct redis client searches |
| 68 | +# redis.ft().search(...) # Don't do this |
| 69 | +``` |
| 70 | + |
| 71 | +### Async-First Design |
| 72 | +- All core operations are async |
| 73 | +- Background task processing with Docket |
| 74 | +- Async Redis connections throughout |
| 75 | + |
| 76 | +## Critical Rules |
| 77 | + |
| 78 | +### Authentication |
| 79 | +- **PRODUCTION**: Never set `DISABLE_AUTH=true` in production |
| 80 | +- **DEVELOPMENT**: Use `DISABLE_AUTH=true` for local testing only |
| 81 | +- JWT/OAuth2 authentication required for all endpoints except `/health`, `/docs`, `/openapi.json` |
| 82 | + |
| 83 | +### Memory Management |
| 84 | +- Working memory automatically promotes structured memories to long-term storage |
| 85 | +- Conversations are summarized when exceeding window size |
| 86 | +- Use model-aware token limits for context window management |
| 87 | + |
| 88 | +### RedisVL Usage (Required) |
| 89 | +Always use RedisVL query types for any search operations. This is a project requirement. |
| 90 | + |
| 91 | +## Testing Notes |
| 92 | + |
| 93 | +The project uses `pytest` with `testcontainers` for Redis integration testing: |
| 94 | + |
| 95 | +- `uv run pytest` - Run all tests |
| 96 | +- `uv run pytest tests/unit/` - Unit tests only |
| 97 | +- `uv run pytest tests/integration/` - Integration tests (require Redis) |
| 98 | +- `uv run pytest -v` - Verbose output |
| 99 | +- `uv run pytest --cov` - With coverage |
| 100 | + |
| 101 | +## Project Structure |
| 102 | + |
| 103 | +``` |
| 104 | +agent_memory_server/ |
| 105 | +├── main.py # FastAPI application entry point |
| 106 | +├── api.py # REST API endpoints |
| 107 | +├── mcp.py # MCP server implementation |
| 108 | +├── config.py # Configuration management |
| 109 | +├── auth.py # OAuth2/JWT authentication |
| 110 | +├── models.py # Pydantic data models |
| 111 | +├── working_memory.py # Session-scoped memory management |
| 112 | +├── long_term_memory.py # Persistent memory with semantic search |
| 113 | +├── messages.py # Message handling and formatting |
| 114 | +├── summarization.py # Conversation summarization |
| 115 | +├── extraction.py # Topic and entity extraction |
| 116 | +├── filters.py # Search filtering logic |
| 117 | +├── llms.py # LLM provider integrations |
| 118 | +├── migrations.py # Database schema migrations |
| 119 | +├── docket_tasks.py # Background task definitions |
| 120 | +├── cli.py # Command-line interface |
| 121 | +├── dependencies.py # FastAPI dependency injection |
| 122 | +├── healthcheck.py # Health check endpoint |
| 123 | +├── logging.py # Structured logging setup |
| 124 | +├── client/ # Client libraries |
| 125 | +└── utils/ # Utility modules |
| 126 | + ├── redis.py # Redis connection and setup |
| 127 | + ├── keys.py # Redis key management |
| 128 | + └── api_keys.py # API key utilities |
| 129 | +``` |
| 130 | + |
| 131 | +## Core Components |
| 132 | + |
| 133 | +### 1. Memory Management |
| 134 | +- **Working Memory**: Session-scoped storage with automatic summarization |
| 135 | +- **Long-term Memory**: Persistent storage with semantic search capabilities |
| 136 | +- **Memory Promotion**: Automatic migration from working to long-term memory |
| 137 | +- **Deduplication**: Prevents duplicate memories using content hashing |
| 138 | + |
| 139 | +### 2. Search and Retrieval |
| 140 | +- **Semantic Search**: Vector-based similarity search using embeddings |
| 141 | +- **Filtering System**: Advanced filtering by session, namespace, topics, entities, timestamps |
| 142 | +- **Hybrid Search**: Combines semantic similarity with metadata filtering |
| 143 | +- **RedisVL Integration**: All search operations use RedisVL query builders |
| 144 | + |
| 145 | +### 3. AI Integration |
| 146 | +- **Topic Modeling**: Automatic topic extraction using BERTopic or LLM |
| 147 | +- **Entity Recognition**: BERT-based named entity recognition |
| 148 | +- **Summarization**: Conversation summarization when context window exceeded |
| 149 | +- **Multi-LLM Support**: OpenAI, Anthropic, and other providers |
| 150 | + |
| 151 | +### 4. Authentication & Security |
| 152 | +- **OAuth2/JWT**: Industry-standard authentication with JWKS validation |
| 153 | +- **Multi-Provider**: Auth0, AWS Cognito, Okta, Azure AD support |
| 154 | +- **Role-Based Access**: Fine-grained permissions using JWT claims |
| 155 | +- **Development Mode**: `DISABLE_AUTH` for local development |
| 156 | + |
| 157 | +### 5. Background Processing |
| 158 | +- **Docket Tasks**: Redis-based task queue for background operations |
| 159 | +- **Memory Indexing**: Asynchronous embedding generation and indexing |
| 160 | +- **Compaction**: Periodic cleanup and optimization of stored memories |
| 161 | + |
| 162 | +## Environment Configuration |
| 163 | + |
| 164 | +Key environment variables: |
| 165 | +```bash |
| 166 | +# Redis |
| 167 | +REDIS_URL=redis://localhost:6379 |
| 168 | + |
| 169 | +# Authentication (Production) |
| 170 | +OAUTH2_ISSUER_URL=https://your-auth-provider.com |
| 171 | +OAUTH2_AUDIENCE=your-api-audience |
| 172 | +DISABLE_AUTH=false # Never true in production |
| 173 | + |
| 174 | +# Development |
| 175 | +DISABLE_AUTH=true # Local development only |
| 176 | +LOG_LEVEL=DEBUG |
| 177 | + |
| 178 | +# AI Services |
| 179 | +OPENAI_API_KEY=your-key |
| 180 | +ANTHROPIC_API_KEY=your-key |
| 181 | +GENERATION_MODEL=gpt-4o-mini |
| 182 | +EMBEDDING_MODEL=text-embedding-3-small |
| 183 | + |
| 184 | +# Memory Configuration |
| 185 | +LONG_TERM_MEMORY=true |
| 186 | +WINDOW_SIZE=20 |
| 187 | +ENABLE_TOPIC_EXTRACTION=true |
| 188 | +ENABLE_NER=true |
| 189 | +``` |
| 190 | + |
| 191 | +## API Interfaces |
| 192 | + |
| 193 | +### REST API (Port 8000) |
| 194 | +- Session management (`/sessions/`) |
| 195 | +- Working memory operations (`/sessions/{id}/memory`) |
| 196 | +- Long-term memory search (`/memories/search`) |
| 197 | +- Memory hydration (`/memories/hydrate`) |
| 198 | + |
| 199 | +### MCP Server (Port 9000) |
| 200 | +- `create_long_term_memories` - Store persistent memories |
| 201 | +- `search_long_term_memory` - Semantic search with filtering |
| 202 | +- `memory_prompt` - Hydrate queries with relevant context |
| 203 | +- `set_working_memory` - Manage session memory |
| 204 | + |
| 205 | +## Development Workflow |
| 206 | + |
| 207 | +0. **Install uv**: `pip install uv` to get started with uv |
| 208 | +1. **Setup**: `uv install` to install dependencies |
| 209 | +2. **Redis**: Start Redis Stack via `docker-compose up redis` |
| 210 | +3. **Development**: Use `DISABLE_AUTH=true` for local testing |
| 211 | +4. **Testing**: Run `uv run pytest` before committing |
| 212 | +5. **Linting**: Pre-commit hooks handle code formatting |
| 213 | +6. **Background Tasks**: Start worker with `uv run agent-memory task-worker` |
| 214 | + |
| 215 | +## Documentation |
| 216 | +- API docs available at `/docs` when server is running |
| 217 | +- OpenAPI spec at `/openapi.json` |
| 218 | +- Authentication examples in README.md |
| 219 | +- System architecture diagram in `diagram.png` |
0 commit comments