mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-22 06:00:26 +00:00
Apply service tier pricing (#675)
This commit is contained in:
parent
7b2c627079
commit
4f216d318f
1 changed files with 24 additions and 0 deletions
|
|
@ -24,6 +24,7 @@ import type {
|
||||||
ThinkingContent,
|
ThinkingContent,
|
||||||
Tool,
|
Tool,
|
||||||
ToolCall,
|
ToolCall,
|
||||||
|
Usage,
|
||||||
} from "../types.js";
|
} from "../types.js";
|
||||||
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
||||||
import { parseStreamingJson } from "../utils/json-parse.js";
|
import { parseStreamingJson } from "../utils/json-parse.js";
|
||||||
|
|
@ -277,6 +278,7 @@ export const streamOpenAIResponses: StreamFunction<"openai-responses"> = (
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
calculateCost(model, output.usage);
|
calculateCost(model, output.usage);
|
||||||
|
applyServiceTierPricing(output.usage, response?.service_tier ?? options?.serviceTier);
|
||||||
// Map status to stop reason
|
// Map status to stop reason
|
||||||
output.stopReason = mapStopReason(response?.status);
|
output.stopReason = mapStopReason(response?.status);
|
||||||
if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") {
|
if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") {
|
||||||
|
|
@ -552,6 +554,28 @@ function convertTools(tools: Tool[]): OpenAITool[] {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getServiceTierCostMultiplier(serviceTier: ResponseCreateParamsStreaming["service_tier"] | undefined): number {
|
||||||
|
switch (serviceTier) {
|
||||||
|
case "flex":
|
||||||
|
return 0.5;
|
||||||
|
case "priority":
|
||||||
|
return 2;
|
||||||
|
default:
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyServiceTierPricing(usage: Usage, serviceTier: ResponseCreateParamsStreaming["service_tier"] | undefined) {
|
||||||
|
const multiplier = getServiceTierCostMultiplier(serviceTier);
|
||||||
|
if (multiplier === 1) return;
|
||||||
|
|
||||||
|
usage.cost.input *= multiplier;
|
||||||
|
usage.cost.output *= multiplier;
|
||||||
|
usage.cost.cacheRead *= multiplier;
|
||||||
|
usage.cost.cacheWrite *= multiplier;
|
||||||
|
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
||||||
|
}
|
||||||
|
|
||||||
function mapStopReason(status: OpenAI.Responses.ResponseStatus | undefined): StopReason {
|
function mapStopReason(status: OpenAI.Responses.ResponseStatus | undefined): StopReason {
|
||||||
if (!status) return "stop";
|
if (!status) return "stop";
|
||||||
switch (status) {
|
switch (status) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue