# aryan09092001/self-red-teaming-multi-agent-platform architecture

> Architecture diagram of the GitHub repository aryan09092001/self-red-teaming-multi-agent-platform, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aryan09092001/self-red-teaming-multi-agent-platform
- Repository: https://github.com/aryan09092001/self-red-teaming-multi-agent-platform

## Overview

#### Purpose and Entry Points
This is an AWS-deployed asynchronous research-report platform with self-red-teaming. A FastAPI API accepts research jobs, returns a job ID, and a co-hosted worker produces safety-checked reports through a multi-agent LLM graph. A separate PyRIT dashboard attacks the live API.

Core entry points:
- API and job worker: `app/main.py`
- Agent graph: `app/agents.py`
- Red-team service: `pyrit_dashboard/main.py`
- Browser UI: `index.html`

#### API, Queue, and Job Lifecycle
`POST /research` validates/authenticates the request, rate-limits it, runs the input safety check, then puts accepted work on a Redis Stream rather than awaiting LLM execution. Clients poll result endpoints by `job_id`.

The worker in `app/main.py` consumes and acknowledges queue messages via `app/queue.py`. Job state/results are persisted in Redis. API and worker currently share one process, an intentional scaling boundary.

#### Safety and Access Boundary
Safety is enforced at both sides of generation:
- Input guardrail rejects unsafe topics before queueing/model usage.
- Output guardrail rejects generated reports before persistence, caching, or return.

AWS Bedrock Guardrails integration is isolated in `app/guardrails.py`; optional API-key middleware is in `app/auth.py`. The README identifies a current prompt-injection gap: classification occurs on raw topic input rather than the assembled prompt.

#### Retrieval, Cache, and Memory Tiers
Worker processing uses a cost/latency-ordered lookup:
1. Semantic Redis cache for near-duplicate topics.
2. pgvector-backed long-term report recall.
3. Full agent graph on misses.

`app/cache.py` implements embedding-similarity caching. `app/memory.py` manages Redis session history (short-term context) and PostgreSQL/pgvector report memory (recall, writer context, and report comparison). `app/pool.py` owns database connectivity. Only output-guardrail-passed generated reports enter cache or long-term storage.

#### Multi-Agent Research Graph and LLM Boundary
`app/agents.py` defines a LangGraph shared-state pipeline: search → summarize → write → verify. The critic can route failed reports back to search until a configured iteration cap, bounding retry cost.

Agents call TensorZero rather than providers directly. `tensorzero/tensorzero.toml` defines routing from OpenAI GPT-4o to Groq fallback; prompt specializations live in:
- `tensorzero/templates/research_summarize_system.minijinja`
- `tensorzero/templates/report_write_system.minijinja`

`app/retry.py` adds application-level exponential retry around calls. This separates model/provider failover policy from Python orchestration.

#### Results, Evaluation, and Red Teaming
Generated reports can be returned as text, structured JSON, PDF, and diffs through output handling in `app/output.py`. Evaluation is asynchronous: four LLM judges score completed reports without delaying job completion, implemented in `app/eval.py` and traced through LangSmith.

The independent PyRIT FastAPI service in `pyrit_dashboard/main.py` runs jailbreak, injection, crescendo, and skeleton-key attacks against the deployed app, persisting its run data through Redis/SQLite-oriented harness dependencies. It is exposed separately from the main API.

#### Deployment and Operating Environment
The production runtime is ECS Fargate behind an ALB, with the app, TensorZero sidecar, and PyRIT service packaged by:
- `app/Dockerfile`
- `tensorzero/Dockerfile`
- `pyrit_dashboard/Dockerfile`

`terraform/main.tf` provisions VPC/private networking, ECS, ALB, ElastiCache Redis, RDS PostgreSQL with pgvector, Bedrock guardrails, ECR, Secrets Manager, IAM, CloudWatch, and scheduled EventBridge red-team execution. `.github/workflows/deploy.yml` builds images, publishes to ECR, updates ECS services, waits for stability, and rolls back on failure.

