BricksLLM Documentation

repository·main·Indexed 22 days ago

https://github.com/bricks-cloud/bricksllm

A cloud-native AI gateway written in Go for managing LLM production workloads. BricksLLM provides enterprise features including rate limiting, cost control, PII redaction, and failover for providers such as OpenAI, Anthropic, and Azure OpenAI. It supports granular access control via API keys and user IDs, allowing administrators to enforce spend limits, restrict allowed models, and define permitted API paths.

Tokens
9K
Snippets
25
Records
29
Agent score
79%

What's inside BricksLLM

  1. Overview of BricksLLM Architecture and Performance

    main

    BricksLLM is a high-performance LLM gateway built in Go, designed to provide authentication, rate-limiting, and cost tracking for LLM providers like OpenAI.

    Key Features

    • Granular Access Control: Rate-limit OpenAI spending via API keys to prevent cost overruns from stolen or misused keys.
    • Low Latency: Uses an event-driven architecture to minimize overhead. While rate-limiting checks are performed synchronously (approx. 30ms) to ensure security, event enrichment (token counting, latency, and cost calculation) and database ingestion are handled asynchronously via Go routines and an event bus to keep the gateway responsive.
    • High Scalability: Designed to handle high request rates (up to 1000+ req/s) without significant performance degradation, leveraging Go's concurrency model compared to Python-based alternatives.
  2. Configure a provider setting

    main

    Before using BricksLLM, you must configure a provider (e.g., openai) with its corresponding API key. Use the PUT method on the admin server's /api/provider-settings endpoint.

    Note: You must copy the id from the JSON response to use it when creating API keys.

    curl -X PUT http://localhost:8001/api/provider-settings \
       -H "Content-Type: application/json" \
       -d '{
              "provider":"openai",
              "setting": {
                 "apikey": "YOUR_OPENAI_KEY"
              }
          }'   
  3. Create a Bricks API key for vLLM

    main

    Once the provider setting is created, use the returned id as the settingIds in a request to /api/key-management/keys. This allows you to control access to the vLLM provider with specific rate limits and spending constraints.

    Key parameters:

    • settingIds: An array containing the id from the provider setting creation step.
    • rateLimitOverTime: The number of requests allowed.
    • rateLimitUnit: The time unit (e.g., m for minutes).
    • costLimitInUsd: The maximum spend allowed in USD.
    curl -X PUT http://localhost:8001/api/key-management/keys \
       -H "Content-Type: application/json" \
       -d '{
    	      "name": "My vLLM Key",
    	      "key": "my-vllm-key",
    	      "tags": ["mykey"],
            "settingIds": ["ID_FROM_STEP_ONE"],
            "rateLimitOverTime": 2,
            "rateLimitUnit": "m",
            "costLimitInUsd": 0.25
          }'   
  4. Implement Access Control Based On User ID

    main

    You can enforce usage limits, rate limits, and model access for specific users by leveraging the userId field in your API requests. This is useful for internal applications (access based on email) or SaaS applications (tier-based limits).

    To implement this, follow these steps:

    1. Create a provider setting: Register your LLM provider (e.g., openai) via /api/provider-settings and capture the returned id.
    2. Create a Bricks API key: Create a key via /api/key-management/keys, associating it with the provider setting id using the settingIds array.
    3. Create a User: Define a user via /api/users. You must provide a unique userId and matching tags that correspond to the API key created in step 2. In this step, you can configure:
      • costLimitInUsd: Total spend limit.
      • costLimitInUsdOverTime: Spend limit over a specific time unit.
      • costLimitInUsdUnit: Time unit for spend limits (e.g., m for minute).
      • rateLimitOverTime: Request rate limit.
      • rateLimitUnit: Time unit for rate limits (e.g., m for minute).
      • allowedPaths: An array of objects specifying allowed API path and method.
      • allowedModels: An array of model strings (e.g., ["gpt-4"]).
      • userId: Your custom identifier for the user.
      • tags: Must match the tags on the API key to link the user to that key.
    4. Make Requests: When calling the Bricks gateway, include the Bricks API key in the Authorization header and the userId in the JSON request body.

    If a user attempts to access a model or path not defined in their allowedModels or allowedPaths, the request will return a 401 error.

    ### Step 1 - Create a provider
    ```bash
    curl -X PUT http://localhost:8001/api/provider-settings \
       -H "Content-Type: application/json" \
       -d '{
              "provider":"openai",
              "setting": {
                 "apikey": "YOUR_OPENAI_API_KEY"
              }
          }'   

    Step 2 - Create a Bricks API key

    curl -X PUT http://localhost:8001/api/key-management/keys \
       -H "Content-Type: application/json" \
       -d '{
    	      "name": "My Secret Key",
    	      "key": "my-secret-key",
    	      "tags": ["team-one"],
    	      "settingIds": ["ID_FROM_STEP_ONE"]
          }'   

    Step 3 - Create a User

    curl -X POST http://localhost:8001/api/users \
       -H "Content-Type: application/json" \
       -d '{
                "name": "Spike Lu",
                "costLimitInUsd": 1,
                "costLimitInUsdOverTime": 0.002,
                "costLimitInUsdUnit": "m",
                "rateLimitOverTime": 5,
                "rateLimitUnit": "m",
                "allowedPaths": [
                    {
                    "path": "/api/providers/openai/v1/chat/completions",
                    "method": "POST"
                    }
                ],
                "allowedModels": ["gpt-4"],
                "userId": "my-user-id",
                "tags": ["team-one"]
          }'   

    Usage Example

    curl -X POST http://localhost:8002/api/providers/openai/v1/chat/completions \
       -H "Authorization: Bearer my-secret-key" \
       -H "Content-Type: application/json" \
       -d '{
              "model": "gpt-4",
              "messages": [
                  {
                      "role": "system",
                      "content": "hi"
                  }
              ],
              "user": "my-user-id"
        }'
  5. Create a Bricks API key with limits

    main

    Create a managed API key by sending a PUT request to /api/key-management/keys. This allows you to enforce rate limits and cost limits on specific users or applications.

    Required fields:

    • name: A descriptive name for the key.
    • key: The actual string used for Bearer authentication.
    • settingIds: An array containing the id obtained from the provider setting configuration.
    • rateLimitOverTime: The number of requests allowed.
    • rateLimitUnit: The time unit for the rate limit (e.g., m for minutes).
    • costLimitInUsd: The maximum spend allowed in USD.
    curl -X PUT http://localhost:8001/api/key-management/keys \
       -H "Content-Type: application/json" \
       -d '{
    	      "name": "My Secret Key",
    	      "key": "my-secret-key",
    	      "tags": ["mykey"],
            "settingIds": ["ID_FROM_STEP_FOUR"],
            "rateLimitOverTime": 2,
            "rateLimitUnit": "m",
            "costLimitInUsd": 0.25
          }'   
  6. Configure AWS credentials for PII detection

    main

    BricksLLM uses AWS Comprehend for PII (Personally Identifiable Information) detection. You must provide AWS credentials via environment variables before starting BricksLLM.

    If running via Docker, include these in your environment configuration:

    • AWS_SECRET_ACCESS_KEY
    • AWS_ACCESS_KEY_ID
    • AMAZON_REGION
        environment:
          - AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
          - AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
          - AMAZON_REGION=us-west-2
  7. Configure a vLLM provider setting

    main

    To integrate vLLM with BricksLLM, you must first create a provider setting via the /api/provider-settings endpoint. This defines the connection details for your vLLM deployment.

    Important: The url value must not end with a trailing slash /.

    curl -X PUT http://localhost:8001/api/provider-settings \
       -H "Content-Type: application/json" \
       -d '{
              "provider":"vllm",
              "setting": {
                 "url": "YOUR_VLLM_DEPLOYMENT_URL",
                 "apikey": "YOUR_VLLM_API_KEY"
              }
          }'   
  8. Create a Bricks API key with multiple provider settings

    main

    Create a new API key using the /api/key-management/keys endpoint. To allow this key to access specific providers, include their respective provider setting IDs in the settingIds array. The order of IDs in settingIds determines the priority/availability for the key.

    curl -X PUT http://localhost:8001/api/key-management/keys \
       -H "Content-Type: application/json" \
       -d '{
    	      "name": "My Secret Key",
    	      "key": "my-secret-key",
              "settingIds": ["ID_FROM_STEP_TWO", "ID_FROM_STEP_ONE"]
          }'   
  9. Create an Azure OpenAI provider setting

    main

    Register an Azure OpenAI provider by sending a PUT request to the /api/provider-settings endpoint. The setting object requires resourceName and apikey. Note the id returned in the response.

    curl -X PUT http://localhost:8001/api/provider-settings \
       -H "Content-Type: application/json" \
       -d '{
              "provider":"azure",
              "setting": {
                    "resourceName": "YOUR_AZURE_RESOURCE_NAME",
                    "apikey": "YOUR_AZURE_API_KEY"
              }
          }'   
  10. Configure Granular Access Control for API Keys

    main

    You can restrict BricksLLM API keys to specific models, endpoints (paths), and rate/spend limits. This is achieved in two steps: first, creating a provider setting that defines allowed models, and second, creating a Bricks API key that references that setting and defines specific path/method restrictions and usage limits.

    ### Step 1 - Create a provider
    curl -X PUT http://localhost:8001/api/provider-settings \
       -H "Content-Type: application/json" \
       -d '{
              "provider":"openai",
              "setting": {
                 "apikey": "YOUR_OPENAI_KEY"
              },
              "allowedModels": ["gpt-3.5-turbo"]
          }'   
    
    ### Step 2 - Create a Bricks API key
    curl -X PUT http://localhost:8001/api/key-management/keys \
       -H "Content-Type: application/json" \
       -d '{
    	      "name": "My Secret Key",
    	      "key": "my-secret-key",
    	      "tags": ["mykey"],
            "settingIds": ["ID_FROM_STEP_ONE"],
            "rateLimitOverTime": 2,
            "rateLimitUnit": "m",
            "costLimitInUsd": 0.25,
            "allowedPaths": [{
                "path": "/api/providers/openai/v1/chat/completions",
                "method": "POST"
            }]
          }'