Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
309 changes: 247 additions & 62 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -1,97 +1,282 @@
# AI Base Template - Development Guide
# AI Base Template - Production-First Development Guide

A Python template for ML/AI projects with FastAPI, designed for rapid prototyping and clean architecture.
A production-ready Python template for AI/ML systems, designed for reliability, observability, and cost management from day one.

## Project Philosophy

This template embodies **production-first AI engineering**, where we optimize for reliability over research metrics:

- **90% Infrastructure, 10% AI Logic** - Most code is defensive engineering, not model development
- **Engineering Discipline** - Comprehensive testing, monitoring, and error handling
- **Cost Management** - Real-time budget tracking and resource controls
- **Observable Systems** - AI-specific metrics and monitoring patterns

## Project Structure

```
ai-base-template/
├── ai_base_template/ # Main application code
├── ai_base_template/ # Production AI service code
│ ├── __init__.py
│ └── main.py # FastAPI entry point
├── tests/ # Test suite
│ └── test_main.py
├── research/ # Notebooks and experiments
│ └── EDA.ipynb # Exploratory data analysis
├── testing/ # API testing utilities
├── Makefile # Development automation
└── pyproject.toml # Project config & dependencies
│ ├── main.py # AI service with defensive patterns
│ ├── config.py # Environment-driven configuration
│ └── monitoring.py # AI-specific observability
├── tests/ # Defensive testing strategy
│ ├── test_main.py # Basic functionality tests
│ └── test_ai_service.py # AI-specific defensive tests
├── research/ # Experimental AI development
│ └── EDA.ipynb # Exploratory data analysis
├── ARCHITECTURE.md # Production system design docs
├── Makefile # Development automation
└── pyproject.toml # Project config & dependencies
```

## Quick Start

### Setup
### Environment Setup
```bash
make environment-create # Creates Python 3.12 env with uv
make environment-sync # Updates dependencies
make init # Complete development environment setup
make sync # Update dependencies
```

### Development Commands
```bash
make format # Auto-format with Ruff
make lint # Lint and auto-fix issues
make type-check # Type check with MyPy
make validate-branch # Run all checks before PR
# Code quality (production-ready standards)
make format # Auto-format with Ruff
make lint # Lint and auto-fix issues
make type-check # Static type validation
make validate-branch # Full pre-commit validation

# Testing (AI-focused test strategy)
make test # Standard test suite (excludes integration)
make test-unit # Fast, isolated component tests
make test-functional # AI workflow tests
make test-integration # Service-level integration tests
make test-all # Complete suite including cost/load tests

# Environment management
make clean-project # Clean Python caches
make clean-env # Remove virtual environment
```

## Production-First Development Workflow

### 1. Configuration-Driven Development
All production concerns are configured, not hardcoded:

```python
# ai_base_template/config.py
class AIServiceConfig(BaseSettings):
# Cost management
monthly_budget_limit: float = 10000.0
cost_alert_threshold: float = 100.0

# Reliability
model_timeout: float = 5.0
enable_fallback: bool = True

# Observability
log_level: str = "INFO"
enable_tracing: bool = True
```

### 2. Defensive Testing Strategy
Test for AI-specific failure modes:

```bash
# Run defensive AI tests
make test-unit # Input validation, cost controls
make test-integration # Service-level AI workflows
make test-all # Include load and cost validation
```

Test categories:
- **Unit tests**: `@pytest.mark.unit` - Fast, isolated AI component tests
- **Functional tests**: `@pytest.mark.functional` - Feature workflow tests
- **Integration tests**: `@pytest.mark.integration` - Service-level tests with dependencies
- **Performance tests**: `@pytest.mark.performance` - Cost and latency validation

### 3. Cost-Aware Development
Every AI operation is cost-tracked:

```python
# Cost tracking in all AI operations
with cost_tracker.track_cost(estimated_cost=0.01):
result = await model.predict(data)

# Monitor budget status
budget_status = cost_tracker.get_budget_status()
```

### Testing
### 4. Comprehensive Validation
Before any commit:

```bash
make test-unit # Run unit tests
make test-functional # Run functional tests
make test # Run standard tests with coverage
make test-all # Run all tests with coverage
make validate-branch # Runs: lint → type-check → test
```

## Development Workflow
This ensures:
- ✅ Code formatting and linting compliance
- ✅ Static type checking passes
- ✅ All defensive tests pass
- ✅ Cost controls are validated
- ✅ Performance requirements met

1. **Write code** following Python conventions:
- Classes: `PascalCase`
- Functions/variables: `snake_case`
- Constants: `UPPER_SNAKE_CASE`
- Max line length: 120 characters
## Key Technologies & Production Patterns

2. **Validate before committing**:
### Core Infrastructure
- **FastAPI**: Production-grade async web framework
- **Pydantic**: Runtime data validation and type safety
- **loguru**: Structured logging for observability
- **uv**: Fast, reliable Python package management

### AI-Specific Engineering
- **Cost Tracking**: Real-time budget monitoring and alerts
- **Circuit Breakers**: Prevent cascading AI model failures
- **Graceful Degradation**: Fallback strategies for AI failures
- **Input Sanitization**: Prevent adversarial input attacks
- **Timeout Management**: Prevent hanging AI operations

### Monitoring & Observability
- **AI Metrics**: Confidence distributions, fallback rates
- **Cost Metrics**: Per-request costs, budget utilization
- **Performance Metrics**: Latency percentiles, throughput
- **Error Categorization**: Input validation vs. model failures

## Production Deployment Patterns

