P5 exit: usage metering, credit billing, promo codes, 3-step wizard
- LlmEvent::Usage across all three providers (Scripted deterministic word-count accounting; Anthropic message_start/delta usage; OpenAI-compat stream_options include_usage) - tc-billing: ceil(tokens/1000) min 1 credit; lots drain oldest-first under FOR UPDATE; balance clamps at zero while the usage ledger records the full obligation; promo codes redeem exactly once via CAS (migration 0006) - Runtime charges every completed run (billing failure never fails a run); proven: 1 token in + 3 out -> 1 credit deducted - API: GET /api/team/usage, POST /api/credits/redeem (409 on reuse, audited) - Credits page: balance, 7-day usage meter with runway estimate, PromoRedeem - /claws/new is the full §9 wizard: ?step=identity|access|slack deep-linked progress, accent swatches + name randomizer, access toggles, optional Slack step, explicit review-and-confirm (creation = live agent), animated provisioning state -> straight into chat - E2E: chat decrements the visible balance and fills the usage meter; WELCOME500 adds exactly 500 once then refuses; wizard round trip 140 Rust + 63 frontend tests + 23 Playwright journeys. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5
parent
91327e3618
commit
a8efada690
@@ -113,6 +113,7 @@ impl LlmProvider for AnthropicProvider {
|
||||
// tool_use input arrives as accumulated partial JSON between
|
||||
// content_block_start and content_block_stop.
|
||||
let mut pending_tool: Option<(String, String, String)> = None; // id, name, json
|
||||
let mut input_tokens: u32 = 0;
|
||||
while let Some(event) = sse.next().await {
|
||||
let event = event.map_err(|e| LlmError::Transport(e.to_string()))?;
|
||||
let data: Value = serde_json::from_str(&event.data)
|
||||
@@ -155,7 +156,17 @@ impl LlmProvider for AnthropicProvider {
|
||||
yield LlmEvent::ToolUse { id, name, input };
|
||||
}
|
||||
}
|
||||
"message_start" => {
|
||||
input_tokens =
|
||||
data["message"]["usage"]["input_tokens"].as_u64().unwrap_or(0) as u32;
|
||||
}
|
||||
"message_delta" => {
|
||||
if let Some(out) = data["usage"]["output_tokens"].as_u64() {
|
||||
yield LlmEvent::Usage {
|
||||
input_tokens,
|
||||
output_tokens: out as u32,
|
||||
};
|
||||
}
|
||||
if let Some(reason) = data["delta"]["stop_reason"].as_str() {
|
||||
yield LlmEvent::Stop(stop_reason(reason));
|
||||
}
|
||||
|
||||
@@ -97,6 +97,7 @@ impl LlmProvider for OpenAiCompatProvider {
|
||||
"max_tokens": request.max_tokens,
|
||||
"messages": OpenAiCompatProvider::wire_messages(&request),
|
||||
"stream": true,
|
||||
"stream_options": {"include_usage": true},
|
||||
});
|
||||
if !tools.is_empty() {
|
||||
body["tools"] = Value::Array(tools);
|
||||
@@ -159,6 +160,12 @@ impl LlmProvider for OpenAiCompatProvider {
|
||||
if let Some(reason) = choice["finish_reason"].as_str() {
|
||||
finish = Some(stop_reason(reason));
|
||||
}
|
||||
if let Some(usage) = data["usage"].as_object() {
|
||||
yield LlmEvent::Usage {
|
||||
input_tokens: usage["prompt_tokens"].as_u64().unwrap_or(0) as u32,
|
||||
output_tokens: usage["completion_tokens"].as_u64().unwrap_or(0) as u32,
|
||||
};
|
||||
}
|
||||
}
|
||||
for (id, name, args) in pending.drain(..) {
|
||||
if name.is_empty() {
|
||||
|
||||
@@ -83,6 +83,11 @@ pub enum LlmEvent {
|
||||
name: String,
|
||||
input: Value,
|
||||
},
|
||||
/// Token accounting for this provider call (drives credit metering).
|
||||
Usage {
|
||||
input_tokens: u32,
|
||||
output_tokens: u32,
|
||||
},
|
||||
Stop(StopReason),
|
||||
}
|
||||
|
||||
|
||||
@@ -163,6 +163,32 @@ impl LlmProvider for ScriptedProvider {
|
||||
}
|
||||
}
|
||||
|
||||
// Deterministic accounting: one "token" per whitespace word in and
|
||||
// out, so billing tests can predict exact charges.
|
||||
let input_tokens = request
|
||||
.messages
|
||||
.iter()
|
||||
.flat_map(|m| m.parts.iter())
|
||||
.filter_map(|p| match p {
|
||||
ContentPart::Text { text } => Some(text.split_whitespace().count()),
|
||||
_ => None,
|
||||
})
|
||||
.sum::<usize>() as u32;
|
||||
let output_tokens = events
|
||||
.iter()
|
||||
.filter_map(|e| match e {
|
||||
Ok(LlmEvent::TextDelta(t)) => Some(t.split_whitespace().count()),
|
||||
_ => None,
|
||||
})
|
||||
.sum::<usize>() as u32;
|
||||
let stop_index = events.len().saturating_sub(1);
|
||||
events.insert(
|
||||
stop_index,
|
||||
Ok(LlmEvent::Usage {
|
||||
input_tokens,
|
||||
output_tokens,
|
||||
}),
|
||||
);
|
||||
Ok(Box::pin(stream::iter(events)))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user