Core concepts
Five ideas explain almost everything Aegis does: routes, stages, nodes and guardrails, verdicts, and RunState.
Request lifecycle
- Authenticate. The
Authorization: Bearer aeg-…key resolves to a principal (/metricsis the only unauthenticated path). With--no-authevery request is the anonymous principal. - Select a route.
/v1/runstakes aroutefield;/v1/chat/completionsuses themodelfield as the route name. - Ingress. The route's ingress nodes run in order against the request.
- Execute. The route's provider is called with the (possibly masked) messages.
- Egress. Egress nodes run against the response — e.g. PII unmasking.
- Record. The run's status and full event log go to the run store; a hash-chained
run_evidencerecord goes to the ledger.
Routes
A route binds a provider to a pipeline. Each route is compiled once at startup into its own LangGraph graph.
providers:
cheap:
type: fake
careful:
type: fake
guardrails:
pii:
pack: aegis.pii
mode: mask
classify:
pack: aegis.classification
pipeline: # the default pipeline for every route…
ingress: [pii]
egress: [pii]
routes:
default:
provider: cheap
sensitive:
provider: careful
pipeline: # …unless the route declares its own
ingress: [classify, pii]
egress: [pii]Stages, nodes and guardrails
A guardrail entry in aegis.yaml names a pack. At startup the pack's factory turns that entry into one or more pipeline nodes, each assigned to a stage. That is why a single pii entry can place a mask node on ingress and an unmask node on egress.
Two kinds of object end up in a stage:
| Contract | Returns | Use it for | |
|---|---|---|---|
| Guardrail | async scan(state) -> Verdict | a verdict | Yes/no/pause decisions: residency, budgets, injection checks |
| Pipeline node | async run(state) -> RunStateDelta | a partial state update | Transformations: masking, labelling, retrieval |
Guardrails are wrapped in a GuardNode, which runs its guards in order, records one event per verdict, and stops at the first block or pause.
Verdicts
Every guardrail returns exactly one verdict:
| Verdict | Constructor | Effect |
|---|---|---|
| allow | Verdict.allow() | Continue unchanged. |
| sanitize | Verdict.sanitize(replacement) | Continue, with the latest user message replaced by replacement. |
| block | Verdict.block(reason) | Stop. Run status blocked; the provider is never called if it happens on ingress. |
| require_approval | Verdict.require_approval(prompt) | Checkpoint and pause. Run status paused until a reviewer resumes it. |
Every verdict — including allow — becomes an event in the run's log and in the ledger, so "which guard let this through?" is answerable.
A run that goes through with something changed on the way records a sanitize, whatever did the changing:
| Where | Recorded as sanitize when |
|---|---|
| PII masking, content pack entities | values were replaced by placeholders — the reason lists types and counts (masked 1 CA_SIN, 1 EMAIL_ADDRESS), never the values |
| RAG retrieval | a passage failed a guard and was withheld from the model; the other passages still go |
A guard returning Verdict.sanitize | the latest user message was replaced |
A streaming guard's scan_chunk | a chunk was replaced before it was sent |
Targeted rewrites belong in nodes
Verdict.sanitize replaces the whole latest user message. For surgical edits (masking a single entity, redacting a span) write a pipeline node that returns rewritten messages in its RunStateDelta — the PII pack does, and records its own sanitize event (see aegis_core.masking).
RunState
Nodes never import each other; they communicate only through RunState:
| Field | Purpose |
|---|---|
run_id, route, principal | Identity of the run. |
messages | The conversation, as the next node will see it. |
labels | Free-form dict[str, str] for cross-pack signals — the classification pack writes labels["classification"]. |
mask_map | Placeholder → original value. Never sent to the model. |
events | Append-only audit log (stage, node, event_type, data). Every node_end carries duration_ms, so a run shows where its time went. |
usage | Token and cost accounting. |
response, status | Output and one of running, completed, blocked, paused, denied. |
A node returns a RunStateDelta; None fields are left untouched and events are appended.
Run statuses
| Status | Meaning |
|---|---|
completed | All stages passed; response is set. |
blocked | A guardrail returned block. |
paused | Waiting for a reviewer (require_approval). |
denied | A reviewer denied a paused run. |
pending / running | Background run not yet finished. |
error | An exception escaped the pipeline. |
Next
- Human approvals — the
require_approvalpath end to end. - Audit & evidence ledger — where the events go.
- Write a plugin — add your own guardrail.