@dotlab-hq/llm-proxy

中文文档
1.0.12发布于 2026/05/04MITTypeScriptES Module构建来源已验证近 30 天下载 5182

LLM Proxy server with OpenAI compatibility

原始使用文档
文档目录31 节
  1. Quick Start
  2. 1. Initialize Configuration
  3. 2. Configure Your Models
  4. 3. Start the Server
  5. CLI Commands
  6. init
  7. serve
  8. Options
  9. Configuration Format
  10. JSONC (JSON with Comments)
  11. Dynamic Schema References
  12. Models Configuration
  13. API Endpoints
  14. Get Cache Status
  15. Get Statistics
  16. OpenAI Compatible Endpoints
  17. Development
  18. Run in Development Mode
  19. Type Checking
  20. Running Tests
  21. Link Package Locally
  22. Build CLI for Distribution
  23. Features
  24. Error Handling
  25. CLI Architecture
  26. Production Deployment
  27. 1. Build the CLI
  28. 2. Use in Projects
  29. Environment Variables
  30. Architecture
  31. License

@dotlab-hq/llm-proxy

A local LLM API proxy server with rate limiting, caching, and multi-backend support. Works as an OpenAI-compatible API endpoint.

Quick Start

1. Initialize Configuration

Create a new model.jsonc configuration file:

npx @dotlab-hq/llm-proxy init

Or skip prompts for automation:

npx @dotlab-hq/llm-proxy init --skip-prompts

2. Configure Your Models

Edit the generated model.jsonc and add your LLM providers:

{
  "$schema": "https://raw.githubusercontent.com/dotlab-hq/llm-proxy/refs/heads/main/schema.json",
  "state-adapter": "memory",
  "models": {
    "openai": [
      {
        "id": "primary-openai",
        "name": "Primary Instance",
        "models": ["gpt-3.5-turbo", "gpt-4"],
        "individualLimit": true,
        "baseUrl": "https://api.openai.com/v1",
        "apiKey": "sk-your-api-key-here",
        "rateLimit": {
          "tokensPerMinute": 90000,
          "requestsPerMinute": 3500,
          "requestsPerDay": 200000
        }
      }
    ]
  }
}

3. Start the Server

npx @dotlab-hq/llm-proxy serve

The server starts on port 25789 by default. If busy, it auto-selects the next available port.

With --skip-prompts, the server starts immediately without prompts.

CLI Commands

init

Initialize a new model.jsonc configuration:

npx @dotlab-hq/llm-proxy init
npx @dotlab-hq/llm-proxy init --skip-prompts

serve

Start the LLM Proxy server:

npx @dotlab-hq/llm-proxy serve
npx @dotlab-hq/llm-proxy serve --skip-prompts

What it does:

  • Loads configuration from model.jsonc
  • Starts on port 25789 (auto-selects next available if busy)
  • Shows server configuration details
  • Press Ctrl+C to stop

Options

  • --skip-prompts - Skip all interactive prompts and use defaults

Configuration Format

JSONC (JSON with Comments)

The configuration uses JSONC format for better user experience:

  • ✅ Comments preserved for documentation
  • ✅ Inline field explanations
  • ✅ Example values shown
  • ✅ Optional fields documented

Dynamic Schema References

The generated schema reference always points to the latest version:

"$schema": "https://raw.githubusercontent.com/dotlab-hq/llm-proxy/refs/heads/main/schema.json"

When installed as an NPM package and linked locally, it references:

"$schema": "./node_modules/@dotlab-hq/llm-proxy/schema.json"

Models Configuration

Each backend configuration includes:

{
  // Unique identifier for this backend
  "id": "primary-openai",
  // Display name
  "name": "Primary Instance",
  // Models this backend supports
  "models": ["gpt-3.5-turbo", "gpt-4"],
  // Track rate limits per instance
  "individualLimit": true,
  // API endpoint (OpenAI-compatible)
  "baseUrl": "https://api.openai.com/v1",
  // API authentication key
  "apiKey": "sk-your-api-key-here",
  // Rate limiting per backend
  "rateLimit": {
    "tokensPerMinute": 90000,
    "requestsPerMinute": 3500,
    "requestsPerDay": 200000
  }
}