### Environment Configuration
```bash
# .env.production
MODEL_VERSION=v2.1.0
MODEL_TIMEOUT=3.0
CONFIDENCE_THRESHOLD=0.90
MAX_REQUESTS_PER_USER=500
COST_ALERT_THRESHOLD=1000.0
ENABLE_FALLBACK=true
```

### Health Checks
```python
# Built-in health check endpoint
def get_service_health() -> dict:
return {
"status": "healthy",
"model_version": config.model_version,
"cost_summary": cost_tracker.get_budget_status(),
"performance_summary": metrics.get_performance_summary()
}
```

## Best Practices for Production AI

### Code Quality Standards
- **Type hints on all functions** - Prevent runtime AI failures
- **Comprehensive error handling** - AI systems fail uniquely
- **Input validation** - Sanitize adversarial inputs
- **Cost awareness** - Track and limit expensive operations
- **Fallback strategies** - Graceful degradation for AI failures

### Testing Standards
- **Test coverage > 80%** - Include AI-specific edge cases
- **Defensive testing** - Validate against malicious inputs
- **Cost validation** - Ensure budget controls work
- **Performance testing** - Validate latency requirements
- **Failure scenario testing** - Test circuit breakers and fallbacks

### Monitoring Standards
- **Real-time cost tracking** - Prevent budget overruns
- **Confidence monitoring** - Detect model drift early
- **Latency monitoring** - Maintain SLA compliance
- **Error categorization** - Distinguish AI vs. infrastructure failures
- **Capacity planning** - Monitor resource utilization trends

## Common Production AI Challenges

### 1. Cost Control
```python
# Rate limiting to prevent cost spirals
@rate_limit(max_requests_per_user=1000)
async def predict(request):
with cost_tracker.track_cost():
return await ai_model.predict(request)
```

### 2. Input Validation
```python
# Sanitize adversarial inputs
def validate_ai_input(data: str) -> str:
if len(data) > MAX_INPUT_LENGTH:
raise ValueError("Input too large")
return sanitize_adversarial_patterns(data)
```

### 3. Timeout Management
```python
# Prevent hanging AI operations
result = await asyncio.wait_for(
ai_model.predict(data),
timeout=config.model_timeout
)
```

### 4. Graceful Degradation
```python
# Fallback strategies for AI failures
try:
return await primary_ai_model.predict(data)
except Exception:
return await fallback_model.predict(data)
```

## Getting Started with Production AI

1. **Clone and initialize**:
```bash
make validate-branch # Runs linting and tests
git clone <repo> my-ai-service
cd my-ai-service
make init
```

3. **Test thoroughly**:
- Unit tests: `@pytest.mark.unit`
- Functional tests: `@pytest.mark.functional`
- Integration tests: `@pytest.mark.integration`
2. **Understand the architecture**:
```bash
# Read the production patterns
cat ARCHITECTURE.md

# Examine the defensive code
cat ai_base_template/main.py
```

## Key Technologies
3. **Run defensive tests**:
```bash
# Validate the foundation
make validate-branch

# Run AI-specific tests
make test-all
```

- **FastAPI**: Modern Python web framework
- **Pydantic**: Data validation using Python type annotations
- **MyPy**: Static type checking
- **Ruff**: Fast Python linter and formatter
- **pytest**: Testing framework
- **uv**: Fast Python package manager
4. **Implement your AI logic**:
- Replace the mock `_make_prediction()` method with your model
- Keep all the defensive infrastructure intact
- Add AI-specific configuration in `config.py`
- Extend monitoring in `monitoring.py`

## ML/Data Science Stack
5. **Deploy with confidence**:
```bash
# Final validation
make validate-branch

# Deploy knowing you have production safeguards
```

- **scikit-learn**: Machine learning library
- **XGBoost/LightGBM**: Gradient boosting frameworks
- **PyTorch**: Deep learning framework
- **pandas/numpy**: Data manipulation
- **SHAP**: Model interpretability
## Remember: Production AI is Infrastructure Engineering

## Best Practices
> "The best AI is the AI that works."

- Type hints on all functions
- Pydantic models for data validation
- Structured logging with loguru
- Environment-based configuration
- No hardcoded secrets
- Test coverage > 80%
This template prioritizes **reliability over research metrics**. Most of your code will be infrastructure—cost controls, error handling, monitoring, and fallbacks—not AI/ML logic.

## Getting Started
That's exactly how production AI systems should be built.

1. Clone the template
2. Run `make environment-create`
3. Start coding in `ai_base_template/`
4. Add tests in `tests/`
5. Use `make validate-branch` before commits
---

This template provides a solid foundation for ML/AI projects with all the modern Python tooling pre-configured.
For detailed architecture patterns and production deployment strategies, see [ARCHITECTURE.md](ARCHITECTURE.md).
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ init: ## Set up Python version, venv, and install dependencies
fi
@echo "🎉 Environment setup complete!"

sync: ## Sync project dependencies
@echo "Syncing project dependencies..."
uv sync --extra dev
$(GREEN_LINE)

clean-project: ## Clean Python caches and tooling artifacts
@echo "Cleaning project caches..."
find . -type d \( -name '.pytest_cache' -o -name '.ruff_cache' -o -name '.mypy_cache' -o -name '__pycache__' \) -exec rm -rf {} +
Expand All @@ -49,10 +54,7 @@ clean-env: ## Remove the virtual environment folder
rm -rf .venv
$(GREEN_LINE)

sync: ## Sync project dependencies
@echo "Syncing project dependencies..."
uv sync --extra dev
$(GREEN_LINE)


# ----------------------------
# Code Quality
Expand Down
Loading
Loading