How mkmahmud/blog_ai_agent works
1. Overall shape
- This is an enterprise FastAPI backend for AI-assisted blog generation.
- The codebase is organized around clean architecture boundaries: API layer, application services, workflow/agents, persistence repositories, and infrastructure adapters.
2. Primary request entrypoint
- app/main.py is the application bootstrap for FastAPI.
- app/api contains the HTTP surface area: authentication, blog operations, and publishing endpoints, plus dependency wiring.
- Request/response contracts are defined in app/schemas, keeping the API boundary separate from database models.
3. Authentication and security boundary
- app/services/auth_service.py and app/api/auth.py implement user auth flows.
- app/core/security.py indicates JWT-based security.
- Cross-cutting controls also live in app/core/rate_limit.py, app/core/cache.py, app/core/logging.py, and app/core/metrics.py, suggesting the API is hardened for SaaS-style production use.
4. Application/use-case layer
- app/services is the main orchestration layer for business operations:
- blog creation and management,
- publishing workflows,
- auth-related use cases.
- This layer mediates between API requests and the lower-level repositories/workflow engine, which is important for diagramming the boundary between transport and domain logic.
5. Multi-agent content-generation pipeline
- The content workflow is modeled as a LangGraph state machine in app/graphs/workflow.py with state definitions in app/graphs/state.py.
- The documented sequence is:
- START -> Planner -> Research -> SEO -> Writer -> Editor -> Fact Checker -> Publisher -> END
- Each stage is implemented as a separate agent in app/agents, with prompt templates stored in prompts/.
- This is the core AI subsystem and should appear as a distinct pipeline in any architecture diagram.
6. LLM/provider abstraction
- app/llm/factory.py centralizes model/provider selection.
- The README says OpenAI is the default provider, with configuration switches for AWS Bedrock.
- This means the agent workflow does not call providers directly; it goes through a model factory abstraction, which is a key boundary.
7. Persistence layer
- app/database contains SQLAlchemy models, session setup, and Alembic metadata.
- app/repositories is the persistence boundary, with separate repositories for blogs, users, and agent logs.
- PostgreSQL is the primary relational datastore, and Alembic is used for schema migrations (alembic/versions/...initial_schema.py).
8. Retrieval / knowledge layer
- app/vectorstore/chroma.py shows a ChromaDB integration for retrieval-augmented generation.
- This likely supports research and fact-checking steps in the agent pipeline by storing and querying embeddings or reference content.
- In diagrams, this should be shown as a separate data store from PostgreSQL, not as part of the transactional database.
9. Async/background processing
- app/queue/redis_queue.py and app/queue/jobs.py indicate background job handling built on Redis.
- This is likely used for long-running workflow steps or publishing tasks so the API can return quickly while work continues asynchronously.
- Redis therefore serves two roles in the system: queue backend and supporting infrastructure for operational tasks.
10. Publishing and output boundary
- app/services/publishing_service.py and app/api/publish.py handle the final handoff from generated content to publication.
- The Publisher agent is the last stage in the workflow, which suggests publishing is treated as a distinct integration boundary rather than just another content transform.
11. Configuration and environment management
- app/core/config.py centralizes environment-driven configuration.
- The repo includes .env.example, Docker support, and separate deployment manifests, which indicates the runtime is designed to be configured via environment variables rather than hardcoded settings.
12. Infrastructure and deployment
- Local development uses Docker Compose for PostgreSQL, Redis, and ChromaDB.
- Deployment artifacts exist for AWS ECS Fargate and Kubernetes under infrastructure/.
- The repo also includes explicit ECS task definitions for both the API and migration jobs, showing that migrations are treated as a separate deployable concern.
13. Testing and validation
- tests/test_workflow.py exercises the multi-agent workflow.
- tests/test_security.py covers security behavior.
- This suggests the most important verification boundaries are workflow correctness and auth/security guarantees.
14. What to emphasize in an architecture diagram
- Show the API as the entrypoint, services as orchestration, the LangGraph agent pipeline as the core AI engine, and the repositories as the persistence boundary.
- Draw PostgreSQL, Redis, and ChromaDB as separate backing services.
- Include LLM providers (OpenAI / Bedrock) as external dependencies behind the model factory.
- Treat deployment/migrations as separate operational flows alongside the main API runtime.
Main entry points: api API layer · workflow.py Workflow · agents Agents · main.py App bootstrap
Components 18
Interface
HTTP API and request/response boundary
- App bootstrap · Application startup and composition root
app/main.py - API layer · Public endpoints for auth, blogs, and publishing
app/api - Contracts · Request and response models at the API boundary
app/schemas
Application
Business services and cross-cutting runtime controls
- Auth service · Authentication and user access orchestration
app/services/auth_service.py - Blog service · Blog creation and management orchestration
app/services/blog_service.py - Publish service · Final handoff from generated content to publication
app/services/publishing_service.py - Runtime core · Config, security, caching, logging, metrics, and rate limiting
app/core
AI Pipeline
LangGraph workflow, agents, prompts, and model abstraction
- Workflow · Orchestrates the AI content-generation sequence
app/graphs/workflow.py - Graph state · Shared state passed through the agent pipeline
app/graphs/state.py - Agents · Planner, research, SEO, writer, editor, fact-checker, and publisher agents
app/agents - Prompts · Task-specific prompt content for each agent stage
prompts - Model factory · Selects and configures OpenAI or Bedrock backends
app/llm/factory.py
Data
Transactional storage, retrieval, and background jobs
- Repositories · Blog, user, and agent-log data access layer
app/repositories - Postgres DB · Transactional relational storage and schema evolution
app/database - ChromaDB · Vector retrieval for research and fact-checking support
app/vectorstore/chroma.py - Redis queue · Async task dispatch and operational queue backend
app/queue
Ops
Deployment and migration artifacts
- AWS deploy · Production deployment and migration task definitions
infrastructure/aws - Kubernetes · Cluster deployment, service, config, and secret manifests
infrastructure/kubernetes
Connections 20
- App bootstrap → API layer: serves · Bootstraps the HTTP surface
- App bootstrap → Runtime core: configures · Loads runtime settings and cross-cutting controls
- API layer → Contracts: uses · Serializes and validates request/response contracts
- API layer → Auth service: auth calls · Routes authentication flows into the service layer
- API layer → Blog service: blog calls · Routes blog operations into the service layer
- API layer → Publish service: publish calls · Routes publishing requests into the service layer
- Auth service → Repositories: reads/writes · Persists and loads user/account data
- Blog service → Repositories: reads/writes · Persists and loads blog data
- Blog service → Workflow: starts · Triggers AI content generation workflow
- Publish service → Workflow: finalizes · Hands off generated content for publication
- Workflow → Graph state: transitions · Carries shared workflow state across stages
- Workflow → Agents: executes · Runs the planner/research/SEO/writer/editor/fact-checker/publisher stages
- Agents → Prompts: loads · Uses prompt templates per stage
- Agents → Model factory: requests model · Obtains provider-specific model clients through the factory
- Agents → ChromaDB: retrieves · Queries knowledge for research and verification
- Repositories → Postgres DB: stores · Persists through SQLAlchemy-backed relational storage
- Redis queue → Workflow: runs jobs · Supports asynchronous or long-running workflow execution
- Runtime core → API layer: guards · Applies security and operational controls to requests
- AWS deploy → App bootstrap: deploys · Production ECS runtime for the API and migrations
- Kubernetes → App bootstrap: deploys · Kubernetes runtime manifests for application components