> ## Documentation Index
> Fetch the complete documentation index at: https://docs.promptguard.co/llms.txt
> Use this file to discover all available pages before exploring further.

# How the gateway handles a request

> How PromptGuard sits in front of your LLM provider and what it does on every request

PromptGuard is a gateway between your application and your LLM provider. Every request flows through five stages, in this order:

```mermaid theme={"system"}
flowchart TD
    App(["Your app"]) --> Auth{"1 · Authenticate<br/>X-API-Key"}
    Auth -->|"invalid / revoked"| E401["401 Unauthorized"]
    Auth -->|valid| ACL{"2 · Access list<br/>IP · end-user · country"}
    ACL -->|"block rule matches"| E403["403 Blocked"]
    ACL -->|allowed| Policy{"3 · Policy engine<br/>injection · PII · jailbreak · exfil"}
    Policy -->|block| Blk["Block"]
    Policy -->|"allow / redact"| Route["4 · Routing rules<br/>rewrite provider / model"]
    Route --> Cred["5 · Resolve provider key<br/>inject upstream Authorization"]
    Cred --> Up(["LLM provider"])
    Up -->|"5xx & failover set<br/>non-streaming"| FO["Retry once:<br/>failover provider"]
    Policy -.->|"decision · tokens"| Log[("security_events")]
```

**1. Authenticate the call**

Your `X-API-Key` is verified against the project that issued it. Invalid or revoked keys return `401` immediately.

**2. Check the access list**

The request's source IP, end-user ID, and (if cached) country are matched against your project's [Allow & Block lists](https://docs.promptguard.co/gateway/access-lists.md). Block rules always win. A non-empty allow list flips the project to default-deny.

**3. Run the policy engine**

The prompt is evaluated against your [security policies](https://docs.promptguard.co/security/overview.md) (prompt injection, PII, jailbreaks, exfiltration, custom rules). Decisions: `allow`, `block`, or `redact`. A blocked request stops here.

**4. Apply routing rules**

[Routing rules](https://docs.promptguard.co/gateway/routing-rules.md) can rewrite the upstream provider/model based on the request shape, e.g. downgrade short prompts to a cheaper model, or pin a specific tenant to a specific vendor.

**5. Resolve the upstream credential**

If you've stored a [Provider Key](https://docs.promptguard.co/gateway/provider-keys.md), PromptGuard injects it as the upstream `Authorization` header so your app code never has to ship vendor credentials. The original `Authorization` header on the inbound request is replaced.

Every decision is persisted to `security_events` with the source IP, end-user ID, country (looked up lazily from the cached GeoIP table), and tokens in/out, so the dashboard can show you per-end-user, per-country, and per-project rollups without a separate logging pipeline. Add [request tags](https://docs.promptguard.co/gateway/request-tags.md) to label requests by feature or customer segment and filter their events by label.

**Prefer to watch?** This page as a silent, step-by-step video:

**Tutorial 04** ·  · a silent follow-along of this page. Pressing
play loads the video from YouTube.
Watch on YouTube

**Prefer to watch?** This page as a silent, step-by-step video:

**Tutorial 04** ·  · a silent follow-along of this page. Pressing
play loads the video from YouTube.
Watch on YouTube

## Why this shape

Most "AI security" products only do step 3, the policy engine. PromptGuard owns the full gateway because the access decisions you actually care about live in the cross-product of all five stages:

* "Block this user across every project I own", solved by user-wide access lists (step 2).
* "Don't ship vendor secrets to my front-end", solved by Provider Keys (step 5).
* "Downgrade summarisation calls to Haiku, keep code generation on Opus", solved by Routing Rules (step 4).
* "Which end-user keeps getting flagged?", solved by [per-end-user rollups](https://docs.promptguard.co/gateway/end-users.md) of every decision.

The other Gateway pages cover each primitive end-to-end.

## Failover

If a Routing Rule specifies a `failover_provider` and the primary upstream returns 5xx on a non-streaming request, PromptGuard retries the same model exactly once against the failover. `failover_provider` does not retry streaming requests. Non-5xx responses (rate limits, validation errors) are surfaced to the caller as-is, you don't want a "smart" gateway turning a 400 into a 200 by silently switching vendors.

To fall back to a **different model** when the one you asked for is rate limited or down, name the models in the request, or in a routing rule's `fallback_models` action. That works for streamed requests too, before the first byte is sent. See [Fallback Models](https://docs.promptguard.co/gateway/fallback-models.md).

## Latency budget

The policy-engine stage is the only one that can do meaningful work. The other four stages are designed to be near-zero:

* Auth: cached for the lifetime of the request.
* Access list: a single indexed query (`user_id, project_id`).
* Routing rules: one query for the project's active rules, then in-Python evaluation.
* Provider key: a single indexed lookup, and decryption is one symmetric-cipher call (the per-customer key is derived in microseconds).

The policy engine is the dominant cost. See the [latency breakdown](https://docs.promptguard.co/platform/dashboard.md) on the Overview tile for live numbers from your account.

## Next steps

> [card] [Provider keys](https://docs.promptguard.co/gateway/provider-keys.md)
>
> Store your provider credentials once, forwarded server-side

> [card] [Integrations overview](https://docs.promptguard.co/integrations/overview.md)
>
> Every provider and framework, and how to connect each

> [card] [Streaming](https://docs.promptguard.co/guides/streaming.md)
>
> How streamed responses are buffered, scanned and released


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.