API Endpoints

Get Cache Status

curl http://localhost:25789/

Get Statistics

curl http://localhost:25789/stats

Auto-loaded on server startup with current rate limit usage.

OpenAI Compatible Endpoints

  • POST /v1/chat/completions - Chat completions
  • POST /v1/completions - Text completions
  • POST /v1/embeddings - Embeddings
  • GET /v1/models - List available models

Example:

curl -X POST http://localhost:25789/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer llm-proxy" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Development

Run in Development Mode

bun run dev

Type Checking

bun run types

Running Tests

bun run testify
bun link

Register locally for use in other projects:

bun link @dotlab-hq/llm-proxy

Build CLI for Distribution

bun run build

Creates dist/cli.js (standalone CLI executable).

Features

✅ OpenAI Compatible - Drop-in replacement for OpenAI API
✅ Multi-Backend - Load balance across multiple providers
✅ Rate Limiting - Granular token/request limits
✅ Caching - Memory or Redis-backed state
✅ Auto-Load Stats - Statistics loaded on server startup
✅ Modular CLI - Separated commands and utilities
✅ Dynamic Templates - Always uses latest schema from GitHub
✅ JSONC Configuration - Comments for better documentation
✅ Bun Linked - Local package development support
✅ TypeScript - Full type safety

Error Handling

All errors are returned in OpenAI-compatible format:

  • 400 - Invalid request (missing model, invalid parameters)
  • 429 - Rate limit exceeded (tries next backend)
  • 502 - All backends failed
  • 503 - No backend configured

Stack traces are suppressed, only user-friendly messages shown.

CLI Architecture

For detailed information about the CLI structure and development, see CLI_ARCHITECTURE.md.

Production Deployment

1. Build the CLI

bun run build

2. Use in Projects

npx @dotlab-hq/llm-proxy init
npx @dotlab-hq/llm-proxy serve

Environment Variables

Use ${VAR_NAME} in your model.jsonc to reference environment variables:

{
  "models": {
    "openai": [
      {
        "apiKey": "${OPENAI_API_KEY}",
        "baseUrl": "https://api.openai.com/v1",
        "models": ["gpt-3.5-turbo"]
      }
    ]
  }
}

Architecture

  • Hono - Fast, lightweight HTTP server
  • Zod - Type-safe configuration validation
  • @clack/prompts - Beautiful interactive CLI
  • Bun - Fast JavaScript runtime
  • Multi-backend - Load balancing across providers
  • Rate Limiting - Granular usage tracking
  • Caching - Pluggable adapters (memory/Redis)

License

MIT


Created with ❤️ by dotlab HQ

版本与发布记录

@dotlab-hq/llm-proxy 共发布 13 个版本,首次发布于 2026/05/02;过去 12 个月发布了 13 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.12latest2026/05/046 个月前
1.0.112026/05/046 个月前
1.0.102026/05/046 个月前
1.0.92026/05/046 个月前
1.0.82026/05/046 个月前
1.0.72026/05/046 个月前

关于 @dotlab-hq/llm-proxy 的常见问题

@dotlab-hq/llm-proxy 是什么?

LLM Proxy server with OpenAI compatibility

如何安装 @dotlab-hq/llm-proxy?

在项目目录运行 npm install @dotlab-hq/llm-proxy,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^5)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 llm-proxy,可以用 npx llm-proxy 直接运行。

@dotlab-hq/llm-proxy 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dotlab-hq/llm-proxy 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dotlab-hq/llm-proxy 可以商用吗?

@dotlab-hq/llm-proxy 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dotlab-hq/llm-proxy 的下载量有多少?

根据 npm 官方下载统计,@dotlab-hq/llm-proxy 在 2026/10/13 – 2026/11/11 这 30 天内被下载 5,182 次,日均约 173 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dotlab-hq/llm-proxy 还在维护吗?

最新版本 1.0.12 发布于 2026/05/04(6 个月前);至今共发布 13 个版本,其中过去 12 个月发布了 13 个;首次发布于 2026/05/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。