Skip to content

Proxy modes and routes

privyx proxy and privyx run put an HTTP proxy between your client and a provider. This page covers how that proxy forwards requests: the two modes, the paths it masks, what happens to every other path, and which key the provider receives.

Two modes

transparent (default) gateway
Forwards a request to the upstream's host, at the path the client asked for one fixed URL, exactly as configured
Serves every path only the paths in proxy.routes, with POST
Masks the paths in proxy.routes every path it serves
Paths without a route forwarded as sent, or refused not served (404)
Key sent upstream the client's, unless Privyx has one only Privyx's own

Select the mode with --transparent / --gateway, or proxy.mode in a config file.

Transparent

The drop-in mode. A client points its base URL at Privyx, and every request goes to the same path on the provider:

privyx proxy --upstream https://api.openai.com
client  → http://localhost:8000/v1/chat/completions
Privyx  → https://api.openai.com/v1/chat/completions      (masked)

client  → http://localhost:8000/v1/models
Privyx  → https://api.openai.com/v1/models                (as sent)

Only the scheme, host, and port of the upstream URL are used; a path in it is ignored, unless provider.path_mapping keeps it. "Transparent" means drop-in: this is an ordinary reverse proxy that your client addresses on purpose, not a network-level interceptor.

Gateway

Some providers serve their API under a base path: https://api.deepseek.com/anthropic/v1/messages, an Azure deployment URL, a router's prefix. Transparent mode mirrors the client's path, so it reaches such an endpoint only when the client itself sends that prefix and a route names it, or with provider.path_mapping. In gateway mode Privyx posts every request to the one URL you configure, whatever path the client used:

# privyx.yaml
proxy:
  mode: gateway
  routes:
    /v1/messages: anthropic       # the path Privyx serves, and its wire schema
provider:
  type: anthropic                 # send the key as x-api-key
  base_url: https://api.deepseek.com/anthropic/v1/messages
export PRIVYX_API_KEY=...         # the key of the provider above: DeepSeek's
privyx -c privyx.yaml proxy
client  → http://localhost:8000/v1/messages
Privyx  → https://api.deepseek.com/anthropic/v1/messages  (masked)

Three things differ from transparent mode:

  • The client's own key is not relayed. Privyx sends the key it was given, or none.
  • Every served path posts to the same URL, so list in proxy.routes only the paths that URL can answer. Embeddings and the legacy completions API are the exception: they post beside it, so …/v1/chat/completions sends them to …/v1/embeddings and …/v1/completions. The OpenAI and Anthropic token-counting and compaction paths are never served in this mode: forwarding one to a chat endpoint would turn a count into a billed completion. Gemini paths are the other exception: with provider.type: google, each one posts to the model and method it names (see Google Gemini), and with any other provider they are not served. Ollama's paths under /api/ are served in transparent mode only.
  • Anything else, such as GET /v1/models, gets a 404.

Neither mode translates between wire schemas. An Anthropic client needs an upstream that speaks the Anthropic format, in either mode.

Routes

proxy.routes maps a request path to the wire schema of its body. A request is masked only when its path is in this map. The default:

Path Schema API
/v1/chat/completions openai OpenAI Chat Completions
/v1/completions openai OpenAI legacy completions
/v1/embeddings embeddings OpenAI embeddings
/v1/messages anthropic Anthropic Messages
/v1/messages/count_tokens anthropic Anthropic token counting
/v1/responses responses OpenAI Responses
/v1/responses/input_tokens responses Responses token counting
/v1/responses/compact responses Responses compaction
/chat/completions, /completions openai
/embeddings embeddings
/responses, /responses/input_tokens, /responses/compact responses
/*/models/*:generateContent, /*/models/*:streamGenerateContent gemini Gemini API, any version (v1beta, v1, v1alpha)
/*/models/*:countTokens gemini Gemini token counting
/*/models/*:embedContent, /*/models/*:batchEmbedContents gemini Gemini embeddings
the same three chat methods under /*/tunedModels/* gemini Gemini tuned models
/*/cachedContents gemini Gemini context caching
/api/chat, /api/generate ollama Ollama's own API
/api/embed, /api/embeddings embeddings Ollama embeddings

