---
title: "Deploy MarginSwitch — Self-Hosted LLM Cost & Loop Guard Proxy"
description: "Self-hosted Go LLM reverse proxy with Stripe margin & loop guard."
category: "AI/ML"
url: https://railway.com/deploy/marginswitch-self-hosted-llm-cost-and-lo
---

# Deploy MarginSwitch — Self-Hosted LLM Cost & Loop Guard Proxy

Self-hosted Go LLM reverse proxy with Stripe margin & loop guard.

**[Deploy MarginSwitch — Self-Hosted LLM Cost & Loop Guard Proxy on Railway](https://railway.com/template/marginswitch-self-hosted-llm-cost-and-lo)**

Machine-readable deploy manifest (JSON, validated by TemplateCI): https://railway.com/deploy/marginswitch-self-hosted-llm-cost-and-lo/manifest.json

- **Creator:** Yunia's Projects
- **Category:** AI/ML

## Template content

### Redis https://cdn.sanity.io/images/sy1jschh/production/0ce0bfdcfbdbf69662b1116671f97c2dd788b655-157x157.svg

- **Image:** redis:8.2
- **Start command:** `/bin/sh -c "rm -rf $RAILWAY_VOLUME_MOUNT_PATH/lost+found/ && exec docker-entrypoint.sh redis-server --requirepass $REDIS_PASSWORD --save 60 1 --dir $RAILWAY_VOLUME_MOUNT_PATH"`

### marginswitch

- **Source:** marginswitch/marginswitch
- **Health check:** /healthz
- **Public domain:** Yes

## Documentation

# Deploy and Host MarginSwitch — Self-Hosted LLM Cost & Loop Guard Proxy on Railway

MarginSwitch is a self-hosted, stateless Go reverse proxy built from scratch for flat-rate AI SaaS teams. Point `OPENAI_BASE_URL` or `ANTHROPIC_BASE_URL` at your own container and connect a read-only Stripe Restricted Key to link Stripe revenue directly to per-customer OpenAI, Anthropic, and Gemini token spend.

## About Hosting MarginSwitch — Self-Hosted LLM Cost & Loop Guard Proxy

This template deploys the MarginSwitch Go proxy alongside an official Redis instance for fast shared state. MarginSwitch tracks real-time per-customer LLM margin in a 56.9ns 64-shard ledger, blocks past-due or canceled zombie accounts with HTTP 402, auto-downgrades over-budget heavy users (Opus to Haiku, GPT-5 to GPT-5 Mini) without HTTP 400 errors, and terminates runaway tool-calling agent loops via a 0-alloc 64-bit SimHash breaker. It starts free in Shadow Mode (`MS_SHADOW_MODE=1`) so you can observe per-customer margins safely before enforcing limits, and supports offline Ed25519 license keys with zero phone-home.

## Common Use Cases

- Track real-time per-customer OpenAI, Anthropic, and Gemini token spend against Stripe subscription revenue
- Automatically downgrade over-budget heavy AI users to lighter models without breaking client requests
- Detect and break runaway tool-calling agent loops before they drain API credits, and block zombie/past-due accounts

## Dependencies for MarginSwitch — Self-Hosted LLM Cost & Loop Guard Proxy Hosting

- Redis (included in this template via `REDIS_URL`)
- Upstream LLM provider API keys/endpoints (OpenAI, Anthropic, Gemini) and an optional read-only Stripe Restricted Key

## Why Deploy MarginSwitch — Self-Hosted LLM Cost & Loop Guard Proxy on Railway?

Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying MarginSwitch — Self-Hosted LLM Cost & Loop Guard Proxy on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.

## Similar templates

- [Chat Chat](https://railway.com/deploy/-WWW5r) — Chat Chat, your own unified chat and search to AI platform.
- [Weaviate](https://railway.com/deploy/weaviate-1) — Vector database with hybrid search and an API key-protected API
- [Möbius Personal AI OS](https://railway.com/deploy/mobius-v01-personal-ai-os) — Self-hosted AI agent that builds and runs apps for you, with memory.

Open this page in a browser: https://railway.com/deploy/marginswitch-self-hosted-llm-cost-and-lo
