Drip Billing Integration
Track usage and costs for AI agents, LLM calls, tool invocations, and any metered workload.
When to Use This Skill
- - Recording LLM usage quantities (for example total tokens per call)
- Tracking tool/function call costs
- Logging agent execution traces
- Metering API requests for billing
- Attributing costs to customers or workflows
Security & Data Privacy
Key scoping (least privilege):
- - Use
pk_ (public) keys for usage tracking, customer management, and billing. This is sufficient for all skill operations. - Only use
sk_ (secret) keys if you need admin operations: webhook management, API key rotation, or feature flags. - Public keys (
pk_) cannot manage webhooks, rotate API keys, or toggle feature flags — this limits blast radius if the key is compromised.
Metadata safety:
- - Include only minimal non-sensitive operational context in metadata.
- Never include PII, secrets, passwords, API keys, raw user prompts, model outputs, or full request/response bodies.
- Use a strict allowlist and redaction policy before telemetry writes.
- Prefer hashes/IDs (for example
queryHash) instead of raw user text.
What data is transmitted:
- - Usage quantities (meter name + numeric value)
- Customer identifiers
- Run lifecycle events (start/end, status, duration)
- Sanitized metadata you explicitly provide (model family, tool name, status code, latency, hashed IDs)
What is NOT transmitted:
- - Raw prompts, completions, or model outputs
- Environment variables or secrets
- File contents or source code
Installation
CODEBLOCK0
Environment Setup
CODEBLOCK1
Telemetry Safety Contract
- - Send only metadata needed for billing and diagnostics.
- Do not send raw prompts, raw model outputs, raw query text, full request/response bodies, or credentials.
- Prefer stable identifiers and hashes (for example
queryHash) over raw user content. - Emit telemetry only to a trusted
DRIP_BASE_URL.
Quick Start
1. Initialize the SDK
CODEBLOCK2
2. Track Usage (Simple)
CODEBLOCK3
3. Record Agent Runs (Complete Execution)
CODEBLOCK4
4. Streaming Execution (Real-Time)
CODEBLOCK5
Event Types
| Event Type | Description | Key Fields |
|---|
| INLINECODE6 | LLM API call | INLINECODE7 , quantity, INLINECODE9 |
| INLINECODE10 |
Tool invocation |
name,
duration,
status |
|
agent.plan | Planning step |
description |
|
agent.execute | Execution step |
description,
metadata |
|
error | Error occurred |
description,
metadata |
Common Patterns
Wrap Tool Calls
CODEBLOCK6
LangChain Auto-Tracking
CODEBLOCK7
API Reference
See references/API.md for complete SDK documentation.
按量计费集成
追踪AI智能体、大语言模型调用、工具调用以及任何计量工作负载的使用情况和成本。
何时使用此技能
- - 记录大语言模型使用量(例如每次调用的总令牌数)
- 追踪工具/函数调用成本
- 记录智能体执行轨迹
- 计量API请求以进行计费
- 将成本归因到客户或工作流
安全与数据隐私
密钥范围(最小权限):
- - 使用 pk(公钥) 进行使用追踪、客户管理和计费。这足以满足所有技能操作。
- 仅当需要管理操作时才使用 sk(密钥):Webhook管理、API密钥轮换或功能开关。
- 公钥(pk_)无法管理Webhook、轮换API密钥或切换功能开关——这限制了密钥泄露时的影响范围。
元数据安全:
- - 在元数据中仅包含最少量的非敏感操作上下文。
- 切勿包含个人身份信息、密钥、密码、API密钥、原始用户提示、模型输出或完整的请求/响应体。
- 在遥测写入之前使用严格的允许列表和编辑策略。
- 优先使用哈希值/ID(例如 queryHash)而非原始用户文本。
传输的数据:
- - 使用量(计量名称 + 数值)
- 客户标识符
- 运行生命周期事件(开始/结束、状态、持续时间)
- 您明确提供的经过清理的元数据(模型系列、工具名称、状态码、延迟、哈希ID)
不传输的数据:
- - 原始提示、补全或模型输出
- 环境变量或密钥
- 文件内容或源代码
安装
bash
npm install @drip-sdk/node
环境设置
bash
推荐:公钥——足以满足所有使用追踪和计费
export DRIP
APIKEY=pk
live...
仅当需要管理操作时才使用(Webhook、密钥管理、功能开关):
export DRIPAPIKEY=sklive...
遥测安全契约
- - 仅发送计费和诊断所需的元数据。
- 不发送原始提示、原始模型输出、原始查询文本、完整的请求/响应体或凭据。
- 优先使用稳定标识符和哈希值(例如 queryHash)而非原始用户内容。
- 仅向受信任的 DRIPBASEURL 发送遥测数据。
快速入门
1. 初始化SDK
typescript
import { Drip } from @drip-sdk/node;
// 自动从环境变量读取DRIPAPIKEY(推荐使用pklive...)
const drip = new Drip({
apiKey: process.env.DRIPAPIKEY
});
2. 追踪使用量(简单模式)
typescript
await drip.trackUsage({
customerId: customer_123,
meter: llm_tokens,
quantity: 1500,
// metadata为可选——仅包含操作上下文,绝不包含PII或密钥
metadata: { model: gpt-4 }
});
3. 记录智能体运行(完整执行)
typescript
await drip.recordRun({
customerId: cus_123,
workflow: research-agent,
events: [
{ eventType: llm.call, model: gpt-4, quantity: 1700, units: tokens },
{ eventType: tool.call, name: web-search, duration: 1500 },
{ eventType: llm.call, model: gpt-4, quantity: 1000, units: tokens },
],
status: COMPLETED,
});
4. 流式执行(实时)
typescript
// 开始运行
const run = await drip.startRun({
customerId: cus_123,
workflowSlug: document-processor,
});
// 实时记录每个步骤
await drip.emitEvent({
runId: run.id,
eventType: llm.call,
model: gpt-4,
quantity: 1700,
units: tokens,
});
await drip.emitEvent({
runId: run.id,
eventType: tool.call,
name: web-search,
duration: 1500,
});
// 完成运行
await drip.endRun(run.id, { status: COMPLETED });
事件类型
| 事件类型 | 描述 | 关键字段 |
|---|
| llm.call | 大语言模型API调用 | model, quantity, units |
| tool.call |
工具调用 | name, duration, status |
| agent.plan | 规划步骤 | description |
| agent.execute | 执行步骤 | description, metadata |
| error | 发生错误 | description, metadata |
常见模式
包装工具调用
typescript
async function trackedToolCall(runId: string, toolName: string, fn: () => Promise): Promise {
const start = Date.now();
try {
const result = await fn();
await drip.emitEvent({
runId,
eventType: tool.call,
name: toolName,
duration: Date.now() - start,
status: success,
});
return result;
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 未知错误;
await drip.emitEvent({
runId,
eventType: tool.call,
name: toolName,
duration: Date.now() - start,
status: error,
// 仅包含错误消息——绝不包含堆栈跟踪、环境变量或用户数据
metadata: { error: message },
});
throw error;
}
}
LangChain自动追踪
typescript
import { DripCallbackHandler } from @drip-sdk/node/langchain;
const handler = new DripCallbackHandler({
drip,
customerId: cus_123,
workflow: research-agent,
});
// 所有大语言模型调用和工具使用自动追踪
const result = await agent.invoke(
{ input: 研究最新的AI新闻 },
{ callbacks: [handler] }
);
API参考
完整的SDK文档请参见 references/API.md。