# huggingface/safeagents architecture

> Architecture diagram of the GitHub repository huggingface/safeagents, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/huggingface/safeagents
- Repository: https://github.com/huggingface/safeagents

## Overview

safeagents is a local proxy that reviews model tool calls before forwarding responses to agent harnesses, with optional scanning and judge decisions. Its principal flow is harness request → upstream model response → tool-call review → unchanged or rewritten response. Separate CLI capabilities manage proxy processes, inspect recorded calls, and scrub or publish trajectories. The graph reflects README-documented capabilities where source excerpts are incomplete; unverified subsystem links are omitted.

Main entry points: [`src/safeagents/guard.py`](https://github.com/huggingface/safeagents/blob/main/src/safeagents/guard.py) (Tool-call review), [`src/safeagents/proxy.py`](https://github.com/huggingface/safeagents/blob/main/src/safeagents/proxy.py) (HTTP proxy), [`src/safeagents/cli.py`](https://github.com/huggingface/safeagents/blob/main/src/safeagents/cli.py) (CLI commands), [`src/safeagents/rewrite.py`](https://github.com/huggingface/safeagents/blob/main/src/safeagents/rewrite.py) (Response rewriting)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_proxy["Proxy and review"]
  node_proxy["HTTP proxy<br/>[proxy.py]"]
  node_rewrite["Response rewriting<br/>[rewrite.py]"]
  node_guard["Tool-call review<br/>[guard.py]"]
  node_policy["Request policy<br/>[policy.py]"]
  node_scan["ATIF scanning<br/>[scan.py]"]
  node_selfprotect["Proxy protection<br/>[selfprotect.py]"]
  node_judge["Decision judge<br/>[judge.py]"]
end

subgraph group_operations["Proxy operations"]
  node_cli["CLI commands<br/>[cli.py]"]
  node_runs["Proxy lifecycle<br/>[runs.py]"]
end

subgraph group_records["Observability"]
  node_monitor["Live monitor<br/>[monitor.py]"]
  node_journal[("Call journal<br/>[journal.py]")]
  node_sinks["Event sinks<br/>[sinks.py]"]
  node_inspect["Call inspection<br/>[cli.py]"]
end

subgraph group_sharing["Trajectory sharing"]
  node_scrub["Trajectory scrubbing<br/>[scrub.py]"]
end

node_harness(("Agent harness"))
node_model{{"Model server"}}
node_hub{{"Hugging Face Hub"}}

node_harness -->|"sends requests"| node_proxy
node_proxy -->|"forwards requests"| node_model
node_proxy -->|"guards responses"| node_rewrite
node_rewrite -->|"reviews tool calls"| node_guard
node_proxy -->|"returns response"| node_harness
node_guard -->|"checks policy"| node_policy
node_guard -->|"scans calls"| node_scan
node_guard -->|"checks protection"| node_selfprotect
node_guard -->|"judges findings"| node_judge
node_guard -->|"records calls"| node_journal
node_guard -->|"reports verdicts"| node_monitor
node_guard -->|"publishes events"| node_sinks
node_cli -->|"starts server"| node_proxy
node_cli -->|"manages processes"| node_runs
node_cli -->|"scrubs trajectories"| node_scrub
node_cli -->|"inspects records"| node_inspect
node_scrub -->|"publishes redacted copies"| node_hub

click node_cli "https://github.com/huggingface/safeagents/blob/main/src/safeagents/cli.py"
click node_runs "https://github.com/huggingface/safeagents/blob/main/src/safeagents/runs.py"
click node_proxy "https://github.com/huggingface/safeagents/blob/main/src/safeagents/proxy.py"
click node_rewrite "https://github.com/huggingface/safeagents/blob/main/src/safeagents/rewrite.py"
click node_guard "https://github.com/huggingface/safeagents/blob/main/src/safeagents/guard.py"
click node_policy "https://github.com/huggingface/safeagents/blob/main/src/safeagents/policy.py"
click node_scan "https://github.com/huggingface/safeagents/blob/main/src/safeagents/scan.py"
click node_selfprotect "https://github.com/huggingface/safeagents/blob/main/src/safeagents/selfprotect.py"
click node_judge "https://github.com/huggingface/safeagents/blob/main/src/safeagents/judge.py"
click node_monitor "https://github.com/huggingface/safeagents/blob/main/src/safeagents/monitor.py"
click node_journal "https://github.com/huggingface/safeagents/blob/main/src/safeagents/journal.py"
click node_sinks "https://github.com/huggingface/safeagents/blob/main/src/safeagents/sinks.py"
click node_inspect "https://github.com/huggingface/safeagents/blob/main/src/safeagents/cli.py"
click node_scrub "https://github.com/huggingface/safeagents/blob/main/src/safeagents/scrub.py"

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_proxy,node_rewrite,node_guard,node_policy,node_scan,node_selfprotect,node_judge toneBlue
class node_cli,node_runs toneAmber
class node_monitor,node_journal,node_sinks,node_inspect,node_model toneMint
class node_scrub toneRose
class node_harness,node_hub toneIndigo
```

## Components

### Proxy and review

- [HTTP proxy](https://github.com/huggingface/safeagents/blob/main/src/safeagents/proxy.py): `src/safeagents/proxy.py`
- [Response rewriting](https://github.com/huggingface/safeagents/blob/main/src/safeagents/rewrite.py): `src/safeagents/rewrite.py`
- [Tool-call review](https://github.com/huggingface/safeagents/blob/main/src/safeagents/guard.py): `src/safeagents/guard.py`
- [Request policy](https://github.com/huggingface/safeagents/blob/main/src/safeagents/policy.py): `src/safeagents/policy.py`
- [ATIF scanning](https://github.com/huggingface/safeagents/blob/main/src/safeagents/scan.py): `src/safeagents/scan.py`
- [Proxy protection](https://github.com/huggingface/safeagents/blob/main/src/safeagents/selfprotect.py): `src/safeagents/selfprotect.py`
- [Decision judge](https://github.com/huggingface/safeagents/blob/main/src/safeagents/judge.py): `src/safeagents/judge.py`

### Proxy operations

- [CLI commands](https://github.com/huggingface/safeagents/blob/main/src/safeagents/cli.py): `src/safeagents/cli.py`
- [Proxy lifecycle](https://github.com/huggingface/safeagents/blob/main/src/safeagents/runs.py): `src/safeagents/runs.py`

### Observability

- [Live monitor](https://github.com/huggingface/safeagents/blob/main/src/safeagents/monitor.py): `src/safeagents/monitor.py`
- [Call journal](https://github.com/huggingface/safeagents/blob/main/src/safeagents/journal.py): `src/safeagents/journal.py`
- [Event sinks](https://github.com/huggingface/safeagents/blob/main/src/safeagents/sinks.py): `src/safeagents/sinks.py`
- [Call inspection](https://github.com/huggingface/safeagents/blob/main/src/safeagents/cli.py): `src/safeagents/cli.py`

### Trajectory sharing

- [Trajectory scrubbing](https://github.com/huggingface/safeagents/blob/main/src/safeagents/scrub.py): `src/safeagents/scrub.py`

### Other components

- **Agent harness**
- **Model server**
- **Hugging Face Hub**

## Connections

- Agent harness → HTTP proxy: sends requests (evidence: `README.md`)
- HTTP proxy → Model server: forwards requests (evidence: `src/safeagents/proxy.py`)
- HTTP proxy → Response rewriting: guards responses (evidence: `src/safeagents/proxy.py`)
- Response rewriting → Tool-call review: reviews tool calls (evidence: `src/safeagents/rewrite.py`)
- HTTP proxy → Agent harness: returns response (evidence: `README.md`)
- Tool-call review → Request policy: checks policy (evidence: `src/safeagents/guard.py`)
- Tool-call review → ATIF scanning: scans calls (evidence: `src/safeagents/guard.py`)
- Tool-call review → Proxy protection: checks protection (evidence: `src/safeagents/guard.py`)
- Tool-call review → Decision judge: judges findings (evidence: `src/safeagents/guard.py`)
- Tool-call review → Call journal: records calls (evidence: `src/safeagents/guard.py`)
- Tool-call review → Live monitor: reports verdicts (evidence: `src/safeagents/guard.py`)
- Tool-call review → Event sinks: publishes events (evidence: `src/safeagents/guard.py`)
- CLI commands → HTTP proxy: starts server (evidence: `src/safeagents/cli.py`)
- CLI commands → Proxy lifecycle: manages processes (evidence: `src/safeagents/cli.py`)
- CLI commands → Trajectory scrubbing: scrubs trajectories (evidence: `src/safeagents/cli.py`)
- CLI commands → Call inspection: inspects records (evidence: `src/safeagents/cli.py`)
- Trajectory scrubbing → Hugging Face Hub: publishes redacted copies (evidence: `README.md`)