The path is matched without its query string, so /v1/messages?beta=true matches too. A Gemini path without its API version, such as /models/m:generateContent, matches the Gemini route it stands for: a client calls it when its base URL ends in the version, as pi does under privyx run pi. A * matches any characters except /, such as the model name in a Gemini path. A route is matched whatever the upstream behind it, so on a server where /api/chat is not Ollama's API, leave /api/chat out of proxy.routes. A route without a * wins over one with it; among those, the first listed wins.

The OpenAI paths without /v1 are what its client calls when the base URL lacks the /v1 it expects (OPENAI_BASE_URL=http://localhost:8000). They are masked like the others and forwarded to the same path, which the provider may not serve; set the base URL with /v1.

Embeddings

An embeddings request has its input masked like a chat message, and its reply, which holds only vectors, is returned as is. The vectors are therefore those of the masked text: ana@example.com is embedded as its token. Two things follow for a vector store:

  • Without an anchor, tokens are numbered per session, so documents embedded in one session and a query in another can give the same value different tokens, or one token to different values. With an anchor, a value gets the same token everywhere.
  • A store built from unmasked text before this route existed does not match queries embedded through Privyx now.

An input (or a legacy completions prompt) of token ids, such as [[101, 2023]], is refused with a 400: Privyx scans text, and the provider would decode the ids back to the original. Send the text instead.

To send embeddings unmasked instead, give proxy.routes without the embeddings paths; they are then forwarded as sent, or refused with proxy.passthrough_unknown: false.

Adding a route

A provider that serves a known format on another path needs a route for it. Gemini's OpenAI-compatible endpoint, for example:

proxy:
  routes:
    /v1beta/openai/chat/completions: openai

routes replaces the default map rather than extending it. List the default paths as well if the same proxy also serves them. Add a route only for a path whose body really has one of the formats above.

Paths without a route

In transparent mode, a path that is not in proxy.routes is forwarded as the client sent it. That keeps GET /v1/models, file uploads, and the rest of a provider's API working, and it means that nothing in those requests is masked. The common ones:

  • file uploads and batch jobs, in the OpenAI, Anthropic, and Gemini APIs
  • reads of stored objects, such as GET /v1/responses/{id}

To close that gap, refuse them instead:

proxy:
  passthrough_unknown: false

The shipped configs/strict.yaml enables this setting. Start it with privyx -c configs/strict.yaml proxy or use it with privyx run. This also blocks unlisted metadata and file endpoints, including /v1/models; add a route only when its body uses a supported masking schema. Route coverage does not imply that every field or entity is detected; see Limitations.

privyx -c configs/strict.yaml doctor lists effective masking routes and reports whether other paths are blocked or forwarded without body masking. It also shows default paths missing from a custom route map. A permissive configuration prints a warning while keeping the check successful; an empty map, malformed path, or unsupported schema fails the check.

Unknown-route decisions emit proxy.passthrough or proxy.blocked audit events, even if the upstream later fails. These contain no path, query, headers, or body. privyx audit stats counts them, and /metrics includes them under privyx_audit_events_total. For unmatched requests the later proxy.request event uses [unmatched] as its path.

$ curl -s http://localhost:8000/v1/files -d '{"purpose": "batch"}'
{"error": {"message": "privyx: /v1/files is not in proxy.routes and proxy.passthrough_unknown is false"}}

The answer is a 403, for harmless paths such as GET /v1/models too.

WebSocket

A WebSocket upgrade is answered 426 Upgrade Required and never forwarded: Privyx cannot relay one, so it could not mask its frames. Codex opens /v1/responses as a WebSocket first and falls back to HTTP at once on that status.

What is masked in a request

On a routed path, Privyx masks the parts of the body that carry conversation content and leaves the request's settings alone:

Masked Left as sent
Messages: text, tool results, tool-call arguments model, sampling settings, response_format
The system prompt (system, instructions) Tool names, parameter names, enum values
Responses input and prompt variables Ids, roles, types, signatures
Tool and parameter descriptions URLs, base64 data, images, files
What the client reports about its machine: Claude Code's safeguards, Codex's client_metadata and x-codex-turn-metadata header Every other request header
Query parameter values (on every path, routed or not) Auth query parameters (key, api_key, api-key, access_token)

Inside the masked parts, a field Privyx does not know is masked, not passed through: a field a provider adds later is covered from the start. The exact rule is in Proxy architecture.

What is restored in a reply

  • In a batch reply, every string: text, reasoning, tool-call arguments.
  • In a stream, text and reasoning deltas as they arrive; a token split between two chunks is held back until it is whole. Tool-call arguments are collected and delivered restored in one piece.
  • A reply labelled text/event-stream whose body is one JSON document, as some gateways answer a request that asks for no stream, is restored as a batch reply, and what follows the document is kept.
  • A token the model wrote without its outer delimiters (PRIVYX_EMAIL_4FF5EF825E496E1A) or with only one of them (<PRIVYX_EMAIL_4FF5EF825E496E1A), in another case (<privyx_email_4ff5ef825e496e1a>), or as its id alone when the id is long, is restored too. See Token format.

A token is restored only if the request's session issued it. A reply cannot make Privyx reveal a value from another session.

Keys and headers

Which key reaches the provider. The first of these that is set:

  1. the api_key of the provider in use. PRIVYX_OPENAI_API_KEY, PRIVYX_ANTHROPIC_API_KEY, and PRIVYX_GOOGLE_API_KEY set it for the built-in provider of that name, as in provider: anthropic, and PRIVYX_API_KEY for the provider the config selects, such as one written under provider itself;
  2. in transparent mode, the client's own Authorization, x-api-key, or x-goog-api-key header, or key query parameter; in gateway mode, none.

A key goes only to the base_url of the provider it is written for, or to its type's default host; see provider and providers.

A key held by Privyx replaces the client's on every request. The transparent proxy sends it as x-api-key on Anthropic paths, as x-goog-api-key on Gemini paths, and as a bearer token elsewhere; the gateway sends it as x-api-key when provider.type is anthropic and as x-goog-api-key when it is google. provider.forward_client_auth: false drops the client's credential headers (Authorization, x-api-key, x-goog-api-key, and Cookie) and key query parameters before anything else, so only a configured key can go upstream. An agent profile whose provider should relay them again sets forward_client_auth: true on that provider, or in the profile's privyx.provider.

Headers Privyx adds or removes.

  • x-privyx-session on a request names the session; Privyx removes it before forwarding and returns the session id it used in the same header of the response.
  • anthropic-version: 2023-06-01 is added on Anthropic paths when the client sent none.
  • provider.headers in the config are added to every upstream request.
  • Other request and response headers are relayed, so rate-limit and request-id headers from the provider reach the client.

Prompt caching

Providers cache a prompt by its exact bytes, so masking must give a turn the client sends again the same bytes on every later turn. It does: a value keeps its token for the whole session, and a request is masked in the order the provider caches it (tools, then system, then the messages), so a value that first appears later never renumbers what came before. A tool the client loads later through tool search is masked where the conversation loads it, so it does not renumber the system prompt either. That holds for the default fresh session per request too, since values are numbered in the order they appear. privyx audit stats and privyx session show report the cache hit rate from the usage the upstream returns.

Anthropic caches only what a request marks with cache_control. A client that marks nothing can have Privyx do it:

features:
  cache_control: true   # off by default

A request that sets cache_control nowhere then gets a 5-minute breakpoint on its last tool when tools is at least about 1,024 tokens, and on its last system block when tools and system together are. A cache write costs 1.25 times the input price and a read about a tenth, so this pays off from the second request with the same prefix. Claude Code and most SDK-based agents mark their prompts themselves, and are left as they are.

Endpoints Privyx answers itself

GET /health and GET /metrics are answered by Privyx in both modes and are never forwarded. Neither needs a key. See Metrics.

Timeouts and connection limits

Setting Default Meaning
provider.timeout 300 Seconds the upstream may take to send the next bytes of a response. A stream runs as long as data keeps coming.
provider.connect_timeout 10 Seconds to open a connection. A connection that cannot be opened is retried twice.
provider.max_connections unset Most connections open to the upstream at once. Unset means no limit.

What the client sees when one of them is hit is listed in Errors.

Reloading the configuration

While you tune a config file, --reload restarts the proxy whenever the file changes:

privyx -c my.yaml proxy --reload --upstream https://api.openai.com

It is a restart, not a hot swap: requests in flight get five seconds to finish, and an in-memory vault starts empty again. A file that fails to parse is reported, and the proxy waits for the next change instead of exiting. It is meant for development.

See also