AI Proxy Documentation

Intelligent AI Request Router with Smart Monitoring

← Back to Home

Architecture

System architecture overview for GLM Proxy.

Related:

Provider Context: This proxy is designed for Z.ai (GLM Coding Plan). See Z.ai Knowledge Base for model specifications and API details.

Overview

GLM Proxy is a Node.js-based HTTP proxy that provides intelligent request routing, load balancing, and fault tolerance for API requests to the Z.AI GLM service.

Dashboard Architecture

The dashboard provides a visual interface to monitor all system components:

Dashboard Overview

Key UI Components

Page Navigation - Switch between Overview, Model Routing, Requests, and System views:

Page Navigation Tabs

Connection Status - Real-time upstream connectivity indicator:

Connection Status

Keys Heatmap - Visual representation of API key health:

Keys Heatmap

Request Rate Chart - Monitor throughput over time:

Request Rate Chart

System Components

┌────────────────────────────────────────────────────────────────────────┐
│                           GLM Proxy                                    │
├────────────────────────────────────────────────────────────────────────┤
│                                                                        │
│  ┌──────────────┐     ┌────────────────────────────────────────────┐  │
│  │   HTTP       │────>│  Request Handler                            │  │
│  │   Server     │     │  (retry, backoff, timeout, streaming)       │  │
│  └──────────────┘     └──────────┬─────────────────────────────────┘  │
│                                     │                                  │
│                                     v                                  │
│  ┌────────────────────────────────────────────────────────────────┐   │
│  │               Model Router (Complexity-Aware)                   │   │
│  │  - Light/Medium/Heavy tier classification                       │   │
│  │  - Per-key model overrides                                      │   │
│  │  - Cost tracking integration                                    │   │
│  └──────────────────────────┬─────────────────────────────────────┘   │
│                             │                                          │
│                             v                                          │
│  ┌────────────────────────────────────────────────────────────────┐   │
│  │            Key Manager & Key Scheduler                          │   │
│  │  - Health-weighted key selection (not just round-robin)        │   │
│  │  - Drift detection for fairness monitoring                      │   │
│  │  - Circuit state tracking                                       │   │
│  └──────────────────────────┬─────────────────────────────────────┘   │
│                             │                                          │
│                             v                                          │
│  ┌────────────────────────────────────────────────────────────────┐   │
│  │            Adaptive Concurrency (AIMD)                          │   │
│  │  - Per-model dynamic limits                                     │   │
│  │  - 429 feedback handling                                        │   │
│  │  - observe_only or enforce modes                                │   │
│  └──────────────────────────┬─────────────────────────────────────┘   │
│                             │                                          │
│                             v                                          │
│  ┌────────────────────────────────────────────────────────────────┐   │
│  │               Rate Limiter (Token Bucket)                       │   │
│  └──────────────────────────┬─────────────────────────────────────┘   │
│                             │                                          │
│                             v                                          │
│  ┌────────────────────────────────────────────────────────────────┐   │
│  │              Request Queue (Backpressure)                       │   │
│  └──────────────────────────┬─────────────────────────────────────┘   │
│                             │                                          │
└─────────────────────────────┼──────────────────────────────────────────┘
                              │
                              v
                ┌───────────────────────────┐
                │       Target API          │
                │       (api.z.ai)          │
                └───────────────────────────┘

Core Modules

Entry Point

Configuration

Logging

Key Management

Request Handling

Model Routing

Concurrency Control

Metrics

Dashboard

Data Flow

  1. Request Arrives - HTTP server receives request
  2. Model Routing - Complexity-based model classification and potential rerouting
  3. Queue Check - Request enters queue if all keys busy
  4. Rate Limit - Per-key rate limiter checks allowance
  5. Adaptive Concurrency - Per-model limit check (if in enforce mode)
  6. Key Selection - Health-weighted selection from available keys (not just round-robin)
  7. Circuit Check - Circuit breaker validates key state
  8. Proxy - Request forwarded to target API
  9. Retry - On failure, exponential backoff with jitter
  10. Response - Response returned to client
  11. Metrics - All events recorded via RingBuffer for efficiency
  12. Cost Tracking - Token usage recorded with pricing lookup

Circuit Breaker

                    ┌─────────┐
                    │ CLOSED │  (normal operation)
                    └────┬────┘
                         │ failures > threshold
                         v
                    ┌─────────┐
                    │  OPEN   │  (key disabled)
                    └────┬────┘
                         │ cooldown elapsed
                         v
                 ┌─────────────┐
                 │ HALF_OPEN   │  (testing recovery)
                 └──────┬──────┘
                        │
         ┌──────────────┴──────────────┐
         │                             │
    success                        failure
         │                             │
         v                             v
    ┌─────────┐                   ┌─────────┐
    │ CLOSED  │                   │  OPEN   │
    └─────────┘                   └─────────┘

Adaptive Concurrency (AIMD)

The AIMD controller dynamically adjusts per-model concurrency limits based on 429 feedback:

           ┌─────────────────────────────────┐
           │    Current Limit: N per model   │
           └───────────────┬─────────────────┘
                           │
              ┌────────────┴────────────┐
              │                         │
         429 Received              Success
              │                         │
              v                         v
     ┌─────────────────┐      ┌─────────────────┐
     │ Multiplicative  │      │  Additive       │
     │ Decrease        │      │  Increase       │
     │ N = N × 0.5     │      │  N = N + 1      │
     └─────────────────┘      └─────────────────┘
              │                         │
              v                         v
     ┌─────────────────┐      ┌─────────────────┐
     │ Start Cooldown  │      │  Respect Max    │
     │ (5s recovery)   │      │  Limit          │
     └─────────────────┘      └─────────────────┘

Modes:

Clustering

GLM Proxy supports multi-worker clustering for high throughput:

┌──────────────┐
│   Master     │
│   Process    │
└──────┬───────┘
       │
       ├─────> ┌─────────┐
       │       │ Worker 1 │
       │       └─────────┘
       │
       ├─────> ┌─────────┐
       │       │ Worker 2 │
       │       └─────────┘
       │
       ├─────> ┌─────────┐
       │       │ Worker 3 │
       │       └─────────┘
       │
       └─────> ┌─────────┐
               │ Worker 4 │
               └─────────┘

Configuration:

Event System

GLM Proxy emits events for monitoring and integration:

See events.md for detailed event documentation.

Testing

Test suite uses Jest:

npm test                    # Run tests
npm run test:verbose       # Run with coverage

See testing.md for test strategy and coverage information.

Security Considerations

See Security for more details.