Files
taiji-AI-PAD/plans/当前计费模式分析报告.md
T
2026-01-08 15:03:52 +00:00

8.2 KiB
Raw Blame History

当前计费模式分析报告

版本: v1.0.0 创建时间: 2026-01-08 分析目标: 分析当前 Agent 计费模式的实现状态和存在的问题


1. 计费模式概述

1.1 计费表结构

系统中存在两个计费相关的表:

表名 用途 主要字段
billing_records 通用计费记录(按调用计费) tenant_id, agent_name, duration, eu, cost
agent_billing_records Agent 运行计费记录(按运行时长计费) user_id, agent_name, start_time, end_time, duration_seconds, eu_consumed, cost

1.2 计费模型定义

BillingRecord - 通用计费记录:

class BillingRecord(BaseModel, Base):
    __tablename__ = "billing_records"
    
    timestamp = Column(DateTime)
    channel_id = Column(GUID())
    tenant_id = Column(GUID())
    agent_id = Column(GUID())
    agent_name = Column(String(100))
    duration = Column(Integer)  # 秒
    eu = Column(Integer)  # 执行单元:1 EU = 10秒
    cost = Column(Numeric(12, 4))

AgentBillingRecord - Agent 运行计费记录:

class AgentBillingRecord(BaseModel, Base):
    __tablename__ = "agent_billing_records"
    
    user_id = Column(GUID())
    channel_id = Column(GUID())
    agent_name = Column(String(100))
    agent_type = Column(String(20))  # 模板名称
    is_platform_agent = Column(Boolean)
    start_time = Column(DateTime)  # Agent 启动时间
    end_time = Column(DateTime)  # Agent 停止时间
    duration_seconds = Column(Integer)
    eu_consumed = Column(Integer)
    cpu_used = Column(String)
    memory_used = Column(String)
    cost = Column(Numeric(12, 4))

2. 计费触发点分析

2.1 平台 Agent 计费

操作 触发位置 计费记录创建 状态
渠道分配平台 Agent 给租户 platform_agent_quota.py:404-579 ❌ 未创建 🔴 缺失
用户使用平台 Agent user.py:1282-1386 ✅ 创建 AgentBillingRecord ✅ 已实现
停止平台 Agent user.py:1388-1456 ✅ 更新 end_time ✅ 已实现

2.2 自定义 Agent 计费

操作 触发位置 计费记录创建 状态
创建自定义 Agent user.py:1539-1712 ✅ 创建 AgentBillingRecord ✅ 已实现
删除自定义 Agent user.py:1715-1797 ✅ 更新 end_time ✅ 已实现
扩缩容自定义 Agent user.py:1800-1924 ✅ 更新资源使用量 ✅ 已实现

2.3 计费记录创建代码示例

自定义 Agent 创建时的计费记录(user.py:1674-1684):

# 记录计费
billing_record = AgentBillingRecord(
    user_id=user_id,
    channel_id=channel_id,
    agent_type=req.template,
    agent_name=req.name,
    is_platform_agent=False,
    start_time=datetime.utcnow(),
    cpu_used=req.cpuRequest,
    memory_used=req.memoryRequest,
)
db.add(billing_record)

平台 Agent 使用时的计费记录(user.py:1351-1361):

# 记录计费
billing_record = AgentBillingRecord(
    user_id=user_id,
    channel_id=channel_id,
    agent_type=req.agentType,
    agent_name=instance_name,
    is_platform_agent=True,
    start_time=datetime.utcnow(),
    cpu_used=quota.cpu_per_pod or "100m",
    memory_used=quota.memory_per_pod or "128Mi",
)
db.add(billing_record)

3. Dashboard 数据查询分析

3.1 /api/v1/monitoring/dashboard 接口

接口位置: monitoring.py:65-132

查询逻辑(monitoring.py:541-552):

# 查询 billing_records 表
total_result = await session.execute(
    text("""
        SELECT
            COALESCE(SUM(eu), 0) as total_eu,
            COALESCE(SUM(cost), 0) as total_cost,
            COUNT(*) as total_calls
        FROM billing_records
        WHERE tenant_id = :tenant_id
        AND timestamp > NOW() - INTERVAL '24 hours'
    """),
    {"tenant_id": tenant_id}
)

