Skip to main content

PhiGateway

Self-hosted AI gateway. LLM proxy, tool registry, RAG knowledge base, and agent memory behind one OpenAI-compatible endpoint.

CI Python 3.12+ License: MIT

Install

pip install phi-gateway

Quick Start

# Start the gateway
uvicorn phi_gateway.main:app

# Create an API key
curl -sX POST http://localhost:8000/v1/keys \
  -H "Content-Type: application/json" \
  -d '{"name":"my-agent","tier":"free"}'

# Chat through the gateway
curl -s http://localhost:8000/v1/chat/completions \
  -H "Authorization: Bearer phi-sk-..." \
  -H "Content-Type: application/json" \
  -d '{"model":"groq/llama-3.3-70b","messages":[{"role":"user","content":"Hello"}]}'

With Docker:

git clone https://github.com/raindragon14/phi-gateway
cd phi-gateway
cp .env.example .env    # add your provider keys
docker compose up -d

What It Does

LLM Proxy — Route to OpenAI, Anthropic, Groq, or OpenRouter. Switch providers or use fallback chains without changing agent code. Streaming, cost tracking, and logging included.

Tool Registry — Register tools with JSON Schema. Agents discover and call them via REST or MCP (JSON-RPC 2.0). MCP-native.

Knowledge Base — Chunk, embed, and search documents. Cosine similarity with keyword fallback. Everything in SQLite. No external vector database.

Agent Memory — Store conversations, paginate history, auto-trim context. Returns X-Context-Truncated header when messages are trimmed.

Usage

from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="phi-sk-...")
response = client.chat.completions.create(
    model="groq/llama-3.3-70b",
    messages=[{"role": "user", "content": "Hello"}],
)

Full API reference at /docs when the server is running.

Architecture

Caddy (reverse proxy, auto TLS)
  └── FastAPI (uvicorn)
        ├── /v1/chat/completions  →  LLM proxy  →  provider APIs
        ├── /v1/tools             →  tool registry
        ├── /v1/kb                →  RAG (SQLite + cosine similarity)
        ├── /v1/memory            →  agent memory
        ├── /mcp                  →  JSON-RPC 2.0 (MCP)
        └── /dashboard            →  HTMX admin UI
              └── SQLite (single file)

Idle RAM: ~250 MB. Python 3.12+. MIT license.

Development

pip install -e ".[dev]"
pytest -v
ruff check src/ tests/

Code style: Google docstrings, ruff format, pytest. See pyproject.toml.

License

MIT

Metadata

Release files for phi-gateway 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for phi-gateway 0.4.1
File Size Uploaded Provenance Actions
phi_gateway-0.4.1.tar.gz 64.3 kB Without provenance Details for phi_gateway-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for phi-gateway 0.4.1
File Interpreter ABI Platform Actions
phi_gateway-0.4.1-py3-none-any.whl Python 3 none any Details for phi_gateway-0.4.1-py3-none-any.whl

Total release size: 151.1 kB

Release files / phi_gateway-0.4.1.tar.gz

Download URL phi_gateway-0.4.1.tar.gz
Size 64.3 kB
Tags Source
SHA-256 checksum
How to use checksums
291a542f523cae019ee8cd3230654465d552d7ac0d03fbf5c386e7ed79066d7b
BLAKE2b-256 checksum
How to use checksums
4a7397d998ef2861e4d6f740ba39939520db3f1953258e82f980a5a2f48edcd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / phi_gateway-0.4.1-py3-none-any.whl

Download URL phi_gateway-0.4.1-py3-none-any.whl
Size 86.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e27af56abb3f195de19a1d1ebfee938cdb3d605592c86c1b8924d2031f2ab7bf
BLAKE2b-256 checksum
How to use checksums
38877acb2d76e677f2f7d5b0bb36ca498fd0dd2e460036fc4783d8bf3f26968e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Security for phi-gateway 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page