Main entry points: [`app/main.py`](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/main.py) (API + job worker), [`app/output.py`](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/output.py) (Result output), [`app/guardrails.py`](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/guardrails.py) (Bedrock guardrails), [`app/agents.py`](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/agents.py) (Research agent graph)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Research Runtime"]
  node_client(("Research clients"))
  node_api_worker["API + job worker<br/>FastAPI / worker<br/>[main.py]"]
  node_queue["Redis Stream<br/>job queue<br/>[queue.py]"]
  node_output["Result output<br/>[output.py]"]
end

subgraph group_intelligence["Research &amp; Models"]
  node_cache["Semantic cache<br/>similarity lookup<br/>[cache.py]"]
  node_memory["Report memory<br/>pgvector recall<br/>[memory.py]"]
  node_agent_graph["Research agent graph<br/>LangGraph pipeline<br/>[agents.py]"]
  node_tensorzero{{"TensorZero sidecar<br/>LLM gateway<br/>[tensorzero.toml]"}}
  node_prompts["Prompt templates<br/>Minijinja prompts"]
end

subgraph group_data["State &amp; Safety"]
  node_auth["API-key middleware<br/>access control<br/>[auth.py]"]
  node_guardrails["Bedrock guardrails<br/>input/output safety<br/>[guardrails.py]"]
  node_redis[("Redis")]
  node_postgres[("PostgreSQL + pgvector<br/>report store<br/>[pool.py]")]
end

subgraph group_assurance["Evaluation &amp; Red Team"]
  node_evaluation["Async evaluation<br/>four LLM judges<br/>[eval.py]"]
  node_pyrit["PyRIT dashboard<br/>red-team FastAPI service<br/>[main.py]"]
end

subgraph group_delivery["AWS Delivery"]
  node_ecs{{"ECS Fargate + ALB<br/>production runtime"}}
  node_terraform["Terraform infrastructure<br/>AWS provisioning<br/>[main.tf]"]
  node_deploy["Deploy workflow<br/>CI/CD<br/>[deploy.yml]"]
end

node_client -->|"POST /research; poll by job ID"| node_api_worker
node_api_worker -->|"authenticate / rate-limit"| node_auth
node_api_worker -->|"input check"| node_guardrails
node_guardrails -->|"accepted jobs"| node_queue
node_queue -->|"consume + acknowledge"| node_api_worker
node_api_worker -->|"job state and results"| node_redis
node_api_worker -->|"first lookup"| node_cache
node_cache -->|"embeddings / cached reports"| node_redis
node_api_worker -->|"cache miss recall"| node_memory
node_memory -->|"long-term reports"| node_postgres
node_api_worker -->|"full miss generation"| node_agent_graph
node_agent_graph -->|"agent calls with retries"| node_tensorzero
node_prompts -->|"specialized prompts"| node_tensorzero
node_agent_graph -->|"generated report output check"| node_guardrails
node_guardrails -->|"passed reports only"| node_output
node_output -->|"persist result"| node_redis
node_output -->|"populate after pass"| node_cache
node_output -->|"store after pass"| node_memory
node_output -.->|"completed reports"| node_evaluation
node_pyrit -->|"live attack campaigns"| node_api_worker
node_deploy -->|"deploy containers"| node_ecs
node_terraform -->|"provisions runtime"| node_ecs
node_ecs -->|"hosts app"| node_api_worker
node_ecs -->|"hosts sidecar"| node_tensorzero
node_ecs -->|"hosts separate service"| node_pyrit