问题: 查询的是 billing_records 表,但 Agent 运行计费写入的是 agent_billing_records 表。


4. EU 计算规则

4.1 基于时长的 EU 计算

计算公式(billing.py:50-60):

def calculate_eu(duration_seconds: int) -> int:
    """
    计算EU:1 EU = 10秒,不足10秒按1 EU计算
    """
    return math.ceil(duration_seconds / 10)

4.2 成本计算

按订阅等级定价(billing.py:18-26):

EU_PRICING = {
    "free": Decimal("0.015"),      # 入门级:$0.015 / EU
    "starter": Decimal("0.015"),   # 入门级别名
    "pro": Decimal("0.02"),        # 专业级:$0.02 / EU
    "enterprise": Decimal("0.03"), # 企业级:$0.03 / EU
}

4.3 平台 Agent 固定价格

按模板定价(billing.py:474-480):

PLATFORM_AGENT_PRICING = {
    "gpt-assistant": Decimal("0.10"),      # GPT 助手每小时 $0.10
    "code-reviewer": Decimal("0.15"),      # 代码审查每小时 $0.15
    "data-analyst": Decimal("0.12"),       # 数据分析每小时 $0.12
    "default": Decimal("0.10"),            # 默认每小时 $0.10
}

4.4 自定义 Agent 资源计费

按资源使用量计费(billing.py:468-472):

AGENT_RESOURCE_PRICING = {
    "cpu_per_hour": Decimal("0.05"),      # CPU 每核每小时 $0.05
    "memory_per_gb_hour": Decimal("0.01"), # 内存每 GB 每小时 $0.01
}

5. 计费流程图

5.1 平台 Agent 计费流程(设计)

sequenceDiagram
    participant CA as 渠道管理员
    participant MCP as mcp-server
    participant DB as 数据库
    participant AM as Agent Manager

    CA->>MCP: 分配平台 Agent 给租户
    MCP->>AM: 创建 Pod
    AM-->>MCP: Pod 创建成功
    MCP->>DB: 创建 AgentBillingRecord (start_time)
    Note over DB: 开始计费
    
    rect rgb(255, 200, 200)
        Note over MCP,DB: ⚠️ 当前实现缺失此步骤
    end
    
    MCP-->>CA: 分配成功
    
    Note over DB: Agent 运行中...
    
    CA->>MCP: 停止 Agent
    MCP->>AM: 删除 Pod
    MCP->>DB: 更新 AgentBillingRecord (end_time, duration, cost)
    Note over DB: 结束计费

5.2 自定义 Agent 计费流程(已实现)

sequenceDiagram
    participant T as 租户
    participant MCP as mcp-server
    participant DB as 数据库
    participant AM as Agent Manager

    T->>MCP: 创建自定义 Agent
    MCP->>MCP: 检查配额
    MCP->>AM: 创建 Pod
    AM-->>MCP: Pod 创建成功
    MCP->>DB: 创建 AgentBillingRecord (start_time)
    Note over DB: 开始计费 ✅
    MCP-->>T: 创建成功
    
    Note over DB: Agent 运行中...
    
    T->>MCP: 删除 Agent
    MCP->>AM: 删除 Pod
    MCP->>DB: 更新 AgentBillingRecord (end_time, duration, cost)
    Note over DB: 结束计费 ✅

6. 相关文件索引

文件 说明
services/mcp-server/models.py 数据模型定义(BillingRecord, AgentBillingRecord)
services/mcp-server/app/billing.py 计费逻辑(EU 计算、成本计算)
services/mcp-server/monitoring.py 监控模块(Dashboard 数据查询)
services/mcp-server/app/routes/monitoring.py 监控 API 路由
services/mcp-server/app/routes/user.py 用户 API(Agent 创建/删除计费)
services/mcp-server/app/routes/platform_agent_quota.py 平台 Agent 配额管理