Resources · Security guides · Security guide
Put a local proxy and a daily cap in front of OpenAI and Anthropic
Point SDKs at the PortGate AI proxy on 127.0.0.1:8787 so known injections are blocked, secrets are redacted, and a daily request and token cap stops runaway spend.
Fix command
export OPENAI_BASE_URL=http://127.0.0.1:8787/v1
Copied.
An agent loop that retries forever, or an injected instruction that makes your app call the model thousands of times, shows up as a bill the next morning. Provider spend limits are monthly and slow to react. A local proxy enforces a daily cap on the same machine, before a request leaves it.
Install the proxy
The AI proxy needs no root. It installs into $HOME/.portgate and listens on 127.0.0.1:8787 only:
curl -fsSL https://YOUR-PORTGATE-HOST/downloads/install.sh | sh -s -- --token '<token>' --mode ai-proxy --url 'https://YOUR-PORTGATE-HOST'
ss -tlnp | grep 8787
Point your SDKs at it
The proxy guards /v1/chat/completions (OpenAI format) and /v1/messages (Anthropic format) and forwards to the ai_proxy.upstream you set in config.json.
# OpenAI SDKs (Python, Node) read this variable
export OPENAI_BASE_URL=http://127.0.0.1:8787/v1
# Anthropic SDKs append /v1/messages themselves
export ANTHROPIC_BASE_URL=http://127.0.0.1:8787
Restart the app so it picks up the variables, then send one request and check that it appears in the dashboard's AI telemetry.
Set the caps
{
"ai_proxy": {
"upstream": "https://api.openai.com",
"daily_request_cap": 1000,
"daily_token_cap": 100000,
"fallback_upstream": "http://127.0.0.1:11434",
"timeout_seconds": 30
}
}
When a cap is reached the proxy answers HTTP 429 and forwards nothing until the next day. Size the caps from a normal day's usage plus headroom, not from your monthly budget divided by 30. fallback_upstream is used when the main upstream times out or answers 429 or 503. A local Ollama on 127.0.0.1:11434 is a common fallback.
What it blocks, and what it does not
- Blocked: prompts that match known instruction-override patterns ("ignore previous instructions", attempts to reveal the system prompt). They get HTTP 400 and are not forwarded.
- Redacted: API keys, AWS keys and similar secrets found in the prompt are replaced before the upstream call.
- Not blocked: reworded or indirect injections that match no pattern. Pattern matching is a filter, not a guarantee. Keep secrets out of the model's context, give tools the least access they need, and treat model output as untrusted input.
Raw prompts and responses stay on the host. PortGate receives counts of requests, blocks, redactions and tokens.
Keep the model server private
The cap only counts traffic that goes through the proxy. If Ollama or another model server listens on 0.0.0.0, anything can call it directly:
ss -tlnp | grep -E '8787|11434'
Both should show 127.0.0.1. For Ollama, set OLLAMA_HOST=127.0.0.1 in its service environment and restart it.