Agentic定价策略:usage-based计费模式的实现

【免费下载链接】chatgpt-api Node.js client for the official ChatGPT API. 🔥 【免费下载链接】chatgpt-api 项目地址: https://gitcode.com/gh_mirrors/ch/chatgpt-api

在当今云服务和API经济快速发展的时代,usage-based计费模式已成为许多SaaS产品的首选定价策略。GitHub加速计划/chatgpt-api项目作为Node.js客户端,为官方ChatGPT API提供了强大支持,其Agentic平台通过灵活的usage-based计费模式,帮助开发者实现精准的服务定价和成本控制。

什么是usage-based计费模式?

usage-based计费模式,也称为按使用量计费,是一种根据用户实际使用的服务资源或功能来计算费用的定价方式。与传统的固定订阅模式相比,它具有以下优势:

  • 公平性:用户只需为实际使用的资源付费
  • 灵活性:支持从小规模试用平滑扩展到大规模应用
  • 成本优化:避免资源浪费,降低用户入门门槛

在Agentic平台中,usage-based计费模式通过metered类型的计费项目实现,与传统的licensed(许可式)计费形成互补。

Agentic MCP网关架构图

图:Agentic MCP网关架构展示了usage-based计费在整个系统中的位置,通过网关实现用量计量和计费

Agentic中的两种计费类型

Agentic平台支持两种主要计费类型,在packages/types/src/pricing.ts中定义:

1. Licensed(许可式)计费

Licensed计费适合固定成本的服务,如基础订阅:

// 示例:licensed类型计费项目定义
{
  slug: 'base',
  usageType: 'licensed',
  name: '基础订阅',
  price: {
    currency: 'usd',
    amount: 2000 // $20.00
  }
}

2. Metered(计量式)计费

Metered计费即usage-based模式,根据实际使用量计费:

// 示例:metered类型计费项目定义
{
  slug: 'requests',
  usageType: 'metered',
  name: 'API请求',
  price: {
    currency: 'usd',
    amount: 10 // $0.01 per request
  },
  unitLabel: '请求'
}

usage-based计费的技术实现

Agentic平台通过以下组件实现usage-based计费:

1. 用量计量系统

apps/api/src/lib/billing/upsert-stripe-pricing-resources.ts中,系统会为每个计量式计费项目创建对应的Stripe Meter资源:

// 创建Stripe Meter用于计量使用量
if (pricingPlanLineItem.usageType === 'metered') {
  const stripeMeter = await stripe.meters.create({
    display_name: `${project.identifier} ${pricingPlanLineItem.label}`,
    event_name: `meter-${project.id}-${pricingPlanLineItemSlug}`,
    aggregation: {
      formula: pricingPlanLineItem.defaultAggregation?.formula ?? 'sum'
    }
  });
}

2. 计费模型配置

packages/types/src/pricing.ts中定义了完整的计费模型,支持多种计价方式:

  • 按单位计费:根据使用的单位数量直接计费
  • 阶梯式计费:不同使用量区间采用不同费率
  • 自定义聚合:支持sum、max、average等多种用量聚合方式

3. 结算流程

结算流程在apps/api/src/lib/billing/create-stripe-checkout-session.ts中实现,系统会根据计量数据自动生成账单:

// 处理计量式计费项目
pricingPlan.lineItems.map(async (lineItem) => {
  if (lineItem.usageType === 'metered') {
    return {
      price: priceId,
      quantity: lineItem.usageType === 'licensed' ? 1 : undefined,
      metered: true
    };
  }
});

如何在项目中应用usage-based计费

要在Agentic项目中实现usage-based计费,只需在项目配置中定义相应的计费项目:

// agentic.config.ts 中配置usage-based计费
export default defineConfig({
  pricing: {
    plans: [
      {
        slug: 'pay-as-you-go',
        name: '按使用付费',
        lineItems: [
          {
            slug: 'requests',
            usageType: 'metered',
            name: 'API请求',
            price: {
              currency: 'usd',
              amount: 5 // $0.005 per request
            },
            unitLabel: '请求'
          }
        ]
      }
    ]
  }
});

API使用示例

图:使用Agentic SDK调用API的示例,每次调用都会被计量并计入usage-based计费

结语

usage-based计费模式为API服务提供了灵活、公平的定价方案,特别适合资源消耗变化较大的应用场景。通过Agentic平台的实现,开发者可以轻松配置和管理复杂的计费模型,同时为用户提供透明、可预测的付费体验。

要开始使用Agentic的usage-based计费功能,只需clone项目仓库:

git clone https://gitcode.com/gh_mirrors/ch/chatgpt-api

详细的计费配置指南可参考项目中的docs/publishing/config/pricing.mdx文档。

【免费下载链接】chatgpt-api Node.js client for the official ChatGPT API. 🔥 【免费下载链接】chatgpt-api 项目地址: https://gitcode.com/gh_mirrors/ch/chatgpt-api

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