click node_api_worker "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/main.py"
click node_queue "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/queue.py"
click node_auth "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/auth.py"
click node_guardrails "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/guardrails.py"
click node_cache "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/cache.py"
click node_memory "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/memory.py"
click node_postgres "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/pool.py"
click node_agent_graph "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/agents.py"
click node_tensorzero "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/tensorzero/tensorzero.toml"
click node_prompts "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/tensorzero/templates/report_write_system.minijinja"
click node_output "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/output.py"
click node_evaluation "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/eval.py"
click node_pyrit "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/pyrit_dashboard/main.py"
click node_terraform "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/terraform/main.tf"
click node_deploy "https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/.github/workflows/deploy.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_client,node_api_worker,node_queue,node_output toneBlue
class node_cache,node_memory,node_agent_graph,node_tensorzero,node_prompts toneAmber
class node_auth,node_guardrails,node_redis,node_postgres toneMint
class node_evaluation,node_pyrit toneRose
class node_ecs,node_terraform,node_deploy toneIndigo
```

## Components

### Research Runtime

Co-hosted API and asynchronous worker.

- **Research clients**: client
- [API + job worker](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/main.py): FastAPI / worker · Accepts jobs, polls results, and consumes stream work in one process. · `app/main.py`
- [Redis Stream](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/queue.py): job queue · Decouples accepted research jobs from LLM execution. · `app/queue.py`
- [Result output](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/output.py): text / JSON / PDF / diff · `app/output.py`

### Research & Models

Retrieval and multi-agent generation.

- [Semantic cache](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/cache.py): similarity lookup · Checks near-duplicate topics before long-term recall or generation. · `app/cache.py`
- [Report memory](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/memory.py): pgvector recall · Provides long-term report recall, writer context, and comparison. · `app/memory.py`
- [Research agent graph](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/agents.py): LangGraph pipeline · Searches, summarizes, writes, and verifies; failed reports loop to search within a cap. · `app/agents.py`
- [TensorZero sidecar](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/tensorzero/tensorzero.toml): LLM gateway · Routes GPT-4o calls to Groq fallback. · `tensorzero/tensorzero.toml`
- [Prompt templates](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/tensorzero/templates/report_write_system.minijinja): Minijinja prompts · `tensorzero/templates/report_write_system.minijinja`

### State & Safety

Guardrails, job state, cache, and memory.

- [API-key middleware](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/auth.py): access control · `app/auth.py`
- [Bedrock guardrails](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/guardrails.py): input/output safety · Checks topics before queueing and reports before persistence or return. · `app/guardrails.py`
- **Redis**: jobs / sessions / cache · Stores job state, stream messages, session history, and semantic cache.
- [PostgreSQL + pgvector](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/pool.py): report store · `app/pool.py`

### Evaluation & Red Team

Post-completion assessment and adversarial testing.

- [Async evaluation](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/app/eval.py): four LLM judges · Scores completed reports without delaying job completion and traces through LangSmith. · `app/eval.py`
- [PyRIT dashboard](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/pyrit_dashboard/main.py): red-team FastAPI service · Attacks the live API with jailbreak, injection, crescendo, and skeleton-key campaigns. · `pyrit_dashboard/main.py`

### AWS Delivery

Container deployment and infrastructure.

- **ECS Fargate + ALB**: production runtime · Runs separately exposed app, TensorZero, and PyRIT containers.
- [Terraform infrastructure](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/terraform/main.tf): AWS provisioning · Provisions networking, ECS, Redis, RDS, guardrails, secrets, IAM, and observability. · `terraform/main.tf`
- [Deploy workflow](https://github.com/aryan09092001/self-red-teaming-multi-agent-platform/blob/main/.github/workflows/deploy.yml): CI/CD · Builds and publishes images, updates ECS, verifies stability, and rolls back failures. · `.github/workflows/deploy.yml`

## Connections

- Research clients → API + job worker: POST /research; poll by job ID
- API + job worker → API-key middleware: authenticate / rate-limit
- API + job worker → Bedrock guardrails: input check
- Bedrock guardrails → Redis Stream: accepted jobs
- Redis Stream → API + job worker: consume + acknowledge
- API + job worker → Redis: job state and results
- API + job worker → Semantic cache: first lookup
- Semantic cache → Redis: embeddings / cached reports
- API + job worker → Report memory: cache miss recall
- Report memory → PostgreSQL + pgvector: long-term reports
- API + job worker → Research agent graph: full miss generation
- Research agent graph → TensorZero sidecar: agent calls with retries
- Prompt templates → TensorZero sidecar: specialized prompts
- Research agent graph → Bedrock guardrails: generated report output check
- Bedrock guardrails → Result output: passed reports only
- Result output → Redis: persist result
- Result output → Semantic cache: populate after pass
- Result output → Report memory: store after pass
- Result output → Async evaluation: completed reports
- PyRIT dashboard → API + job worker: live attack campaigns
- Deploy workflow → ECS Fargate + ALB: deploy containers
- Terraform infrastructure → ECS Fargate + ALB: provisions runtime
- ECS Fargate + ALB → API + job worker: hosts app
- ECS Fargate + ALB → TensorZero sidecar: hosts sidecar
- ECS Fargate + ALB → PyRIT dashboard: hosts separate service
