# ara-5/ai-security-gateway architecture

> Architecture diagram of the GitHub repository ara-5/ai-security-gateway, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ara-5/ai-security-gateway
- Repository: https://github.com/ara-5/ai-security-gateway

## Overview

AI Security Gateway screens user input and model output, produces an allow/flag/sanitize/block verdict, and records an audit event. Its central runtime path is request entry → direction-specific detectors → risk scoring and decision → audit. The README also documents model backends, labeled-corpus evaluation, multi-model comparison, and baseline replay. Some evaluation modules were not sampled; their documented relationships are shown with README evidence rather than implying source inspection.

Main entry points: [`gateway/pipeline.py`](https://github.com/ara-5/ai-security-gateway/blob/main/gateway/pipeline.py) (Security gateway), [`cli.py`](https://github.com/ara-5/ai-security-gateway/blob/main/cli.py) (CLI commands), [`benchmark/multi_model.py`](https://github.com/ara-5/ai-security-gateway/blob/main/benchmark/multi_model.py) (Multi-model comparison), [`replay/engine.py`](https://github.com/ara-5/ai-security-gateway/blob/main/replay/engine.py) (Attack replay and diff)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Request entry"]
  node_cli["CLI commands<br/>[cli.py]"]
  node_http["HTTP API<br/>[api.py]"]
end

subgraph group_inspection["Security inspection"]
  node_request["Request and verdict types<br/>[schemas.py]"]
  node_gateway["Security gateway<br/>[pipeline.py]"]
  node_detectors["Security detectors"]
  node_detector_base["Detector foundation<br/>[base.py]"]
  node_scoring["Risk scoring and decisions<br/>[scoring.py]"]
  node_config["Gateway thresholds<br/>[config.py]"]
  node_audit["Audit logger<br/>[audit.py]"]
  node_audit_files[("Daily JSONL audit logs")]
end

subgraph group_models["Model backends"]
  node_registry["Backend registry<br/>[registry.py]"]
  node_mock["Offline mock models<br/>[mock_backend.py]"]
  node_model_interface["Model backend interface<br/>[base.py]"]
  node_provider_models["OpenAI and Anthropic"]
end

subgraph group_evaluation["Evaluation and replay"]
  node_corpora["Attack and holdout corpora"]
  node_evaluation_run["Detection benchmark<br/>[evaluate.py]"]
  node_multi_model["Multi-model comparison<br/>[multi_model.py]"]
  node_replay["Attack replay and diff<br/>[engine.py]"]
  node_baseline["Replay baseline"]
end

node_user(("User or agent"))

node_user -->|"runs command"| node_cli
node_user -.->|"sends request"| node_http
node_cli -->|"inspects text"| node_gateway
node_http -->|"routes inspection"| node_gateway
node_http -->|"selects backend"| node_registry
node_gateway -->|"runs detectors"| node_detectors
node_detectors -->|"uses foundation"| node_detector_base
node_detectors -->|"reads request"| node_request
node_gateway -->|"aggregates results"| node_scoring
node_scoring -->|"uses thresholds"| node_config
node_gateway -->|"limits input"| node_config
node_gateway -->|"records verdict"| node_audit
node_audit -->|"appends events"| node_audit_files
node_audit -->|"reads request fields"| node_request
node_registry -->|"registers mocks"| node_mock
node_registry -->|"loads when keyed"| node_provider_models
node_mock -->|"implements"| node_model_interface
node_cli -->|"runs benchmark"| node_evaluation_run
node_evaluation_run -.->|"evaluates cases"| node_corpora
node_cli -->|"runs comparison"| node_multi_model
node_multi_model -.->|"uses backends"| node_registry
node_multi_model -.->|"compares protection"| node_gateway
node_multi_model -.->|"tests attacks"| node_corpora
node_cli -->|"runs replay"| node_replay
node_replay -->|"inspects cases"| node_gateway
node_replay -->|"loads corpus"| node_corpora
node_replay -.->|"diffs outcomes"| node_baseline

click node_cli "https://github.com/ara-5/ai-security-gateway/blob/main/cli.py"
click node_http "https://github.com/ara-5/ai-security-gateway/blob/main/gateway/api.py"
click node_request "https://github.com/ara-5/ai-security-gateway/blob/main/gateway/schemas.py"
click node_gateway "https://github.com/ara-5/ai-security-gateway/blob/main/gateway/pipeline.py"
click node_detectors "https://github.com/ara-5/ai-security-gateway/tree/main/gateway/detectors"
click node_detector_base "https://github.com/ara-5/ai-security-gateway/blob/main/gateway/detectors/base.py"
click node_scoring "https://github.com/ara-5/ai-security-gateway/blob/main/gateway/scoring.py"
click node_config "https://github.com/ara-5/ai-security-gateway/blob/main/gateway/config.py"
click node_audit "https://github.com/ara-5/ai-security-gateway/blob/main/gateway/audit.py"
click node_audit_files "https://github.com/ara-5/ai-security-gateway/tree/main/logs"
click node_registry "https://github.com/ara-5/ai-security-gateway/blob/main/models/registry.py"
click node_mock "https://github.com/ara-5/ai-security-gateway/blob/main/models/mock_backend.py"
click node_model_interface "https://github.com/ara-5/ai-security-gateway/blob/main/models/base.py"
click node_provider_models "https://github.com/ara-5/ai-security-gateway/tree/main/models"
click node_corpora "https://github.com/ara-5/ai-security-gateway/tree/main/data"
click node_evaluation_run "https://github.com/ara-5/ai-security-gateway/blob/main/benchmark/evaluate.py"
click node_multi_model "https://github.com/ara-5/ai-security-gateway/blob/main/benchmark/multi_model.py"
click node_replay "https://github.com/ara-5/ai-security-gateway/blob/main/replay/engine.py"
click node_baseline "https://github.com/ara-5/ai-security-gateway/blob/main/data/replay_baseline.json"

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_cli,node_http,node_user toneBlue
class node_request,node_gateway,node_detectors,node_detector_base,node_scoring,node_config,node_audit,node_audit_files toneAmber
class node_registry,node_mock,node_model_interface,node_provider_models toneMint
class node_corpora,node_evaluation_run,node_multi_model,node_replay,node_baseline toneRose
```

## Components

### Request entry

- [CLI commands](https://github.com/ara-5/ai-security-gateway/blob/main/cli.py): `cli.py`
- [HTTP API](https://github.com/ara-5/ai-security-gateway/blob/main/gateway/api.py): `gateway/api.py`

### Security inspection

- [Request and verdict types](https://github.com/ara-5/ai-security-gateway/blob/main/gateway/schemas.py): `gateway/schemas.py`
- [Security gateway](https://github.com/ara-5/ai-security-gateway/blob/main/gateway/pipeline.py): `gateway/pipeline.py`
- [Security detectors](https://github.com/ara-5/ai-security-gateway/tree/main/gateway/detectors): `gateway/detectors`
- [Detector foundation](https://github.com/ara-5/ai-security-gateway/blob/main/gateway/detectors/base.py): `gateway/detectors/base.py`
- [Risk scoring and decisions](https://github.com/ara-5/ai-security-gateway/blob/main/gateway/scoring.py): `gateway/scoring.py`
- [Gateway thresholds](https://github.com/ara-5/ai-security-gateway/blob/main/gateway/config.py): `gateway/config.py`
- [Audit logger](https://github.com/ara-5/ai-security-gateway/blob/main/gateway/audit.py): `gateway/audit.py`
- [Daily JSONL audit logs](https://github.com/ara-5/ai-security-gateway/tree/main/logs): `logs`

### Model backends

- [Backend registry](https://github.com/ara-5/ai-security-gateway/blob/main/models/registry.py): `models/registry.py`
- [Offline mock models](https://github.com/ara-5/ai-security-gateway/blob/main/models/mock_backend.py): `models/mock_backend.py`
- [Model backend interface](https://github.com/ara-5/ai-security-gateway/blob/main/models/base.py): `models/base.py`
- [OpenAI and Anthropic](https://github.com/ara-5/ai-security-gateway/tree/main/models): `models`

### Evaluation and replay

- [Attack and holdout corpora](https://github.com/ara-5/ai-security-gateway/tree/main/data): `data`
- [Detection benchmark](https://github.com/ara-5/ai-security-gateway/blob/main/benchmark/evaluate.py): `benchmark/evaluate.py`
- [Multi-model comparison](https://github.com/ara-5/ai-security-gateway/blob/main/benchmark/multi_model.py): `benchmark/multi_model.py`
- [Attack replay and diff](https://github.com/ara-5/ai-security-gateway/blob/main/replay/engine.py): `replay/engine.py`
- [Replay baseline](https://github.com/ara-5/ai-security-gateway/blob/main/data/replay_baseline.json): `data/replay_baseline.json`

### Other components

- **User or agent**

## Connections

- User or agent → CLI commands: runs command (evidence: `cli.py`)
- User or agent → HTTP API: sends request (evidence: `README.md`)
- CLI commands → Security gateway: inspects text (evidence: `cli.py`)
- HTTP API → Security gateway: routes inspection (evidence: `gateway/api.py`)
- HTTP API → Backend registry: selects backend (evidence: `gateway/api.py`)
- Security gateway → Security detectors: runs detectors (evidence: `gateway/pipeline.py`)
- Security detectors → Detector foundation: uses foundation (evidence: `gateway/detectors/prompt_injection.py`)
- Security detectors → Request and verdict types: reads request (evidence: `gateway/detectors/prompt_injection.py`)
- Security gateway → Risk scoring and decisions: aggregates results (evidence: `gateway/pipeline.py`)
- Risk scoring and decisions → Gateway thresholds: uses thresholds (evidence: `gateway/scoring.py`)
- Security gateway → Gateway thresholds: limits input (evidence: `gateway/pipeline.py`)
- Security gateway → Audit logger: records verdict (evidence: `gateway/pipeline.py`)
- Audit logger → Daily JSONL audit logs: appends events (evidence: `gateway/audit.py`)
- Audit logger → Request and verdict types: reads request fields (evidence: `gateway/audit.py`)
- Backend registry → Offline mock models: registers mocks (evidence: `models/registry.py`)
- Backend registry → OpenAI and Anthropic: loads when keyed (evidence: `models/registry.py`)
- Offline mock models → Model backend interface: implements (evidence: `models/mock_backend.py`)
- CLI commands → Detection benchmark: runs benchmark (evidence: `cli.py`)
- Detection benchmark → Attack and holdout corpora: evaluates cases (evidence: `README.md`)
- CLI commands → Multi-model comparison: runs comparison (evidence: `cli.py`)
- Multi-model comparison → Backend registry: uses backends (evidence: `README.md`)
- Multi-model comparison → Security gateway: compares protection (evidence: `README.md`)
- Multi-model comparison → Attack and holdout corpora: tests attacks (evidence: `README.md`)
- CLI commands → Attack replay and diff: runs replay (evidence: `cli.py`)
- Attack replay and diff → Security gateway: inspects cases (evidence: `replay/engine.py`)
- Attack replay and diff → Attack and holdout corpora: loads corpus (evidence: `replay/engine.py`)
- Attack replay and diff → Replay baseline: diffs outcomes (evidence: `README.md`)
