Backend Development Skill
Build and maintain Python/FastAPI backend, database operations, and Intercom integration.
Workflow
Phase 1: Understand Requirements
- •
Review Context
- •Read
docs/architecture.mdfor system design - •Check
src/db/schema.sqlfor current schema - •Review existing patterns in codebase
- •Read
- •
Identify Scope
- •What files need to be modified?
- •Are migrations required?
- •Do API contracts change?
- •Does this affect other agents' domains?
Phase 2: Plan Implementation
- •
Design Approach
- •Follow existing patterns in codebase
- •Use Pydantic models for all data structures
- •Plan database operations (transactions, bulk operations)
- •Consider error handling and edge cases
- •
Check Dependencies
- •Will this affect frontend? (Coordinate with Sophia)
- •Does this change API contracts? (Document)
- •Are new environment variables needed? (Update
.env.example)
Phase 3: Implement
- •
Write Type-Safe Code
- •Use type hints everywhere
- •Pydantic models for validation
- •Proper async/await for I/O operations
- •Context managers for database connections
- •
Follow Patterns
- •Database: Use patterns from
src/db/classification_storage.py - •API: Follow structure in
src/api/routers/ - •External APIs: Follow
src/intercom_client.pypatterns - •Error handling: Proper status codes and error messages
- •Database: Use patterns from
- •
Handle Edge Cases
- •Null values and empty results
- •Database connection failures
- •External API timeouts
- •Invalid input data
Phase 4: Verify
- •
Run Existing Tests
- •
pytest tests/ -vmust pass - •Fix any broken tests
- •Don't modify test code (Kenji's domain unless asked)
- •
- •
Manual Verification
- •Test with real data if possible
- •Check database state after operations
- •Verify API responses have correct status codes
Success Criteria
Before claiming completion:
- • Code has type hints on all functions
- • Database operations use context managers (
withblocks) - • API endpoints return appropriate HTTP status codes
- • No secrets hardcoded in code
- • Existing tests pass:
pytest tests/ -v - • Error handling covers failure cases
- • Pydantic models used for data validation
- • Async operations use
async/awaitwhere appropriate
Constraints
- •DO NOT touch frontend code (
frontend/,webapp/) - Sophia's domain - •DO NOT modify prompts or classification logic - Kai's domain
- •DO NOT write test files - Kenji's domain (unless specifically asked)
- •DO NOT hardcode secrets - use environment variables
- •ALWAYS run existing tests before claiming code works
- •ALWAYS use Pydantic models for data structures
- •ALWAYS use context managers for database operations
Key Files & Patterns
Database Layer
| File | Purpose |
|---|---|
src/db/schema.sql | Database schema definition |
src/db/connection.py | Connection management |
src/db/models.py | Pydantic models |
src/db/classification_storage.py | Reference for batch operations |
Pattern: Batch operations with execute_values
python
from psycopg2.extras import execute_values
with get_db_connection() as conn:
with conn.cursor() as cur:
execute_values(cur, query, data_list)
conn.commit()
API Layer
| File | Purpose |
|---|---|
src/api/main.py | FastAPI app initialization |
src/api/routers/ | API route definitions |
Pattern: Proper status codes and error responses
python
from fastapi import HTTPException, status
@router.get("/resource/{id}")
async def get_resource(id: int):
resource = await fetch_resource(id)
if not resource:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Resource {id} not found"
)
return resource
External API Integration
| File | Purpose |
|---|---|
src/intercom_client.py | Intercom API client |
reference/intercom-llm-guide.md | Integration patterns |
Pattern: Async client with error handling
python
async def fetch_data():
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response.raise_for_status()
return await response.json()
except aiohttp.ClientError as e:
logger.error(f"API call failed: {e}")
raise
Common Pitfalls
- •N+1 queries: Always batch database operations, use
execute_valuesfor bulk inserts - •Missing context managers: Database connections must use
withblocks - •Forgetting async: Intercom client and LLM calls should use async variants
- •Hardcoded values: Use environment variables for config
- •Swallowing exceptions: Log and re-raise, don't silently catch
If Blocked
If you cannot proceed:
- •State what you're stuck on
- •Explain what's not working (include error messages)
- •Share what you've already tried
- •Provide relevant code context
- •Ask the Tech Lead for guidance