Run This Ai
EN DE

BricksLLM Tutorial: Set Up LLM API Cost Control in 10 Minutes

Step-by-step guide to deploy BricksLLM with Docker Compose, PostgreSQL, and Redis. Real commands, real mistakes, real fixes.

BricksLLM Logo

How to Set Up BricksLLM in 10 Minutes β€” a Real Walkthrough

I'm going to show you exactly how I set up BricksLLM to control my team's OpenAI spending. This isn't a theoretical guide β€” these are the actual commands I ran, the mistakes I made, and how I fixed them.

What you'll need: Docker, a PostgreSQL database, and Redis. If you don't have Redis running, stop now and spin one up β€” BricksLLM won't rate-limit without it (I learned this the hard way).

πŸš€ Want to deploy BricksLLM yourself?

Docker configs, system requirements, and installation guides β€” all on one page.

View BricksLLM Tool Page β†’

Step 1: Start the Infrastructure

BricksLLM needs PostgreSQL and Redis. I use Docker Compose for everything because it's simpler to reproduce. Create a docker-compose.yml:

version: '3.8'
services:
  postgres:
    image: postgres:15
    environment:
      POSTGRES_USER: bricksllm
      POSTGRES_PASSWORD: your_password_here
      POSTGRES_DB: bricksllm
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

  bricksllm:
    image: luyuanxin1995/bricksllm:latest
    ports:
      - "8080:8080"
    environment:
      DB_HOST: postgres
      DB_PORT: 5432
      DB_USER: bricksllm
      DB_PASSWORD: your_password_here
      DB_DATABASE: bricksllm
      REDIS_ADDRESS: redis:6379
    depends_on:
      - postgres
      - redis

volumes:
  pgdata:

Time estimate: One minute to copy-paste, 30 seconds for Docker to pull the images.

⚠️ What I messed up: I used DB_HOST: localhost at first. Don't do that β€” Docker containers can't reach each other via localhost. Use the service name (postgres) as the host. Lost 20 minutes on this.

Step 2: Start Everything

docker-compose up -d
docker-compose logs bricksllm

If you see something like "Starting server on :8080" β€” congrats, it's running. If not, check the logs. The most common issue is the database connection failing. Verify with:

docker-compose logs bricksllm | grep -i error

Step 3: Create Your First API Key

This is where the magic happens. BricksLLM exposes its management API on port 8080. Let's create an admin key first, then a restricted key for a team member:

# Create an admin API key
curl -X POST http://localhost:8080/api/admin/keys \
  -H "Content-Type: application/json" \
  -d '{
    "name": "My Admin Key",
    "key": "sk-bricks-admin-123"
  }'

# Create a restricted key with a $50 monthly limit
curl -X POST http://localhost:8080/api/admin/keys \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Dev Team Key",
    "key": "sk-bricks-dev-456",
    "cost_limit_usd": 50.00,
    "cost_limit_period": "monthly",
    "rate_limit_rpm": 100,
    "rate_limit_tpm": 100000
  }'

πŸ’‘ Pro tip: The sk-bricks- prefix in the key isn't required β€” you can use any string. But using a prefix makes logs easier to grep later.

Step 4: Route Your Traffic Through BricksLLM

Now, instead of your team calling https://api.openai.com/v1/chat/completions directly, they call http://your-server:8080/v1/chat/completions with their assigned API key. BricksLLM transparently proxies the request to OpenAI (or whatever provider you configured).

# Before (direct OpenAI β€” no cost control)
curl https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{"model": "gpt-4", "messages": [{"role": "user", "content": "Hello"}]}'

# After (through BricksLLM β€” cost controlled)
curl http://localhost:8080/v1/chat/completions \
  -H "Authorization: Bearer sk-bricks-dev-456" \
  -d '{"model": "gpt-4", "messages": [{"role": "user", "content": "Hello"}]}'

The beauty is that your application code barely changes β€” just swap the endpoint URL and the API key. Everything else stays the same.

Step 5: Monitor Usage

The admin dashboard is available at http://localhost:8080/dashboard. You'll see real-time metrics on requests, token usage, and costs per key. I check this once a day β€” it takes 10 seconds to see if anything looks unusual.

Final Check: Did I Miss Anything?

If everything is working, you'll get a 200 response from your proxied requests. If you get a 429 (rate limited) β€” that means your limits are working. If you get a 403 (forbidden) β€” the API key has hit its cost limit. That's the feature in action, not a bug.

If you see connection refused, go back to Step 1 and check the logs. 90% of issues are either PostgreSQL connection problems or forgetting Redis.

πŸš€ Explore BricksLLM on Run This Ai

Docker Compose configs, system requirements, installation guides, and more β€” all in one place.

View BricksLLM Tool Page β†’
#llm-gateway #docker #tutorial #api-gateway #openai