quota-management

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Table of Contents

目录

Quota Management

配额管理

Overview

概述

Patterns for tracking and enforcing resource quotas across rate-limited services. This skill provides the infrastructure that other plugins use for consistent quota handling.
针对限流服务的资源配额跟踪与执行模式。该Skill为其他插件提供一致的配额处理基础设施。

When To Use

适用场景

  • Building integrations with rate-limited APIs
  • Need to track usage across sessions
  • Want graceful degradation when limits approached
  • Require cost estimation before operations
  • 构建与限流API的集成
  • 需要跨会话跟踪使用情况
  • 希望在接近限制时实现优雅降级
  • 操作前需要进行成本估算

When NOT To Use

不适用场景

  • Project doesn't use the leyline infrastructure patterns
  • Simple scripts without service architecture needs
  • 项目未采用leyline基础设施模式
  • 无服务架构需求的简单脚本

Core Concepts

核心概念

Quota Thresholds

配额阈值

Three-tier threshold system for proactive management:
LevelUsageAction
Healthy<80%Proceed normally
Warning80-95%Alert, consider batching
Critical>95%Defer non-urgent, use secondary services
用于主动管理的三级阈值系统:
级别使用情况操作
健康<80%正常执行
警告80-95%发出警报,考虑批量处理
严重>95%延迟非紧急任务,使用备用服务

Quota Types

配额类型

python
@dataclass
class QuotaConfig:
    requests_per_minute: int = 60
    requests_per_day: int = 1000
    tokens_per_minute: int = 100000
    tokens_per_day: int = 1000000
python
@dataclass
class QuotaConfig:
    requests_per_minute: int = 60
    requests_per_day: int = 1000
    tokens_per_minute: int = 100000
    tokens_per_day: int = 1000000

Quick Start

快速开始

Check Quota Status

检查配额状态

python
from leyline.quota_tracker import QuotaTracker

tracker = QuotaTracker(service="my-service")
status, warnings = tracker.get_quota_status()

if status == "CRITICAL":
    # Defer or use secondary service
    pass
python
from leyline.quota_tracker import QuotaTracker

tracker = QuotaTracker(service="my-service")
status, warnings = tracker.get_quota_status()

if status == "CRITICAL":
    # Defer or use secondary service
    pass

Record Usage

记录使用情况

python
tracker.record_request(
    tokens=estimated_tokens,
    success=True,
    duration=elapsed_seconds
)
python
tracker.record_request(
    tokens=estimated_tokens,
    success=True,
    duration=elapsed_seconds
)

Estimate Before Execution

执行前估算

python
can_proceed, issues = tracker.can_handle_task(estimated_tokens)
if not can_proceed:
    print(f"Quota issues: {issues}")
python
can_proceed, issues = tracker.can_handle_task(estimated_tokens)
if not can_proceed:
    print(f"Quota issues: {issues}")

Integration Pattern

集成模式

Other plugins reference this skill:
yaml
undefined
其他插件可引用该Skill:
yaml
undefined

In your skill's frontmatter

In your skill's frontmatter

dependencies: [leyline:quota-management]

Then use the shared patterns:
1. Initialize tracker for your service
2. Check quota before operations
3. Record usage after operations
4. Handle threshold warnings gracefully
dependencies: [leyline:quota-management]

然后使用共享模式:
1. 为你的服务初始化跟踪器
2. 操作前检查配额
3. 操作后记录使用情况
4. 妥善处理阈值警告

Detailed Resources

详细资源

  • Threshold Strategies: See
    modules/threshold-strategies.md
    for degradation patterns
  • Estimation Patterns: See
    modules/estimation-patterns.md
    for token/cost estimation
  • 阈值策略:查看
    modules/threshold-strategies.md
    了解降级模式
  • 估算模式:查看
    modules/estimation-patterns.md
    了解令牌/成本估算

Exit Criteria

退出标准

  • Quota status checked before operation
  • Usage recorded after operation
  • Threshold warnings handled appropriately
  • 操作前已检查配额状态
  • 操作后已记录使用情况
  • 已妥善处理阈值警告